Repository navigation
Conversation
When handling `Throw` after successful unwinding of the control stack, we run cleanups of unwound TimeoutFrames. We must wrap them in `uninterruptibleMask_` but only if it matters, so they are uninterrupted and pending async exceptions are delivered when `uninterruptibleMask_` unmasks. The latter makes sure that continuation after a timeout is not run before delivery of a pending async exception (the counterexample in the tests catches that case). We do the wrapping in `uninterruptibleMask_` conditionally, for performance reasons and to avoid extra steps in IOSimPOR.
A thread blocked in `throwTo` (its target has async exceptions masked) can be interrupted: by another thread's `throwTo` (e.g. `killThread`), or by one of its own pending async exceptions, since `throwTo` is an interruptible point. Its exception stayed in the target's `threadThrowTo` and was raised when the target unmasked: the target received an exception from a `throwTo` which had already been interrupted, and the scheduler woke the original thrower, even out of an unrelated block. In GHC the `throwTo` is cancelled. `ThrowTo` has two cases: 1. blocking case (`willBlock = True`): if the thread is not `MaskedUninterruptible` and there are pending async exceptions one of them is raised (since `throwTo` is a blocking call in this case). Only otherwise we can add the async exception from `throwTo` to the target thread. 2. non-blocking case (`willBlock = False`): the exception is thrown to the target, and if the target is blocked in `throwTo`, its exception is dropped from the pending async exceptions of its own target (exactly one thread, possibly the current one). The IOSimPOR fix follows the same logic, with a few differences: * ad 1. the step keeps its `throwToEffect`: whether `e` is thrown depends on the masking state of the target. * ad 2. the exception is dropped only from the thread which holds it in its own `threadThrowTo`. IOSim applies `dropThrowToFrom tid'` to all threads, which is a no-op for the others, IOSimPOR applies `dropThrowToFrom tid'` and modifies effects (see next point). * ad 2. the current step records a `throwToEffect` for these threads (there might be just one such thread), since its pending async exceptions change, so that IOSimPOR can find races between this step and its steps. * ad 2. target thread's vector clock is joined with the current one, so IOSimPOR knows its later steps depend on this step, and doesn't move them before it in a modified schedule (`propExploration` failed with "Thread ... not runnable" without it). `unit_async_10` and `unit_async_11` relied on the old behaviour: child 1 now survives, as in GHC.
This patch fixes a simple bug where `throwTo tid e1 >> throwTo tid e2` delivers only `e2`, while `e1` is lost when the first `throwTo` is non-blocking. To avoid subsequent `throwTo` overwrite the delivered async exception we make the target thread interruptible masked. Then the latter `throwTo` will land in the `willBlock = True` branch, and the exception will not be overwritten. The thread masking state is restored when we unwind the control stack to find an exception handler - thus any exception handler will run in the right masking state. GHC is using the same mechanism to avoid this bug. `prop_stacked_timeouts` is adjusted, it now agrees with how GHC does things. On a tie, the outer timeout now wins.
This eliminates a bug where timeout assassin threads were not marked as racy; `setRacyThread` is removed.
since `io-sim-por: childThreadId take racy flag` now explores more races, the `stacked timeout` test needed to be updated. IOSim test was only refactored to mach the same style as the IOSimPOR test.
IOSim already does that. This should be done, because `StartTimeout` creates a timeout lock.
A `throwTo` to a masked thread, which is blocked in STM, races with a write which wakes the thread up: if the exception is thrown first, it interrupts the transaction; if the write is first, the exception is pending until the thread unmasks, after the transaction committed. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
As in `IOSim` when cancelling a timeout we should only unblock threads that were blocked on STM, not any other threads. `NewTimeout` creates a `TVar s Bool` to which `CaneclTimeout` writes, thus they might be unblocked now. There's no reason to unblock threads that are blocked for other reasons.
`conjoinSpeculate` is a custom version of `conjoin` from `QuickCheck`. This patch fixes a performance bug in it. When a conjunct failed (`ok result == Just False`), we returned the conjunct itself rather than its result. Since `conj` is written in the `Rose` monad, the bind ran it again, unlike `QuickCheck`'s `conjoin`, which returns the already reduced rose. The same happened when a conjunct was discarded (`ok result == Nothing`): we returned `rest`, the conjunction of the remaining conjuncts, instead of its result, so `rest` was evaluated twice. `exploreSimTrace` nests two conjunctions (`.&&|` and `conjoinPar`) per level of race reversals, so a failure at depth L was re-evaluated in the order of `4^L` times; each discarded conjunct on the way doubled it again.
`conjoinPar` wasn't effective in starting sparks, since the property in `exploreSimTrace` was wrapping a property inside `counterexample` and `tabulate` which add `IORose` on top of the property. We need to run the `IO` part (using `unsafePerformIO`) and then use `par` to evaluate the simulation result in parallel using sparks. This patch gives 2x elapsed time improvement on one o `cardano-diffusion` tests, with `MUT time / elapsed time` ratio raised from around 1 to almost 12.
Since `QuickCheck-2.15`, `res { abort = False }` is not needed - since
`2.15` that's the default, so we can drop `againNoCatch`.
We can use `.&&.` instead of our custom `.&&|` since now `conjoinPar`
can now evaluate `IORose`, and `.&&.` is already sequential.
runSimTraceWith takes SimOptions modifiers. A Preemption oracle, given a thread and its yield count, may preempt the thread at a yield point (STM commit, yield, timeout setup) for a given time. preemptRandom draws preemptions from a seed, and preemptionsOf with preemptFromMap replays and shrinks them. runSimTrace is unchanged. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
karknu
force-pushed
the
karknu/preempt
branch
from
November 17, 2026 14:39
c03b62c to
c586fa4
Compare
coot
force-pushed
the
coot/masked-timeout-fix
branch
2 times, most recently
from
November 18, 2026 09:52
071d1a8 to
91d156b
Compare
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Add a preemption oracle to IOSim
runSimTraceWith takes SimOptions modifiers. A Preemption oracle, given
a thread and its yield count, may preempt the thread at a yield point
(STM commit, yield, timeout setup) for a given time. preemptRandom draws
preemptions from a seed, and preemptionsOf with preemptFromMap replays
and shrinks them. runSimTrace is unchanged.