Skip to content

throwTo: do not return until the exception is raised in the target - #6

Closed
dmjio wants to merge 4 commits into
masterfrom
throwto-sync
Closed

dmjio wants to merge 4 commits into
masterfrom
throwto-sync

Conversation

@dmjio

@dmjio dmjio commented Oct 4, 2026 •

Copy link
Copy Markdown
Member

Makes throwTo, and so killThread, wait until the exception has been raised in the target thread, as GHC does. One commit on top of upstream augustss/MicroHs master (6a961d30), meant to be sent upstream as is.

The problem

GHC documents that throwTo does not return until the exception has been raised in the target thread. MicroHs put the exception in the target's exception MVar and returned at once. The target stayed runnable with the exception pending, so after killThread t returned, t could still run for a while, and take values from MVars or write shared state, before it was interrupted.

This was found in miso. Its integration tests start and tear down many apps in one page. Teardown kills the app's scheduler thread, and the next app posts its first redraw notification right after. With the old throwTo the killed scheduler could still take that notification, so the next app never redrew: two tests were flaky (miso #1663). The same pattern is in miso's hot reload, and in component unmounting, which kills the component's subscription threads and then continues the teardown.

Code written for GHC relies on this guarantee in general: killing a worker and then reusing what it held, timeout, cancelling an async.

The change

All of it is in the scheduler in src/runtime/eval.c, about 50 lines of code:

  • throwTo waits. After handing over the exception it puts the calling thread on the read queue of the target's exception MVar, and records the target in a new field mt_throwto.
  • wake_throwto releases it when the target takes the exception (check_thrown), finishes, or dies. When throwTo runs again it sees mt_throwto and returns.
  • The wait is interruptible. thread_intr already searches the read queues of all MVars, so an exception thrown to a thread that is waiting in throwTo is delivered as before.
  • Throwing to yourself does not wait. The exception is raised at the next interruption point, as before.
  • A killed thread that is already finished or dead is not waited for, so killing a dead thread twice is still a no-op.
  • The GC marks mt_throwto.

Keeping killThread in a loop linear

Waiting for the target made killing many runnable threads quadratic. The killer parked at the tail of the run queue, the target took the exception only after every thread ahead of it had run a slice, and then the woken killer waited again for a slice of every other thread. So:

  • throwTo moves a runnable target to the head of the run queue before it waits;
  • wake_throwto puts the woken thrower right after the target (or at the head if the target is gone).

Each kill is then a couple of context switches.

Tests

tests/ThrowToSync.hs (new, in test):

check before after
kill a runnable thread, then look at what its handler recorded not raised thread killed
kill a thread blocked on an MVar not raised thread killed
a killed thread does not take a value from a shared MVar passes passes
killing a dead thread twice is a no-op passes passes
throwTo to ourselves does not deadlock passes passes
kill a thread in uninterruptibleMask: killThread waits until it leaves the mask not raised thread killed

make runtestmhs and make runtest pass, on macOS x86-64.

Cost

killThread now does a couple of context switches instead of returning at once. Killing n threads that spin in yield (3 runs each, macOS x86-64):

threads before after
1,000 0.02 s 0.40 to 0.42 s
5,000 0.66 to 0.67 s 1.51 to 1.52 s
20,000 4.35 to 4.48 s 4.94 to 4.99 s

It grows linearly. Without the run queue change above it was quadratic: on the fork, 5,000 threads took 35 s and 20,000 over 300 s.

System.Timeout.timeout kills its timer thread on every call, so it is the most common place where a program kills threads often. n calls of timeout 1000000 (return x), and killing n threads blocked on an MVar (3 runs each):

benchmark before after
timeout, 1,000 calls 0.42 to 0.46 s 0.43 s
timeout, 10,000 calls 3.07 to 3.52 s 4.27 to 5.03 s
kill 1,000 blocked threads 0.02 to 0.03 s 0.50 s
kill 10,000 blocked threads 2.37 to 2.79 s 3.75 to 4.04 s

So timeout is unchanged at 1,000 calls and about 35% slower at 10,000, roughly 0.1 to 0.15 ms more per call.

