Skip to content

TlsContext/TlsSession: document lifetime, tighten AttachSocket - #131457

Open
wfurt wants to merge 1 commit into
dotnet:mainfrom
wfurt:TlsSession-followup
Open

TlsContext/TlsSession: document lifetime, tighten AttachSocket#131457
wfurt wants to merge 1 commit into
dotnet:mainfrom
wfurt:TlsSession-followup

Conversation

@wfurt

@wfurt wfurt commented Jul 28, 2026

Copy link
Copy Markdown
Member

Follow-up on #130366 — small, safe cleanup driven by post-merge review feedback. The larger SslStream/TlsSession unification (turning SslStream into a thin adapter over TlsBufferSession and retiring the parallel PAL glue) will land as its own dedicated PR — that discussion had already outgrown this one.

Changes

  • TlsContext XML remarks — drop the promise that it is safe to Dispose the context while sessions are still in use. Native handles (SSL_CTX, SChannel credentials) are ref-counted, but SslStreamCertificateContext is not IDisposable and has no refcount, so an internally-built cert chain gets released on the context's Dispose even when live sessions still hold a reference. The revised docs make the ownership contract explicit — the context retains cert contexts it built itself (raw ServerCertificate), caller-supplied ServerCertificateContext stays caller-owned, and the caller keeps the context alive for as long as any session derived from it is in use. This mirrors the ownership model used by SslStream today (r3624718621).

  • TlsSession.AttachSocket — tighten from internal to private protected, add Debug.Asserts that the session is fresh at the call site (no prior socket handle, no managed Socket wrapper, no PAL security context, not disposed), and update the leading comment to reflect that it's called from OnContextInitialized after the base InitializeFromContext runs, not from TlsSocketSession's constructor as the old comment claimed (bartonjs r3562414767).

No public API change. No behavior change outside Debug.Asserts.

Note

This PR description was drafted with GitHub Copilot assistance.

Copilot AI review requested due to automatic review settings July 28, 2026 10:56
@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
Successfully started running 4 pipeline(s).
12 pipeline(s) were filtered out due to trigger conditions.
There may be pipelines that require an authorized user to comment /azp run to run.

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 removes the internal SslStreamTlsSession “wedge” integration path (and associated internal seams), reverting SslStream handshaking to its existing PAL-driven GenerateToken flow on all platforms. It also tightens/clarifies parts of the standalone TlsContext/TlsSession implementation and documentation based on post-merge review feedback.

Changes:

  • Delete the SslStream handshake wedge (files, csproj wiring, and NextMessage short-circuit) so SslStream no longer routes handshakes through TlsSession.
  • Simplify TlsContext / TlsSession by removing wedge-only state and APIs, and by always cloning per-session options.
  • Update TlsContext XML remarks to document correct ownership/lifetime expectations; tighten TlsSession.AttachSocket accessibility and add debug invariants.

Reviewed changes

Copilot reviewed 9 out of 9 changed files in this pull request and generated 1 comment.

Show a summary per file
File Description
src/libraries/System.Net.Security/src/System/Net/Security/TlsSession.cs Removes wedge-only members/surface area; tightens AttachSocket; adjusts disposal behavior.
src/libraries/System.Net.Security/src/System/Net/Security/TlsContext.OpenSsl.cs Removes wedge-mode early-return so the context-owned SSL_CTX is always established when applicable.
src/libraries/System.Net.Security/src/System/Net/Security/TlsContext.cs Removes wedge mode plumbing; always clones per-session options; updates XML remarks about ownership/lifetime.
src/libraries/System.Net.Security/src/System/Net/Security/SslStream.TlsSessionWedge.cs Removed (eliminates the handshake routing wedge).
src/libraries/System.Net.Security/src/System/Net/Security/SslStream.Protocol.cs Removes NextMessage wedge short-circuit and the partial method declaration.
src/libraries/System.Net.Security/src/System/Net/Security/SslStream.NoTlsSession.cs Removed (no longer needed after wedge deletion).
src/libraries/System.Net.Security/src/System/Net/Security/SslStream.cs Stops setting the wedge-only SslStream reference on the options bag; keeps validator wiring.
src/libraries/System.Net.Security/src/System/Net/Security/SslAuthenticationOptions.cs Removes wedge-only SslStream field; updates clone/copy comments accordingly.
src/libraries/System.Net.Security/src/System.Net.Security.csproj Removes conditional compilation entries for the wedge files.

{
_options.Dispose();
}
_options?.Dispose();
Copilot AI review requested due to automatic review settings July 28, 2026 11:06
@wfurt
wfurt force-pushed the TlsSession-followup branch from f57b2bb to 322f47f Compare July 28, 2026 11:06
@wfurt wfurt changed the title SslStream/TlsSession: remove the TlsSession wedge + address post-merge review TlsContext/TlsSession: document lifetime, tighten AttachSocket Jul 28, 2026

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

Copilot reviewed 2 out of 2 changed files in this pull request and generated no new comments.

Address post-merge review feedback on dotnet#130366:

- TlsContext XML remarks: drop the promise that it is safe to Dispose
  the context while sessions are still in use. The native handles are
  ref-counted, but SslStreamCertificateContext is not IDisposable and
  has no refcount, so an internally-built cert chain gets released on
  the context's Dispose even when live sessions still hold a reference.
  Document the ownership rules (context owns cert contexts it built,
  caller-supplied ServerCertificateContext stays caller-owned) and the
  lifetime requirement (context must outlive all sessions), mirroring
  the model used by SslStream (r3624718621).

- TlsSession.AttachSocket: tighten from internal to private protected,
  add Debug.Asserts that the session is fresh at the call site (no
  prior socket handle / managed Socket wrapper / PAL security context,
  not disposed), and update the comment to reflect that it is called
  from OnContextInitialized after the base InitializeFromContext runs
  (bartonjs r3562414767).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants