Implement stop token in ChannelRaw for improved control#86
Open
planetis-m wants to merge 5 commits intonim-lang:masterfrom
Open
Implement stop token in ChannelRaw for improved control#86planetis-m wants to merge 5 commits intonim-lang:masterfrom
planetis-m wants to merge 5 commits intonim-lang:masterfrom
Conversation
Added stop token functionality to ChannelRaw for better control over channel state. Updated channelSend and channelReceive to handle timeout and stop conditions.
Add tests for channel stop behavior and timeouts.
planetis-m
commented
Feb 20, 2026
Contributor
Author
|
Ready I think. |
Contributor
Author
|
I use this implementation in my app: https://github.com/planetis-m/pdfocr |
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.
Summary
This PR makes
Chansafer to shut down and easier to use in real-world threaded systems:Atomic[bool]) and public control APIs:chan.stop()to request shutdown and wake blocked waiterschan.stopToken()to query stop statesend(..., timeout = default(Duration)) -> boolrecv(..., timeout = default(Duration)) -> booltimeout != default(Duration)Why this should be merged
Today, blocking channel operations can wait indefinitely unless callers build their own cancellation/time budget logic around them. This PR moves those concerns into the primitive itself, which improves correctness and reduces boilerplate in every caller.
Concretely, this improves:
stop().Duration.Behavioral details
trySend/tryRecvstay non-blocking.send/recvas statements continue to compile; return values are now available for timeout/stop outcomes.recv()/recvIso()value-returning forms now raise"channel stopped"if they cannot receive due to stop.=destroyon slot storage).Tests
Added:
tests/tchannels_stop_timeout.nimCovers:
recvsendAlso validated existing channel tests still pass:
tests/tchannels_simple.nimtests/tchannels_singlebuf.nimtests/tchannels_cooperative.nimRisk / compatibility
send/recvnow returnbool.sleep(1ms)) becauselockscondvars in this runtime do not expose timed wait primitives.