The "before" kill timings flatter the old throwTo. When killThread returns without waiting, these benchmarks print and exit before the killed threads have run at all, so that work is skipped, not done faster. A program that keeps running pays most of it later.

Neither column grows linearly for timeout (10 times the calls take 7 to 10 times as long, before and after). This change does not cause that. A possible cause is thread_intr, which looks through all MVars to find the one a thread is blocked on (see the XXX comment there), but I have not confirmed it.

Like GHC's, throwTo now blocks for as long as the target is in an uninterruptible mask.

This trades a small, measurable cost in killThread for the guarantee that a killed thread has stopped what it was doing when killThread returns. A program can't work around the missing guarantee, while the cost only shows when killing thousands of threads.

Not included

No generated files are changed: nothing in the compiler or the libraries changed, and make links the new runtime. generated/mhseval.js and web-mhs/ contain the runtime compiled with emscripten and are stale until the next regeneration.

Update: two fixes from review

A review of this PR found two problems, fixed in the second commit (ThrowToSync has a test for each):

  • A second throwTo could hang. When two threads throw to the same thread, the second waits for the target's exception slot, which holds the first exception. If the target finished or died without taking it, nothing released the second thrower, and it waited forever (this also happened before this PR). Now, when a thread finishes or dies, every thread waiting to throw to it is released (release_throwto), runs throwTo again, finds the target gone, and returns.
  • An interrupted throwTo still delivered its exception. If a thread waiting in throwTo was itself interrupted (e.g. killed), its exception stayed in the target's slot and was raised there later. In GHC an interrupted throwTo has no effect. Now the interrupted thread takes its exception back, if the target has not taken it yet (revoke_throwto).

Checked against GHC 9.14.1 with small programs: killing a thread whose thrower is interrupted (the target now finishes normally, as in GHC), and two threads killing each other (only one dies, as GHC guarantees). make runtestmhs passes.

GHC's throwTo (and so killThread) does not return until the exception
has been raised in the target thread.  MicroHs handed the exception
over and returned at once, so the target stayed runnable with the
exception pending and could still run, and consume shared state, before
it was interrupted.  For example, miso's tests killed the scheduler
thread of one app, and the old scheduler could then take the redraw
notification of the next app.

Now throwTo waits on the read queue of the target's exception MVar
until the target takes the exception (check_thrown), or finishes or
dies.  The wait is itself interruptible, since thread_intr searches
that queue.  A thread that throws to itself does not wait.

Since throwTo waits for the target, killing a runnable thread could
park the killer twice at the tail of the run queue, which made a loop
of killThread quadratic.  So throwTo moves a runnable target to the
head of the run queue, and the woken thrower goes right after it.

tests/ThrowToSync.hs: killing a runnable, a blocked and a masked
thread, killing a dead thread, and throwTo to ourselves.
@dmjio dmjio changed the title throwTo: do not return until the exception is raised in the target throwTo: do not return until the exception is raised in the target Oct 4, 2026
@dmjio dmjio changed the title throwTo: do not return until the exception is raised in the target throwTo: do not return until the exception is raised in the target Oct 4, 2026
@dmjio

dmjio commented Oct 5, 2026

Copy link
Copy Markdown
Member Author

augustss#534

dmjio added 3 commits October 5, 2026 01:13
…owTo

Two problems found in review:

A second throwTo to a thread waits for the thread's exception slot,
which the first throwTo has filled.  If the thread finished or died
without taking the first exception, nothing released the second
thrower, and it waited forever.  Now, when a thread finishes or dies,
every thread waiting to throw to it is released (release_throwto); each
runs throwTo again, finds the thread gone, and returns.

A throwTo that was interrupted while it waited still delivered its
exception, because the exception stayed in the target's slot.  In GHC
an interrupted throwTo has no effect.  Now a thread that takes an
exception while it waits in throwTo takes its own exception back from
the target, if the target has not taken it (revoke_throwto).

ThrowToSync tests both.
Use them for the run queue insertions in wake_throwto and throwto,
instead of doing the list manipulation inline.
@dmjio dmjio closed this Oct 5, 2026
@dmjio
dmjio deleted the throwto-sync branch October 5, 2026 15:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant