Skip to content

Run DirectTls handshake user callbacks off the epoll pump thread - #68664

Merged
wtgodbe merged 16 commits into
dotnet:release/11.0from
DeagleGross:dmkorolev/directtls/dispatch-user-callback-net11
Aug 24, 2026
Merged

wtgodbe merged 16 commits into
dotnet:release/11.0from
DeagleGross:dmkorolev/directtls/dispatch-user-callback-net11

Conversation

@DeagleGross

Copy link
Copy Markdown
Member

DirectTls invoked user-supplied handshake callbacks — the certificate selector, the ClientHello bytes callback, and client-certificate validation — synchronously on the epoll pump thread. A slow callback stalled accept, handshakes and read/write readiness for every connection assigned to that pump.

The pump now suspends the handshake instead of calling user code inline: it de-arms the fd, copies what the callback needs off the session, and queues a pooled IThreadPoolWorkItem. When the callback finishes, the result goes on a lock-free queue and an eventfd registered in the pump's epoll set wakes it, so the handshake resumes on its owning thread. The TlsSocketSession / OpenSSL object is still only ever touched by the pump.

Details:

  • Exactly-once resume via reference identity on the pending callback token; a suspended connection is not serviced while parked.
  • Teardown: handshakes parked on a callback when the pump exits are deferred to a drained-shutdown path, so a session is never disposed under live user code. TlsSocketSession.Dispose remains the single point where the fd is closed.
  • Callback exceptions drop that one handshake and are logged; they do not reach the pump loop.
  • Fast path: when neither ServerCertificateSelector nor TlsClientHelloBytesCallback is configured, context resolution cannot reach user code, so it runs inline and skips the suspension, the thread-pool hop and the early DirectTlsConnection allocation.

Closes #68647

@DeagleGross DeagleGross self-assigned this Aug 20, 2026
Copilot AI lite review requested due to automatic review settings August 20, 2026 14:02
@DeagleGross DeagleGross added the area-networking Includes servers, yarp, json patch, bedrock, websockets, http client factory, and http abstractions label Aug 20, 2026
@DeagleGross
DeagleGross requested a review from SamMonoRT as a code owner August 20, 2026 14:02

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

This PR updates Kestrel’s DirectTls transport so user-supplied TLS handshake callbacks (certificate selection, ClientHello inspection, and client-certificate validation) no longer run inline on the epoll pump thread. Instead, the pump suspends the handshake, queues the callback to the thread pool, and resumes the handshake on the owning pump thread via an eventfd wakeup and a completion queue—preventing a slow callback from stalling unrelated connections on the same pump.

Changes:

  • Add a suspend/resume mechanism for handshake user callbacks using pooled IThreadPoolWorkItems, a completion queue, and an eventfd wakeup.
  • Introduce a fast-path to resolve TLS context inline on the pump thread when resolution cannot reach user code.
  • Add targeted tests for callback dispatch behavior and fast-path thread affinity.
Show a summary per file
File Description
src/Servers/Kestrel/Transport.DirectTls/test/DirectTlsUserCallbackDispatchTests.cs New tests ensuring blocking/throwing selector/ClientHello callbacks don’t stall other handshakes on the same pump.
src/Servers/Kestrel/Transport.DirectTls/test/DirectTlsContextResolverFastPathTests.cs New tests verifying the context resolver runs inline on the pump thread only when it can’t reach user code.
src/Servers/Kestrel/Transport.DirectTls/src/TlsEventPumpPool.cs Plumbs a flag down to pumps indicating whether a server certificate selector is configured.
src/Servers/Kestrel/Transport.DirectTls/src/TlsEventPump.UserCallbacks.cs New pump half implementing callback suspension, wakeup, completion draining, and resume logic.
src/Servers/Kestrel/Transport.DirectTls/src/TlsEventPump.cs Integrates callback suspension/resume into the pump loop and handshake state machine; adds inline resolver fast-path and shutdown draining changes.
src/Servers/Kestrel/Transport.DirectTls/src/Interop/NativeTls.cs Adds eventfd, read, and write interop plus eventfd flags/constants.
src/Servers/Kestrel/Transport.DirectTls/src/HandshakeUserCallback.cs New thread-pool work item base + concrete callback types for context resolution and client-cert validation.
src/Servers/Kestrel/Transport.DirectTls/src/DirectTlsTransportFactory.cs Passes selector-configured flag into the listener/pump setup.
src/Servers/Kestrel/Transport.DirectTls/src/Connection/DirectTlsConnectionListener.cs Stores/plumbs selector-configured flag into pump startup.
src/Servers/Kestrel/Transport.DirectTls/src/ClientCertificateValidator.cs Updates remarks to reflect validation work now running off the pump thread.

