Skip to content
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 @@ -70,7 +70,9 @@ public bool UnsafeConnectionNtlmAuthentication
{
return;
}
lock ((DisconnectResults as ICollection).SyncRoot)

var disconnectResults = DisconnectResults;
Copy link
Member

Choose a reason for hiding this comment

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

The local variable isn't helpful as it doesn't contain any common expression.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Definition of DisconnectResults

private Dictionary<ulong, DisconnectAsyncResult> DisconnectResults =>
    LazyInitializer.EnsureInitialized(ref _disconnectResults, () => new Dictionary<ulong, DisconnectAsyncResult>());

So every access of DisconnectResults is a call to LazyInitializer.EnsureInitialized.
So using the local variable only one call is necessary.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

What do you want to do?

Remove the local variable and directly access DisconnectResults (this will result in more Volatile.Read of _disconnectResults) or do you have another comment.

Copy link
Member

Choose a reason for hiding this comment

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

HttpListener isn't so optimized. I don't think reducing several volatile reads can result in real world difference. Let's focus on fixing only.

lock ((disconnectResults as ICollection).SyncRoot)
{
if (_unsafeConnectionNtlmAuthentication == value)
{
Expand All @@ -79,7 +81,7 @@ public bool UnsafeConnectionNtlmAuthentication
_unsafeConnectionNtlmAuthentication = value;
if (!value)
{
foreach (DisconnectAsyncResult result in DisconnectResults.Values)
foreach (DisconnectAsyncResult result in disconnectResults.Values)
{
result.AuthenticatedConnection = null;
}
Expand Down Expand Up @@ -694,7 +696,13 @@ public HttpListenerContext EndGetContext(IAsyncResult asyncResult)
// assurance that we do this only for NTLM/Negotiate is not here, but in the
// code that caches WindowsIdentity instances in the Dictionary.
DisconnectAsyncResult? disconnectResult;
DisconnectResults.TryGetValue(connectionId, out disconnectResult);

var disconnectResults = DisconnectResults;
lock ((disconnectResults as ICollection).SyncRoot)
{
disconnectResults.TryGetValue(connectionId, out disconnectResult);
}

if (UnsafeConnectionNtlmAuthentication)
{
if (authorizationHeader == null)
Expand Down Expand Up @@ -1327,7 +1335,12 @@ private static void RegisterForDisconnectNotification(HttpListenerSession sessio
// Need to make sure it's going to get returned before adding it to the hash. That way it'll be handled
// correctly in HandleAuthentication's finally.
disconnectResult = result;
session.Listener.DisconnectResults[connectionId] = disconnectResult;

var disconnectResults = session.Listener.DisconnectResults;
lock ((disconnectResults as ICollection).SyncRoot)
{
disconnectResults[connectionId] = disconnectResult;
}
}

if (statusCode == Interop.HttpApi.ERROR_SUCCESS && HttpListener.SkipIOCPCallbackOnSuccess)
Expand Down Expand Up @@ -1646,8 +1659,21 @@ private void HandleDisconnect()
{
HttpListener listener = _listenerSession.Listener;

if (NetEventSource.Log.IsEnabled()) NetEventSource.Info(this, $"DisconnectResults {listener.DisconnectResults} removing for _connectionId: {_connectionId}");
listener.DisconnectResults.Remove(_connectionId);
var disconnectResults = listener.DisconnectResults;
if (NetEventSource.Log.IsEnabled())
{
string? results;
lock ((disconnectResults as ICollection).SyncRoot)
{
results = disconnectResults.ToString();
}
NetEventSource.Info(this, $"DisconnectResults {results} removing for _connectionId: {_connectionId}");
}

lock ((disconnectResults as ICollection).SyncRoot)
{
disconnectResults.Remove(_connectionId);
}

// Cached identity is disposed with the session context
Session?.Dispose();
Expand Down