Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix unhandled KeyNotFoundException in RequestReplyCorrelator #4743

Merged
merged 1 commit into from
Oct 15, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@
// See the LICENSE file in the project root for more information.


using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Collections;
using System.Runtime;
using System.ServiceModel.Diagnostics;
using System.Xml;
Expand All @@ -13,11 +12,11 @@ namespace System.ServiceModel.Channels
{
internal class RequestReplyCorrelator : IRequestReplyCorrelator
{
private Dictionary<Key, object> _states;
private Hashtable _states;

internal RequestReplyCorrelator()
{
_states = new Dictionary<Key, object>();
_states = new Hashtable();
}

void IRequestReplyCorrelator.Add<T>(Message request, T state)
Expand Down Expand Up @@ -45,13 +44,12 @@ T IRequestReplyCorrelator.Find<T>(Message reply, bool remove)
UniqueId relatesTo = GetRelatesTo(reply);
Type stateType = typeof(T);
Key key = new Key(relatesTo, stateType);
T value;
T value = (T)_states[key];
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems like this would have been a potential issue before, but Dictionary would throw here if the key wasn't found, resulting in an exception here instead of returning null. Unless that was the intent of this method. In which case, Hashtable doesn't do that.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's exactly the issue I'm fixing 😄. On NetFx it uses Hashtable. The change to Dictionary caused an uncaught exception thrown right here.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's right there in the title and everything. Doh! 🤦‍♂️


lock (_states)
if (remove)
{
value = (T)_states[key];

if (remove)
// With HashTable, only need to lock when modifying
lock (_states)
{
_states.Remove(key);
}
Expand Down Expand Up @@ -259,7 +257,6 @@ public override bool Equals(object obj)
return other.MessageId == MessageId && other.StateType == StateType;
}

[SuppressMessage(FxCop.Category.Usage, "CA2303:FlagTypeGetHashCode", Justification = "The hashcode is not used for identity purposes for embedded types.")]
public override int GetHashCode()
{
return MessageId.GetHashCode() ^ StateType.GetHashCode();
Expand Down