14 Commits

Author SHA1 Message Date
Jeroen van Rijn
1903d7211e Fix early join after start. 2025-06-21 11:47:00 +02:00
Laytan Laats
30e1c96269 uncomment test, see if it's fixed 2025-06-20 22:15:21 +02:00
Laytan
7f648d11d6 Merge pull request #5329 from JackMordaunt/jfm-fix_chan_try_send
chan: fix try_send and send
2025-06-20 22:11:39 +02:00
Jeroen van Rijn
1bd48df41f Disable test_try_select_raw_happy 2025-06-16 12:59:24 +02:00
Jack Mordaunt
130b2dc36d tests/core/sync/chan: test concurrent send/close/recv
This test is designed to ensure that a call to send will always
correctly report whether the value was transmitted. If recv wins, a
close call should not be able to intercept the send thread.
2025-06-13 18:07:21 -03:00
Jack Mordaunt
760d8c1cdd core/sync/chan.send: return false if channel is closed while blocked
This commit makes send behave the same as recv: that the call will
return false if the channel is closed while a thread is waiting on the
blocking operation.

Prior logic would have send return true even if the channel was actually
closed rather than read from.

Docs adjusted to make this clear.
Tests added to lock in this behaviour.
2025-06-12 17:35:49 -03:00
Jack Mordaunt
2d12e265cc tests/core/sync/chan: add test for contended try_send
This test ensures that contending threads racing to try_send against a
single blocking read will result in exactly one winner without any
senders blocking.
2025-06-12 17:35:48 -03:00
Jack Mordaunt
c29168f76f core/sync/chan.try_send: avoid blocking if no reader is available
This changes the semantics of try_send to be consistently non-blocking.

That is, if the buffered is full OR there are no readers it returns
false.

The previous behaviour was such that it would block in the latter case
of no reader, and it would wait for a reader. That is problematic
because it produces inconsistent behaviour between buffered and
unbuffered channels which is astonishing and adds complexity to the
caller.

To illustrate the problem with the old behaviour, consider the
try_select operation: if a send-channel happens to be unbuffered the
try_select (which wants to never block) can now block, that unbuffered
send channel is selected (at random) and there is no reader on the other
side. Thus we have unpredictable blocking behaviour, which breaks the
guarantee that try_select never blocks.

If you want a blocking send you can just call "send" (the blocking
variant).

In addition, there is some reader/writer math done inside
can_{send,recv} such that they only report true if there is sufficient
reader/writer capacity. If there is contention we need to ensure that
each reader is paired to exactly one writer.

Consider try_send: if there is a single reader we can send. If there is
a single reader and a single writer, then we cannot send, as that reader
will be paired with the existing writer. Therefore can_send is only true
if there are more readers than writers at the time of check.

NOTE: The original tests don't need to use wait-looping with thread.yield()
or heuristic sleep. Instead we can just use blocking channel operations
rather than non-blocking operations.
2025-06-12 17:35:48 -03:00
Jack Mordaunt
52d38f1788 test/core/sync/chan: serialize try_select tests
These tests will race access to __global_context_for_test, which can
cause the test suite to flake. Even though only a single test actually
references the variable, the logic in try_select consumes it.
2025-06-12 17:15:37 -03:00
Jack Mordaunt
3c3fd6e580 tests/core/sync/chan: move global state into test
While this state is not actually needed by more than one test, we can
just make it a static variable.
2025-06-12 16:14:52 -03:00
Jack Mordaunt
4d7c182f7d tests/core/sync/chan: test harness for chan.try_select_raw
This test harness ensures consistent non-blocking semantics and
validates that we have solved the toctou condition.

The __global_context_for_test is a bit of a hack to fuse together the
test supplied proc and the executing logic in packaage chan.
2025-06-12 16:14:52 -03:00
pkova
aa25714d43 Remove comment from core sync tests now that they're fixed 2024-09-17 02:11:41 +03:00
pkova
fff99c726e Fix core sync test deadlock on darwin 2024-09-17 01:52:51 +03:00
Feoramund
7f7cfebc91 Add tests for core:sync and core:sync/chan 2024-09-11 08:39:38 -04:00