Repository navigation
Conversation
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.
throwTo: do not return until the exception is raised in the target
throwTo: do not return until the exception is raised in the target
Member
Author
…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.
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.
Makes
throwTo, and sokillThread, wait until the exception has been raised in the target thread, as GHC does. One commit on top of upstreamaugustss/MicroHsmaster (6a961d30), meant to be sent upstream as is.The problem
GHC documents that
throwTodoes 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 afterkillThread treturned,tcould 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
throwTothe 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 anasync.The change
All of it is in the scheduler in
src/runtime/eval.c, about 50 lines of code:throwTowaits. 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 fieldmt_throwto.wake_throwtoreleases it when the target takes the exception (check_thrown), finishes, or dies. WhenthrowToruns again it seesmt_throwtoand returns.thread_intralready searches the read queues of all MVars, so an exception thrown to a thread that is waiting inthrowTois delivered as before.mt_throwto.Keeping
killThreadin a loop linearWaiting 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:
throwTomoves a runnable target to the head of the run queue before it waits;wake_throwtoputs 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, intest):throwToto ourselves does not deadlockuninterruptibleMask:killThreadwaits until it leaves the maskmake runtestmhsandmake runtestpass, on macOS x86-64.Cost
killThreadnow does a couple of context switches instead of returning at once. Killing n threads that spin inyield(3 runs each, macOS x86-64):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.timeoutkills its timer thread on every call, so it is the most common place where a program kills threads often.ncalls oftimeout 1000000 (return x), and killingnthreads blocked on an MVar (3 runs each):timeout, 1,000 callstimeout, 10,000 callsSo
timeoutis 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. WhenkillThreadreturns 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 isthread_intr, which looks through all MVars to find the one a thread is blocked on (see theXXXcomment there), but I have not confirmed it.Like GHC's,
throwTonow blocks for as long as the target is in an uninterruptible mask.This trades a small, measurable cost in
killThreadfor the guarantee that a killed thread has stopped what it was doing whenkillThreadreturns. 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
makelinks the new runtime.generated/mhseval.jsandweb-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 (
ThrowToSynchas a test for each):throwTocould 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), runsthrowToagain, finds the target gone, and returns.throwTostill delivered its exception. If a thread waiting inthrowTowas itself interrupted (e.g. killed), its exception stayed in the target's slot and was raised there later. In GHC an interruptedthrowTohas 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 runtestmhspasses.