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

Implement Equals & GetHashCode for LingerOption - fix Socket outerloop tests #78747

Merged
merged 2 commits into from
Nov 24, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
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 @@ -41,5 +41,12 @@ public int LingerTime
_lingerTime = value;
}
}

public override bool Equals(object? comparand)
{
return comparand is LingerOption option && option.Enabled == _enabled && option.LingerTime == _lingerTime;
}

public override int GetHashCode() => base.GetHashCode();
wfurt marked this conversation as resolved.
Show resolved Hide resolved
antonfirsov marked this conversation as resolved.
Show resolved Hide resolved
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -283,9 +283,17 @@ internal Socket CopyStateFromSource(Socket source)
// Try to detect if a property gets added that we're not copying correctly.
foreach (PropertyInfo pi in typeof(Socket).GetProperties(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.DeclaredOnly))
{
object? origValue = pi.GetValue(source);
object? cloneValue = pi.GetValue(this);
Debug.Assert(Equals(origValue, cloneValue), $"{pi.Name}. Expected: {origValue}, Actual: {cloneValue}");
try
{
object? origValue = pi.GetValue(source);
object? cloneValue = pi.GetValue(this);

Debug.Assert(Equals(origValue, cloneValue), $"{pi.Name}. Expected: {origValue}, Actual: {cloneValue}");
Copy link
Member

@antonfirsov antonfirsov Nov 23, 2022

Choose a reason for hiding this comment

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

It is very weird that Equals(origValue, cloneValue) == false for LingerOption on Unix only. They should be equal by reference regardless of the platform.

Copy link
Member Author

Choose a reason for hiding this comment

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

Maybe @liveans knows the answer? Part of the difference is that Windows support Accept from socket natively while on Unix we construct it behind the scenes.

Copy link
Member

@liveans liveans Nov 23, 2022

Choose a reason for hiding this comment

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

I just realized that, when we create a new SafeSocketHandle for this purpose, we're not replacing public properties, we're just replacing private variables, that can be root cause of the issue. I'm going to raise a PR for this.

Edit: It turns out that wasn't the problem, but we need Equals function anyway, because we're creating new instance for LingerOption everytime we try to get value from LingerState.

Copy link
Member

Choose a reason for hiding this comment

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

After this PR gets merged, I'll open a PR for transferring handle state between temporary and new handles for this case, as we already did here in ReplaceHandle method:

if (_handle.IsTrackedOption(TrackedSocketOptions.DualMode)) DualMode = _handle.DualMode;
if (_handle.IsTrackedOption(TrackedSocketOptions.DontFragment)) DontFragment = dontFragment;
if (_handle.IsTrackedOption(TrackedSocketOptions.EnableBroadcast)) EnableBroadcast = broadcast;
if (_handle.IsTrackedOption(TrackedSocketOptions.LingerState)) LingerState = linger!;
if (_handle.IsTrackedOption(TrackedSocketOptions.NoDelay)) NoDelay = noDelay;
if (_handle.IsTrackedOption(TrackedSocketOptions.ReceiveBufferSize)) ReceiveBufferSize = receiveSize;
if (_handle.IsTrackedOption(TrackedSocketOptions.ReceiveTimeout)) ReceiveTimeout = receiveTimeout;
if (_handle.IsTrackedOption(TrackedSocketOptions.SendBufferSize)) SendBufferSize = sendSize;
if (_handle.IsTrackedOption(TrackedSocketOptions.SendTimeout)) SendTimeout = sendTimeout;
if (_handle.IsTrackedOption(TrackedSocketOptions.Ttl)) Ttl = ttl;

}
catch (TargetInvocationException ex) when (ex.InnerException is SocketException se && se.SocketErrorCode == SocketError.OperationNotSupported)
{
// macOS fails to retrieve DontFragment and MulticastLoopback at the moment
}
}
#endif
return this;
Expand Down