Review details

💡 Add a code-review agent skill for context-aware, tailored reviews. Learn more in the docs.

Suppressed comments (1)

src/Servers/Kestrel/Transport.DirectTls/test/DirectTlsContextResolverFastPathTests.cs:129

  • Similar to the handshake task, awaiting a no-op continuation on the drain task doesn't observe exceptions. If the drain loop faults, this can still become an unobserved task exception later.
            await listener.DisposeAsync();
            await drain.ContinueWith(static _ => { }, TaskScheduler.Default);
  • Files reviewed: 10/10 changed files
  • Comments generated: 5
  • Review effort level: Lite

Comment thread src/Servers/Kestrel/Transport.DirectTls/src/TlsEventPump.cs Outdated
Comment thread src/Servers/Kestrel/Transport.DirectTls/src/TlsEventPump.cs
Comment thread src/Servers/Kestrel/Transport.DirectTls/src/HandshakeUserCallback.cs Outdated
Comment thread src/Servers/Kestrel/Transport.DirectTls/src/HandshakeUserCallback.cs Outdated
Comment thread src/Servers/Kestrel/Transport.DirectTls/src/TlsEventPump.cs
Comment thread src/Servers/Kestrel/Transport.DirectTls/src/TlsEventPump.UserCallbacks.cs Outdated
Comment thread src/Servers/Kestrel/Transport.DirectTls/src/TlsEventPump.cs Outdated
Comment thread src/Servers/Kestrel/Transport.DirectTls/src/TlsEventPump.UserCallbacks.cs Outdated
Comment thread src/Servers/Kestrel/Core/src/DirectTlsEndpointOptions.cs Outdated
Comment thread src/Servers/Kestrel/Core/src/DirectTlsEndpointOptions.cs Outdated
Comment thread src/Servers/Kestrel/Core/src/DirectTlsEndpointOptions.cs Outdated
@wtgodbe
wtgodbe merged commit 49e0516 into dotnet:release/11.0 Aug 24, 2026
26 checks passed
@DeagleGross
DeagleGross deleted the dmkorolev/directtls/dispatch-user-callback-net11 branch August 24, 2026 16:31
@dotnet-milestone-bot dotnet-milestone-bot Bot added this to the 11.0-rc2 milestone Aug 24, 2026
wtgodbe pushed a commit that referenced this pull request Sep 14, 2026
…e 11 (#69144)

* Revert "Run DirectTls handshake user callbacks off the epoll pump thread (#68664)"

This reverts commit 49e0516.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>

* Revert "[Experimental] DirectTls transport implementation (#67912)"

This reverts commit 07d11eb.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>

* Apply suggestion from @DeagleGross

---------

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
wtgodbe added a commit that referenced this pull request Sep 16, 2026
* [SignalR] Fix CTS disposal (#69158)

* Fix: hardening http2 to refuse newline chars on trailers and dynamic HPACK table (#68743)

* Hardening trailers to refuse new line chars

* Hardening HPACK dynamic header to refuse new line chars

* Http3 request trailers with newline chars test

* Added test to exercise newline chars on indexed headers

* Fix flaky HTTP/3 request trailers test

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>

* [SignalR] [Java] Fix hang when reading an HTTP response body fails (#69037) (#69206)

Co-authored-by: Brennan <brecon@microsoft.com>

* Add AggressiveOptimization to key long-running async methods (#69287)

* [DirectTLS] [release/11.0] exclude experimental transport from release 11 (#69144)

* Revert "Run DirectTls handshake user callbacks off the epoll pump thread (#68664)"

This reverts commit 49e0516.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>

* Revert "[Experimental] DirectTls transport implementation (#67912)"

This reverts commit 07d11eb.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>

* Apply suggestion from @DeagleGross

---------

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>

* Update dependencies from https://github.com/dotnet/extensions build 20260909.4 (#69269)

On relative base path root
Microsoft.Extensions.Caching.Hybrid , Microsoft.Extensions.Diagnostics.Testing , Microsoft.Extensions.ServiceDiscovery , Microsoft.Extensions.ServiceDiscovery.Yarp , Microsoft.Extensions.TimeProvider.Testing From Version 10.9.0 -> To Version 10.10.0

Co-authored-by: dotnet-maestro[bot] <dotnet-maestro[bot]@users.noreply.github.com>

* [release/11.0] Source code updates from dotnet/dotnet (#69256)

* Backflow from https://github.com/dotnet/dotnet / e775569 build 331727

Diff: https://github.com/dotnet/dotnet/compare/48e3f46c03e3ed36611a49fd7107330dade9f7c8..e775569ca8ce94c363d910311f5f889e648ddc23

From: dotnet/dotnet@48e3f46
To: dotnet/dotnet@e775569

[[ commit created by automation ]]

* Update dependencies from build 331727
Updated Dependencies:
Microsoft.NET.Runtime.WebAssembly.Sdk, Microsoft.NETCore.BrowserDebugHost.Transport, Microsoft.NET.Runtime.MonoAOTCompiler.Task, dotnet-ef, Microsoft.Bcl.AsyncInterfaces, Microsoft.Bcl.Cryptography, Microsoft.Bcl.TimeProvider, Microsoft.EntityFrameworkCore, Microsoft.EntityFrameworkCore.Design, Microsoft.EntityFrameworkCore.InMemory, Microsoft.EntityFrameworkCore.Relational, Microsoft.EntityFrameworkCore.Sqlite, Microsoft.EntityFrameworkCore.SqlServer, Microsoft.EntityFrameworkCore.Tools, Microsoft.Extensions.Caching.Abstractions, Microsoft.Extensions.Caching.Memory, Microsoft.Extensions.Configuration, Microsoft.Extensions.Configuration.Abstractions, Microsoft.Extensions.Configuration.Binder, Microsoft.Extensions.Configuration.CommandLine, Microsoft.Extensions.Configuration.EnvironmentVariables, Microsoft.Extensions.Configuration.FileExtensions, Microsoft.Extensions.Configuration.Ini, Microsoft.Extensions.Configuration.Json, Microsoft.Extensions.Configuration.UserSecrets, Microsoft.Extensions.Configuration.Xml, Microsoft.Extensions.DependencyInjection, Microsoft.Extensions.DependencyInjection.Abstractions, Microsoft.Extensions.DependencyModel, Microsoft.Extensions.Diagnostics, Microsoft.Extensions.Diagnostics.Abstractions, Microsoft.Extensions.FileProviders.Abstractions, Microsoft.Extensions.FileProviders.Composite, Microsoft.Extensions.FileProviders.Physical, Microsoft.Extensions.FileSystemGlobbing, Microsoft.Extensions.HostFactoryResolver.Sources, Microsoft.Extensions.Hosting, Microsoft.Extensions.Hosting.Abstractions, Microsoft.Extensions.Http, Microsoft.Extensions.Logging, Microsoft.Extensions.Logging.Abstractions, Microsoft.Extensions.Logging.Configuration, Microsoft.Extensions.Logging.Console, Microsoft.Extensions.Logging.Debug, Microsoft.Extensions.Logging.EventLog, Microsoft.Extensions.Logging.EventSource, Microsoft.Extensions.Logging.TraceSource, Microsoft.Extensions.Options, Microsoft.Extensions.Options.ConfigurationExtensions, Microsoft.Extensions.Options.DataAnnotations, Microsoft.Extensions.Primitives, Microsoft.Internal.Runtime.AspNetCore.Transport, Microsoft.NETCore.App.Ref, Microsoft.NETCore.Platforms, System.Collections.Immutable, System.Composition, System.Configuration.ConfigurationManager, System.Diagnostics.DiagnosticSource, System.Diagnostics.EventLog, System.Diagnostics.PerformanceCounter, System.DirectoryServices.Protocols, System.Formats.Asn1, System.Formats.Cbor, System.IO.Hashing, System.IO.Pipelines, System.Memory.Data, System.Net.Http.Json, System.Net.Http.WinHttpHandler, System.Net.ServerSentEvents, System.Numerics.Tensors, System.Reflection.Metadata, System.Resources.Extensions, System.Runtime.Caching, System.Security.Cryptography.Pkcs, System.Security.Cryptography.Xml, System.Security.Permissions, System.ServiceProcess.ServiceController, System.Text.Encodings.Web, System.Text.Json, System.Threading.AccessControl, System.Threading.Channels, System.Threading.RateLimiting (Version 11.0.0-rc.2.26458.109 -> 11.0.0-rc.2.26461.110)
Microsoft.DotNet.Arcade.Sdk, Microsoft.DotNet.Build.Tasks.Archives, Microsoft.DotNet.Build.Tasks.Installers, Microsoft.DotNet.Build.Tasks.Templating, Microsoft.DotNet.Helix.JobMonitor, Microsoft.DotNet.Helix.Sdk, Microsoft.DotNet.RemoteExecutor, Microsoft.DotNet.SharedFramework.Sdk (Version 11.0.0-beta.26458.109 -> 11.0.0-beta.26461.110)
Microsoft.Web.Xdt (Version 3.3.0-rc.2.26458.109 -> 3.3.0-rc.2.26461.110)
NuGet.Frameworks, NuGet.Packaging, NuGet.Versioning (Version 7.12.0-rc.45909 -> 7.12.0-rc.46210)
[[ commit created by automation ]]

* Fix authorization component Razor compilation

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

* test(iis): scope upgrade MaxRequestBodySize assert to in-process hosting

- in-process must clear the limit on upgrade because its read loop enforces it
- out-of-process runs on Kestrel, which never enforces it once upgraded
- unblocks the three WebSocketsOutOfProcessTests failures

🧪 - Generated by Copilot

(cherry picked from commit befb2e9)

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

---------

Co-authored-by: dotnet-maestro[bot] <dotnet-maestro[bot]@users.noreply.github.com>
Co-authored-by: William Godbe <wigodbe@microsoft.com>
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Roman Konecny <rokonecn@microsoft.com>

* Remove experimental DirectTLS transport

Carry the release/11.0 removal from #69144 into main while preserving the other auto-merge conflict resolutions.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

---------

Co-authored-by: Brennan <brecon@microsoft.com>
Co-authored-by: Alan Morais Fialho <51328556+amf-fs@users.noreply.github.com>
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
Co-authored-by: Jakob Botsch Nielsen <Jakob.botsch.nielsen@gmail.com>
Co-authored-by: Sam Patel <46026722+SamMonoRT@users.noreply.github.com>
Co-authored-by: Korolev Dmitry <dmkorolev@microsoft.com>
Co-authored-by: dotnet-maestro[bot] <42748379+dotnet-maestro[bot]@users.noreply.github.com>
Co-authored-by: dotnet-maestro[bot] <dotnet-maestro[bot]@users.noreply.github.com>
Co-authored-by: William Godbe <wigodbe@microsoft.com>
Co-authored-by: Roman Konecny <rokonecn@microsoft.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area-networking Includes servers, yarp, json patch, bedrock, websockets, http client factory, and http abstractions

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants