close
Skip to content

Fix race condition in HttpSys shutdown request draining logic#65330

Closed
Copilot wants to merge 2 commits into
brecon/httpsysshutdownfrom
copilot/sub-pr-65217
Closed

Fix race condition in HttpSys shutdown request draining logic#65330
Copilot wants to merge 2 commits into
brecon/httpsysshutdownfrom
copilot/sub-pr-65217

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented Feb 4, 2026

Fix race condition in HttpSys shutdown request draining logic

  • You've read the Contributor Guide and Code of Conduct.
  • You've included unit or integration tests for your change, where applicable.
  • You've included inline docs for your change, where applicable.
  • There's an open issue for the PR that you are making. If you'd like to propose a new feature or change, please open an issue to discuss the change or find an existing issue.

Addresses potential hang in StopAsync due to memory visibility issues when checking shutdown state.

Description

Without proper synchronization, DecrementOutstandingRequest could observe stale _acceptLoopsCompleted.Task.IsCompleted state after the last request completes, causing _requestsDrained to never signal and StopAsync to hang.

Changes:

  • Take _shutdownLock in DecrementOutstandingRequest when count == 0 before checking _acceptLoopsCompleted state
  • Ensures memory visibility with AcceptLoopCompleted which also holds the lock when setting completion
  • Lock only acquired once during server lifetime (when final request completes during shutdown)
internal int DecrementOutstandingRequest()
{
    var count = Interlocked.Decrement(ref _outstandingRequests);

    if (count == 0)
    {
        lock (_shutdownLock)
        {
            if (_acceptLoopsCompleted.Task.IsCompleted)
            {
                _requestsDrained.TrySetResult();
            }
        }
    }

    return count;
}

💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

Co-authored-by: halter73 <54385+halter73@users.noreply.github.com>
Copilot AI changed the title [WIP] Update HttpSys shutdown lock logic based on review feedback Fix race condition in HttpSys shutdown request draining logic Feb 4, 2026
Copilot AI requested a review from halter73 February 4, 2026 21:30
@halter73 halter73 closed this Feb 4, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants