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 Http2 deadlock #99889

Merged
merged 3 commits into from
Mar 21, 2024
Merged
Show file tree
Hide file tree
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 @@ -257,6 +257,7 @@ private void Shutdown()
if (NetEventSource.Log.IsEnabled()) Trace($"{nameof(_shutdown)}={_shutdown}, {nameof(_abortException)}={_abortException}");

Debug.Assert(Monitor.IsEntered(SyncObject));
Debug.Assert(!_pool.HasSyncObjLock);

if (!_shutdown)
{
Expand All @@ -276,6 +277,8 @@ private void Shutdown()

public bool TryReserveStream()
{
Debug.Assert(!_pool.HasSyncObjLock);

lock (SyncObject)
{
if (_shutdown)
Expand All @@ -302,6 +305,8 @@ public bool TryReserveStream()
// Otherwise, will be called when the request is complete and stream is closed.
public void ReleaseStream()
{
Debug.Assert(!_pool.HasSyncObjLock);

lock (SyncObject)
{
if (NetEventSource.Log.IsEnabled()) Trace($"{nameof(_streamsInUse)}={_streamsInUse}");
Expand Down Expand Up @@ -333,6 +338,8 @@ public void ReleaseStream()
// Returns false to indicate that the connection is shutting down and cannot be used anymore
public Task<bool> WaitForAvailableStreamsAsync()
{
Debug.Assert(!_pool.HasSyncObjLock);

lock (SyncObject)
{
Debug.Assert(_availableStreamsWaiter is null, "As used currently, shouldn't already have a waiter");
Expand Down Expand Up @@ -730,6 +737,7 @@ private void ProcessAltSvcFrame(FrameHeader frameHeader)
{
if (NetEventSource.Log.IsEnabled()) Trace($"{frameHeader}");
Debug.Assert(frameHeader.Type == FrameType.AltSvc);
Debug.Assert(!Monitor.IsEntered(SyncObject));

ReadOnlySpan<byte> span = _incomingBuffer.ActiveSpan.Slice(0, frameHeader.PayloadLength);

Expand Down Expand Up @@ -1304,6 +1312,8 @@ private Task SendRstStreamAsync(int streamId, Http2ProtocolErrorCode errorCode)

internal void HeartBeat()
{
Debug.Assert(!_pool.HasSyncObjLock);

if (_shutdown)
return;

Expand Down Expand Up @@ -1810,10 +1820,14 @@ private bool ForceSendConnectionWindowUpdate()

public override long GetIdleTicks(long nowTicks)
{
lock (SyncObject)
{
return _streamsInUse == 0 ? base.GetIdleTicks(nowTicks) : 0;
}
// The pool is holding the lock as part of its scavenging logic.
// We must not lock on Http2Connection.SyncObj here as that could lead to lock ordering problems.
Debug.Assert(_pool.HasSyncObjLock);

// There is a race condition here where the connection pool may see this connection as idle right before
// we start processing a new request and start its disposal. This is okay as we will either
// return false from TryReserveStream, or process pending requests before tearing down the transport.
return _streamsInUse == 0 ? base.GetIdleTicks(nowTicks) : 0;
}

/// <summary>Abort all streams and cause further processing to fail.</summary>
Expand Down Expand Up @@ -2002,6 +2016,7 @@ private static TaskCompletionSourceWithCancellation<bool> CreateSuccessfullyComp
public async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, bool async, CancellationToken cancellationToken)
{
Debug.Assert(async);
Debug.Assert(!_pool.HasSyncObjLock);
if (NetEventSource.Log.IsEnabled()) Trace($"Sending request: {request}");

try
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -406,7 +406,7 @@ private object SyncObj
}
}

private bool HasSyncObjLock => Monitor.IsEntered(_availableHttp11Connections);
public bool HasSyncObjLock => Monitor.IsEntered(_availableHttp11Connections);

// Overview of connection management (mostly HTTP version independent):
//
Expand Down Expand Up @@ -459,6 +459,8 @@ private static void ThrowGetVersionException(HttpRequestMessage request, int des

private bool CheckExpirationOnGet(HttpConnectionBase connection)
{
Debug.Assert(!HasSyncObjLock);

TimeSpan pooledConnectionLifetime = _poolManager.Settings._pooledConnectionLifetime;
if (pooledConnectionLifetime != Timeout.InfiniteTimeSpan)
{
Expand Down Expand Up @@ -1999,6 +2001,7 @@ private void ReturnHttp2Connection(Http2Connection connection, bool isNewConnect
{
if (NetEventSource.Log.IsEnabled()) connection.Trace($"{nameof(isNewConnection)}={isNewConnection}");

Debug.Assert(!HasSyncObjLock);
Debug.Assert(isNewConnection || initialRequestWaiter is null, "Shouldn't have a request unless the connection is new");

if (!isNewConnection && CheckExpirationOnReturn(connection))
Expand Down Expand Up @@ -2402,6 +2405,7 @@ internal void HeartBeat()
localHttp2Connections = _availableHttp2Connections?.ToArray();
}

// Avoid calling HeartBeat under the lock, as it may call back into HttpConnectionPool.InvalidateHttp2Connection.
if (localHttp2Connections is not null)
{
foreach (Http2Connection http2Connection in localHttp2Connections)
Expand Down