Repository navigation
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
Conversation
… 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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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
TcpConnectionobject 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.TcpDuplexPipeandTlsDuplexPipehave it, and answer for both halves, which they made at the same moment.PipeReaderandPipeWriter, 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,TlsDecryptingPipeReaderandTlsEncryptingPipeWriter.TcpConnection.generationstays internal.decRef()'s doc now says that a reference kept past the release can end up on the next client's connection, and points toisValid().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
PipeIsValidTestis incommonTest, so it runs on both runtimes. A handler keeps itsTcpDuplexPipeand 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.TlsPipeIsValidTestdoes the same throughTlsDuplexPipe. The kept pipe is already closed, with its session freed, so this also shows that asking touches only the connection.With the change:
TcpPipeWriter, whichPipeIsValidTestcovers.🤖 Generated with Claude Code