Skip to content

Commit

Permalink
HttpListener fix: Operations that change non-concurrent collections m…
Browse files Browse the repository at this point in the history
…ust have exclusive access (#107804)
  • Loading branch information
pjannesen authored Oct 2, 2024
1 parent 6906730 commit 0d86380
Showing 1 changed file with 32 additions and 6 deletions.
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;
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

0 comments on commit 0d86380

Please sign in to comment.