Skip to content

tcp, tls: isValid() says whether a pipe's connection still serves the client it was made for - connections are pooled, so a pipe kept past its handler can be on the next client's, and kronos leaves the check to whoever kept it (#23) - #52

Merged
MDA2AV merged 2 commits into
mainfrom
fix/pipe-generation
Nov 9, 2026

Conversation

@MDA2AV

@MDA2AV MDA2AV commented Nov 8, 2026 •

Copy link
Copy Markdown
Owner

Part of #23, the way it was decided: kronos doesn't stop code that keeps a connection past its life. A pipe can now say whether it's still on its own client, and checking is up to whoever kept it. #23 stays open, marked partially solved, for the connection's own API, which has no such check.

What was wrong

Connections are pooled. Once a client is gone, and both the handler and the proactor have let go, the same TcpConnection object is reset and handed to the next client. Code that kept the connection or a pipe over it past its handler, such as a fire-and-forget coroutine or a list of subscribers, then reads and writes that next client's connection. It had no way to tell: the generation that changes at each recycle is internal, and the caller had no copy to compare. #23 measured a kept pipe writer delivering one client's bytes to the next.

What changed

  • isValid(): true while the connection under a pipe still serves the client the pipe was made for, false once it has been recycled for another.
    • TcpDuplexPipe and TlsDuplexPipe have it, and answer for both halves, which they made at the same moment.
    • So does each half, through PipeReader and PipeWriter, for code that keeps only one of them. Each of the four saves its connection's generation when it's made and compares it with the connection's at each call: TcpPipeReader, TcpPipeWriter, TlsDecryptingPipeReader and TlsEncryptingPipeWriter.
  • Nothing refuses. A pipe used past its client still reads and writes as before. The docs say to ask on the proactor thread, where the recycle runs, so nothing can come between the check and the call.
  • TcpConnection.generation stays internal. decRef()'s doc now says that a reference kept past the release can end up on the next client's connection, and points to isValid().
  • For an implementation outside kronos, the two interfaces gain isValid() to implement. This repo has none besides the four.

MDA2AV/ioxide#553 takes the other road for ioxide: its pipe writer and stream refuse once their connection's life ends.

Tests

  • PipeIsValidTest is in commonTest, so it runs on both runtimes. A handler keeps its TcpDuplexPipe and returns. The pipe and both halves are valid while it runs, and still valid after it has returned while its client is connected. Once that client leaves and the pooled object serves the next one, the kept pipe and its halves are not valid, and the new pipe's are. The test also checks that the second client got the same object, without which it would prove nothing.
  • TlsPipeIsValidTest does the same through TlsDuplexPipe. The kept pipe is already closed, with its session freed, so this also shows that asking touches only the connection.

With the change:

  • Native: all 318 tests passed, the 2 new ones among them. The new ones passed 5 runs in a row.
  • JVM: all 93 tests passed, the new one among them.
  • TLS: the test runs BoringSSL's pipes. Under kTLS TX the TLS pipe's writer is a TcpPipeWriter, which PipeIsValidTest covers.

🤖 Generated with Claude Code

… the client it was made for - connections are pooled, so a pipe kept past its handler can be on the next client's, and kronos leaves the check to whoever kept it (#23)

Connections are pooled: once a client is gone, the same TcpConnection object is reset and handed to the next one,
and a pipe kept past its handler then reads and writes that client's connection. Each pipe now saves its
connection's generation when it's made, and isCurrent, on PipeReader and PipeWriter, compares it with the
connection's. Nothing refuses: a pipe used past its client works as before, and checking is up to whoever kept it.

TcpConnection.generation stays internal; decRef()'s doc says what a kept reference does and points to isCurrent.
… have it too, answering for both halves

Renamed from isCurrent, and a function now. A duplex pipe makes its two halves at the same moment, so either half's
answer is the pipe's; each half keeps its own for code that holds only one of them.
@MDA2AV MDA2AV changed the title tcp, tls: a pipe's isCurrent says whether its connection still serves the client it was made for - connections are pooled, so a pipe kept past its handler can be on the next client's, and kronos leaves the check to whoever kept it (#23) tcp, tls: isValid() says whether a pipe's connection still serves the client it was made for - connections are pooled, so a pipe kept past its handler can be on the next client's, and kronos leaves the check to whoever kept it (#23) Nov 8, 2026
@MDA2AV
MDA2AV merged commit b897256 into main Nov 9, 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.

1 participant