Skip to content

Use a fresh execution context for application logic #46164

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

Closed
wants to merge 3 commits into from
Closed
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 @@ -42,6 +42,7 @@ internal sealed partial class HttpConnectionContext : ConnectionContext,
private IDictionary<object, object?>? _items;
private readonly CancellationTokenSource _connectionClosedTokenSource;
private readonly CancellationTokenSource _connectionCloseRequested;
private ConnectionDelegate? _connectionDelegate;

private CancellationTokenSource? _sendCts;
private bool _activeSend;
Expand Down Expand Up @@ -551,12 +552,50 @@ private async Task ExecuteApplication(ConnectionDelegate connectionDelegate)
// Verify some initialization invariants
Debug.Assert(TransportType != HttpTransportType.None, "Transport has not been initialized yet");

// Jump onto the thread pool thread so blocking user code doesn't block the setup of the
// - Jump onto the thread pool thread so blocking user code doesn't block the setup of the
// connection and transport
await Task.Yield();
// - Discard the current ExecutionContext and SynchronizationContext if any

// Running this in an async method turns sync exceptions into async ones
await connectionDelegate(this);
// Set the connection delegate here so we can avoid the closure allocation in Task.Run.
// We're still paying for the delegate allocation but this happens once per connection so there's no point caching.
_connectionDelegate = connectionDelegate;

if (!ExecutionContext.IsFlowSuppressed())
{
ExecutionContext.SuppressFlow();
}

await Task.Run(ExecuteApplication);

// No need to undo the flow suppression since we're in an async method.
// The suppression won't be observed outside of this call.
}

private async Task ExecuteApplication()
{
var connectionDelegate = _connectionDelegate!;

Debug.Assert(connectionDelegate is not null, "Connection delegate was not set!");

// Clear the field
_connectionDelegate = null;

if (_logger.IsEnabled(LogLevel.Critical))
{
// Preserve the connection id logging scope when we execute the connection delegate
var logScope = new ConnectionLogScope(ConnectionId);

// REVIEW: Should we create also create an activity here

Choose a reason for hiding this comment

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

I guess you meant Should we also create an activity here


using (_logger.BeginScope(logScope))
{
await connectionDelegate(this);
}
}
else
{
await connectionDelegate(this);
}
}

internal void StartSendCancellation()
Expand Down