Skip to content

Commit 069ba7e

Browse files
committed
test(timer): fix flaky StopTokenCancelsOne on IOCP
The test raced a 10ms delay timer against the 500ms shared timer t to drive cancellation. Under a runtime stall both expire together and t completes w1 with success before the delay-driven request_stop() runs, so the cancellation assertion fails intermittently in CI. Drop the delay timer; call request_stop() from a posted task instead. run_async queues it FIFO, so both waiters register on t before the cancel runs — deterministic, no wall-clock margin.
1 parent 22d9d0e commit 069ba7e

1 file changed

Lines changed: 8 additions & 3 deletions

File tree

test/unit/timer.cpp

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -633,14 +633,12 @@ struct timer_test
633633
{
634634
io_context ioc(Backend);
635635
timer t(ioc);
636-
timer delay(ioc);
637636

638637
std::stop_source stop_src;
639638
bool w1 = false, w2 = false;
640639
std::error_code ec1, ec2;
641640

642641
t.expires_after(std::chrono::milliseconds(500));
643-
delay.expires_after(std::chrono::milliseconds(10));
644642

645643
// w1 has a stop_token — will be cancelled individually
646644
auto wait_task = [&]() -> capy::task<> {
@@ -656,9 +654,16 @@ struct timer_test
656654
w2 = true;
657655
};
658656

657+
// Cancel w1 once both waiters are registered. Posting through the
658+
// executor (rather than racing a wall-clock delay timer against t's
659+
// expiry) makes the ordering deterministic: run_async queues the
660+
// tasks, and the IOCP/reactor loop drains posted completions in FIFO
661+
// order, so both wait tasks run and suspend on t before this task
662+
// calls request_stop(). t's deadline is far enough out that w2 then
663+
// completes via a normal expiry afterwards.
659664
auto cancel_one = [&]() -> capy::task<> {
660-
(void)co_await delay.wait();
661665
stop_src.request_stop();
666+
co_return;
662667
};
663668

664669
capy::run_async(ioc.get_executor(), stop_src.get_token())(wait_task());

0 commit comments

Comments
 (0)