Skip to content

tls: the proactor frees the TLS services started on it when it stops (#46) - #57

Merged
MDA2AV merged 2 commits into
mainfrom
fix/tls-service-close
Nov 11, 2026
Merged

MDA2AV merged 2 commits into
mainfrom
fix/tls-service-close

Conversation

@MDA2AV

@MDA2AV MDA2AV commented Nov 11, 2026 •

Copy link
Copy Markdown
Owner

Closes #46.

Nothing freed what TlsService.start built: the SSL_CTX with its certificate and key, the ALPN list its callback reads, and the kTLS handoff's buffer, made on first use. A host starting servers again in one process kept them all, once per proactor per start.

The proactor now frees them when it stops, as MDA2AV/ioxide#562 has the reactor do, so hosts change nothing. start hands each service to its proactor, and teardown, on the proactor's own thread, frees them last, after the drain #53 added. A session still open holds its own reference, so the context goes with the last of them. A context of 0 marks a freed service, so there is no new flag, and accept refuses it. A start whose context fails to build now frees the ALPN list it made. The Ktor engine gets this with no change.

With #54, now merged. Every proactor started with one TlsOptions instance shares one context, so the first to stop must not free it under the rest. TlsOptions also keeps the ALPN list and a count of the services on the context, under the lock #54 builds it under:

  • start raises the count. A proactor started while others still run shares the context.
  • Each service's free drops its own handoff buffer and lowers the count. The one that reaches zero frees the context and the list and clears them, so a start after that builds another.

Tests.

…46)

Nothing freed what TlsService.start built: the SSL_CTX with its certificate and key, the ALPN list its callback reads and the kTLS handoff's buffer. A host starting servers again in one process kept them all, once per proactor per start.

Hosts already stop their proactors, and teardown, on the proactor's own thread, already frees all else the proactor owns. So start hands each service to its proactor, and teardown frees them last. A context of 0 marks a freed service, which accept refuses, and a start whose context fails to build frees the ALPN list it made.
@MDA2AV
MDA2AV force-pushed the fix/tls-service-close branch from 8bd0946 to 034c503 Compare November 11, 2026 17:02
@MDA2AV MDA2AV changed the title tls: TlsService.close frees the context, its ALPN list and the handoff buffer once the proactor has stopped (#46) tls: the proactor frees the TLS services started on it when it stops (#46) Nov 11, 2026
…ptions (#54) count their services, so the last to stop frees the context

With #54 every proactor started with one TlsOptions instance shares its context, so the first proactor to stop could no longer free it under the rest. TlsOptions also keeps the ALPN list the context's callback reads and a count of the services on it, under the lock #54 builds it under: start raises the count, free lowers it, and the one that reaches zero frees the context and the list and clears them, so a start after that builds another. Each service still frees its own kTLS handoff buffer, and teardown frees the services after #53's drain, last of all.

Tests: TlsResumptionTest runs one-proactor servers on one port through a stop, a later start, and a start after the last stopped; the heap test runs two proactors sharing one options instance.
@MDA2AV
MDA2AV merged commit d874cf8 into main Nov 11, 2026
3 checks passed
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.

tls: TlsService has no way to free its context

1 participant