You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
IOSim: timeout can return while its assasin threas is still ailve #271
Describe the bug
timeout can return while its assassin thread is still alive, so runSimStrictShutdown fails with FailureSloppyShutdown
In IOSim, timeout forks an assassin thread when its timer fires. If the action finishes at the same moment the timeout expires, timeout can return while the assassin is still alive. Any simulation run with runSimStrictShutdown then fails with FailureSloppyShutdown.
The result itself is correct: the leftover assassin never throws. But in simulations the action often finishes exactly at the deadline, so this makes strict shutdown unusable for code that uses timeout. GHC has no equivalent of the strict-shutdown check, so the problem only shows up in the simulator.
Case c: the assassin hasn't run yet. At 1s the delay timer wakes the main thread, and the timeout timer forks the assassin, which is queued behind main.
Main leaves the TimeoutFrame, takes the lock and finishes. The assassin is still in the run queue, so TraceMainReturn lists it as running. A yield
doesn't help: the assassin's tryPutTMVar commit yields too, so main finishes before the assassin exits.
Case d: the assassin holds the lock but hasn't thrown yet. The frame exit killThreads it. Killing an unmasked thread doesn't block; it only replaces the
thread's next action with Throw ThreadKilled. So the assassin isn't dead yet when main finishes. One yield lets it terminate, which is why d′ passes.
1. Keep the assassin, but finish it from the scheduler.
When the TimeoutFrame is left, the scheduler removes the assassin directly, in ST, instead of handing the timed thread a killThread action:
If the timer fired but the assassin hasn't run yet, remove it from threads and the run queue, i.e. un-fork it. This needs a map from TimeoutId to the assassin's thread id.
If the assassin holds the lock but hasn't thrown, it is unmasked and its only remaining step is the throwTo, which we want to suppress anyway. Terminate it directly.
This is a small, local change in IOSim. In IOSimPOR, removing a thread interacts with clocks and race tracking, so it needs more care.
2. Don't fork an assassin at all.
When the timer fires, the scheduler delivers the TimeoutException itself, exactly as ThrowTo would: it raises it if the thread is interruptible and queues it otherwise. To keep "the action wins a tie", the delivery waits until every thread runnable at that moment has run (a sentinel at the back of the run queue). Leaving the frame drops any undelivered or queued exception for it.
This removes the problem entirely and also simplifies the code: there is no lock and no extra threads. The costs:
A queued exception has no real thrower thread, so it needs a placeholder.
Traces change: EventThrowTo from a "timeout timer" pseudo-thread instead of EventThreadForked, and TVarIds shift because there is no lock TVar.
An action that yields after the deadline before finishing is now interrupted.
In IOSimPOR, the race between the timeout and the action would no longer be a step that can be explored. It isn't explored today anyway.
result: Short, paste-ready descriptions of the two fixes for the IOSim timeout issue are above: finish the assassin from the scheduler, or drop the assassin and deliver the exception from the scheduler.
Activity
coot commentedon Nov 4, 2026
This is a discovered leftover not fixed in #269.
coot commentedon Nov 4, 2026
Possible solutions
1. Keep the assassin, but finish it from the scheduler.
When the
TimeoutFrameis left, the scheduler removes the assassin directly, inST, instead of handing the timed thread akillThreadaction:threadsand the run queue, i.e. un-fork it. This needs a map fromTimeoutIdto the assassin's thread id.throwTo, which we want to suppress anyway. Terminate it directly.This is a small, local change in
IOSim. InIOSimPOR, removing a thread interacts with clocks and race tracking, so it needs more care.2. Don't fork an assassin at all.
When the timer fires, the scheduler delivers the
TimeoutExceptionitself, exactly asThrowTowould: it raises it if the thread is interruptible and queues it otherwise. To keep "the action wins a tie", the delivery waits until every thread runnable at that moment has run (a sentinel at the back of the run queue). Leaving the frame drops any undelivered or queued exception for it.This removes the problem entirely and also simplifies the code: there is no lock and no extra threads. The costs:
EventThrowTofrom a "timeout timer" pseudo-thread instead ofEventThreadForked, andTVarIds shift because there is no lockTVar.IOSimPOR, the race between the timeout and the action would no longer be a step that can be explored. It isn't explored today anyway.result: Short, paste-ready descriptions of the two fixes for the IOSim timeout issue are above: finish the assassin from the scheduler, or drop the assassin and deliver the exception from the scheduler.
coot commentedon Nov 4, 2026
Option 2 is explored in a
coot/timeout-no-assassinbranch.