Skip to content

IOSim: timeout can return while its assasin threas is still ailve #271

Open

Description

@coot

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.

To Reproduce

Each scenario run with runSimStrictShutdown:

Scenario Code Result
a. timer never fires timeout 1 (threadDelay 0.5) Just ()
b. timeout wins timeout 1 (threadDelay 2) Nothing
c. tie, action finishes first timeout 1 (threadDelay 1) FailureSloppyShutdown
c′. c, then yield timeout 1 (threadDelay 1) <* yield FailureSloppyShutdown
d. tie, action yields before finishing timeout 1 (threadDelay 1 >> atomically (pure ())) FailureSloppyShutdown
d′. d, then yield timeout 1 (threadDelay 1 >> atomically (pure ())) <* yield Just ()
  • 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.

Activity

  1. added
    bug
    on Nov 4, 2026
  2. coot commented on Nov 4, 2026

    @coot
    CollaboratorAuthor

    This is a discovered leftover not fixed in #269.

  3. coot commented on Nov 4, 2026

    @coot
    CollaboratorAuthor

    Possible solutions

    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.

  4. coot commented on Nov 4, 2026

    @coot
    CollaboratorAuthor

    Option 2 is explored in a coot/timeout-no-assassin branch.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions