Repository navigation
Conversation
…ends When the reactor's queue entry was the only thing queued, a zero-timeout call returned without running the reactor. A read parked on a socket that already had data never completed through poll(), however often it was called, and a timer past its deadline never fired; the reactor ran only if some other handler happened to be queued. A zero timeout now runs a non-blocking reactor pass in that case too. The pass that ends a poll is the one that queues nothing, so poll() still returns once no work is ready. The pass runs without the lock, so another thread may find the queue empty and go to sleep meanwhile; an empty pass wakes one before returning, so the reactor isn't left with nobody to run it. The epoll timerfd is armed relative to when it was set, so it can fire a little after a deadline. A pass that doesn't block now also checks the clock, so a timer that is already due fires on that pass.
run_for() and run_one_for() hold the reactor for a bounded pass, then return without waking anyone. A thread that called run() meanwhile found the queue empty and went to sleep, so once the bounded call left, nobody ran the reactor: ready I/O and expired timers waited until some other thread happened to post work. A bounded pass now wakes one sleeping thread before returning, as an empty poll does, so the reactor is taken over. That makes a bounded call more likely to find the reactor held by another thread, and its wait for work then started over each time it timed out, never giving the caller a chance to see its deadline pass. A bounded call now blocks at most once, in its wait or in a reactor pass, and then finishes as a poll does. A wake that hands it the reactor no longer starts a second full block, and a handler its pass found runs in that call instead of the next one. The io_uring scheduler's bounded wait blocks once in the same way.
Work a handler posts goes to its thread's private queue and reaches the shared queue when the handler returns. A poll(), poll_one() or run_one() called from inside that handler pushed a fresh frame and never saw it: poll() returned without running it, and run_one() blocked with the work sitting in the frame below. Each run and poll entry now moves the enclosing frame's private queue and its work count to the shared queue first, so the nested call runs what was posted before it.
run_until() and run_one_until(), and run_for() and run_one_for() through them, always made one scheduler pass, even when the deadline had already passed. A late call therefore acted as poll() or poll_one(): it ran whatever was ready, and stopped the context if no work was left. A deadline that has passed now returns 0 straight away, leaving queued work and the context's state alone. Tests that relied on that one pass now get there another way: those expecting a bounded call to run work or stop the context use a long bound, which returns as soon as it does, and those that only start coroutines before a bounded wait start them with poll(). Preemption past a short deadline can no longer skip the step.
These schedulers marked a handler's work finished only after the handler returned. A handler that threw left its work counted, so the context never again saw it had nothing to do: it never stopped by itself, and a later run() blocked with no work to wait for. On IOCP a signal handler resumed inline left the signal service's own work counted the same way. The work is now finished as the handler exits, however it exits.
IOCP learns of a stop only when a thread reaches the stop's wakeup in the completion port. Queued work can still run after stop() is called. The other backends check for the stop before running anything further.
run(), run_one(), poll(), poll_one() and the bounded forms are each called with no work, with posted work, with work a handler posts, with a parked read that has data and one that has none, with a timer past its deadline and one far from it, on a stopped context, with stop() called from a handler, and with a handler that throws. Each check compares one line naming the function and what it saw, so a failure on any backend shows the whole result. A situation a backend can't make deterministic is skipped there: only the reactors leave readiness for the test to wait on, and IOCP reports timer expiry from a thread of its own. On IOCP, work queued ahead of a stop still runs, so the stop checks expect that there.
|
No actionable comments were generated in the recent review. Walkthrough
Merge Risk:
|
|
An automated preview of the documentation is available at https://423.corosio.prtest3.cppalliance.org/index.html If more commits are pushed to the pull request, the docs will rebuild at the same URL. 2026-11-16 20:41:38 UTC |
[Medium impact] The PR appears safe to merge; no actionable issues were found.
|
|
GCOVR code coverage report https://423.corosio.prtest3.cppalliance.org/gcovr/index.html Build time: 2026-11-16 20:58:38 UTC |
Benchmark report
linux — full results (226 benchmarks, 5 iterations, 1.5s each)accept_churn
fan_out
http_server
local_socket_latency
local_socket_throughput
socket_latency
socket_throughput
windows — full results (77 benchmarks, 5 iterations, 1.5s each)accept_churn
fan_out
http_server
socket_latency
socket_throughput
macos — full results (113 benchmarks, 5 iterations, 1.5s each)accept_churn
fan_out
http_server
local_socket_latency
local_socket_throughput
socket_latency
socket_throughput
Run & raw JSON artifacts · flag rule: |median Δ| > max(2%, 3×CV of the noisier side) · advisory only |
On epoll, select and kqueue,
poll()andpoll_one()returned 0 when only the reactor sentinel was queued. They never ran the reactor, so ready I/O and expired timers waited for a laterrun(). They now make one non-blocking pass. A pass that queues nothing wakes a sleeping peer before returning, so the sentinel is not stranded. On epoll, a non-blocking pass also checks the clock for due timers, because the timerfd can fire a little after the deadline.Fixing that turned up several related run-loop bugs, each in its own commit:
run_for,run_one_forand the_untilforms) now block at most once per call, then finish as a poll does. Before, a bounded pass handed back the reactor without waking a sleepingrun()thread. A follower woken to take the reactor blocked for a second full slice, sorun_for(200ms)could take 350 ms. A handler the pass found was also left for the next call. io_uring had the same overrun.poll()could not see, and a nestedrun_one()could hang. Each run function now takes over the enclosing frame's queue and work count on entry.run()never returned.stop(), because eachrun()sees the stop only when it reaches the stop's wakeup. This matches asio's IOCP backend and is now documented rather than changed.Behaviour change: a bounded run given a zero duration or a deadline that has already passed now returns 0 at once, without running work or stopping the context. Before, it ran one non-blocking pass, so
run_for(0ms)worked likepoll(). The new behaviour matches asio. Code that used a zero bound as a poll should callpoll().Tests:
test/unit/io_context.cppadds a matrix that runs all 8 run functions through 10 situations. These cover no work, posted and chained work, a parked read (idle and ready), expired and future timers, stopping with work queued, stopping inside a handler, and a throwing handler. Each row asserts every field of the result. Backend differences are gated explicitly, such as the IOCP stop semantics and io_uring consuming the data before a readiness check.bounded_run.cppcovers poll on ready reads and expired timers, and a bounded pass that runs what it found.Verified locally on Linux: each commit builds and passes the unit suites (220/0), and TSan is clean. A differential harness of 81 scenarios matches asio row for row on epoll, select and io_uring. Develop differed on 7 rows and hung on a nested
run_one. The cost is one extra non-blocking syscall perpoll()call.run()is unchanged.The timing races (the lost wakeup and the deadline overrun) have no unit tests, because they depend on timing. Scratch reproducers confirmed both fixes.
Resolves #418
Summary by CodeRabbit
stop()until eachrun()call receives the stop wakeup.