Skip to content

Commit 43eb49f

Browse files
committed
doc: explain how green threads avoid and break stalls
Recommend waiting without blocking, for example with a lock built on Atomics.waitAsync(), which can't stall the pool, and describe which deadlocks setStallPolicy() detects. Deadlocks that more pool threads wouldn't help with, such as two green threads that each hold a lock that the other one waits for, are not detected, and are stopped by maxTaskCpuTimeMs instead. Assisted-by: Claude Opus 5.5
1 parent a42c05a commit 43eb49f

1 file changed

Lines changed: 70 additions & 10 deletions

File tree

‎doc/api/green_threads.md‎

Lines changed: 70 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -335,15 +335,60 @@ initial script, and their callbacks run on the same OS thread as that task.
335335

336336
### Stalls
337337

338-
Green threads that wait for each other without returning, for example by
339-
spinning on a `SharedArrayBuffer` until another green thread changes it, can
340-
stall: if they keep all pool threads busy, the green thread they wait for
341-
never runs. A green thread that keeps a pool thread busy also delays the
342-
timers of other green threads that run on that thread.
338+
A green thread that waits for another one without returning, for example by
339+
spinning on a `SharedArrayBuffer` until the other one changes it, keeps a pool
340+
thread busy while it waits. If such green threads keep all pool threads busy,
341+
the green thread they wait for never runs, and they wait forever. A green
342+
thread that keeps a pool thread busy also delays the timers of other green
343+
threads that run on that thread. Green threads stall the pool this way even
344+
when the same code would work with [`Worker`][]s, since each worker has an OS
345+
thread of its own.
346+
347+
#### Waiting without blocking
348+
349+
Green threads that wait for each other should return while they wait, so that
350+
their pool threads can run other green threads, including the one they wait
351+
for. Messages, [RPC][], and [`Atomics.waitAsync()`][] all do this.
352+
`Atomics.wait()` throws in green threads, since it would block the pool thread.
353+
A lock built on `Atomics.waitAsync()` can't stall the pool, however many green
354+
threads wait for it:
343355

344-
[`greenThreads.setStallPolicy()`][] detects such stalls, and breaks them by
345-
adding pool threads, or by terminating the green threads that cause them,
346-
either automatically or as decided by the host:
356+
```cjs
357+
const { GreenThread } = require('node:green_threads');
358+
359+
// A lock that green threads wait for without keeping a pool thread busy.
360+
const source = `
361+
async function acquire(lock) {
362+
while (Atomics.compareExchange(lock, 0, 0, 1) !== 0) {
363+
await Atomics.waitAsync(lock, 0, 1).value;
364+
}
365+
}
366+
function release(lock) {
367+
Atomics.store(lock, 0, 0);
368+
Atomics.notify(lock, 0, 1);
369+
}
370+
onmessage = async ({ data: lock }) => {
371+
await acquire(lock);
372+
// ... use what the lock protects ...
373+
release(lock);
374+
postMessage('done');
375+
};
376+
`;
377+
378+
const lock = new Int32Array(new SharedArrayBuffer(4));
379+
for (let i = 0; i < 100; i++) {
380+
const greenThread = new GreenThread(source);
381+
greenThread.on('message', () => greenThread.terminate());
382+
greenThread.postMessage(lock);
383+
}
384+
```
385+
386+
#### Breaking stalls
387+
388+
For code that may block anyway, such as code that the host doesn't control,
389+
[`greenThreads.setStallPolicy()`][] detects stalls, and breaks them by adding
390+
pool threads, or by terminating the green threads that cause them, either
391+
automatically or as decided by the host:
347392

348393
```cjs
349394
const { setStallPolicy } = require('node:green_threads');
@@ -358,6 +403,17 @@ setStallPolicy({
358403
});
359404
```
360405

406+
A stall is only detected while some work waits for a pool thread: a green
407+
thread that hasn't been able to run, or a timer behind a green thread that
408+
keeps its pool thread busy. Green threads that wait for each other in a way
409+
that more pool threads wouldn't help with, such as two that each hold a lock
410+
the other one waits for, or one that waits for a message that it can only
411+
handle once it stops waiting, would wait forever with workers too. The pool
412+
looks the same as when green threads run long computations, so this isn't
413+
detected. A green thread that spins while it waits uses CPU time, so
414+
`maxTaskCpuTimeMs` stops it, whatever it waits for. See
415+
[CPU time limits][].
416+
361417
## Resource limits
362418

363419
Each green thread can have its own CPU time and memory limits, which are set
@@ -472,7 +528,8 @@ that have been reported already.
472528

473529
Since a policy applies to all green threads in the process, including those
474530
created by other threads, `timeoutMs` should be longer than any task is
475-
expected to run.
531+
expected to run. Deadlocks that don't depend on pool threads aren't detected:
532+
see [Breaking stalls][].
476533

477534
## `greenThreads.setUnresponsiveTimeouts(timeouts)`
478535

@@ -912,7 +969,7 @@ uncaught `RangeError` in it.
912969

913970
`SharedArrayBuffer`s are shared rather than copied. `Atomics.wait()` throws in
914971
green threads, since it would block the pool thread, and with it other green
915-
threads.
972+
threads. See [Waiting without blocking][].
916973

917974
### `greenThread.ref()`
918975

@@ -957,6 +1014,7 @@ added: REPLACEME
9571014

9581015
Calls [`greenThread.terminate()`][].
9591016

1017+
[Breaking stalls]: #breaking-stalls
9601018
[Broadcast channels]: #broadcast-channels
9611019
[CPU time limits]: #cpu-time-limits
9621020
[ES modules]: #es-modules
@@ -968,6 +1026,7 @@ Calls [`greenThread.terminate()`][].
9681026
[RPC]: #rpc
9691027
[Resource limits]: #resource-limits
9701028
[Stalls]: #stalls
1029+
[Waiting without blocking]: #waiting-without-blocking
9711030
[`'error'`]: #event-error
9721031
[`'exit'`]: #event-exit
9731032
[`'message'`]: #event-message
@@ -976,6 +1035,7 @@ Calls [`greenThread.terminate()`][].
9761035
[`--experimental-green-threads`]: cli.md#--experimental-green-threads
9771036
[`AbortController`]: globals.md#class-abortcontroller
9781037
[`AbortSignal`]: globals.md#class-abortsignal
1038+
[`Atomics.waitAsync()`]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Atomics/waitAsync
9791039
[`BroadcastChannel`]: worker_threads.md#class-broadcastchannel-extends-eventtarget
9801040
[`DOMException`]: globals.md#class-domexception
9811041
[`ERR_GREEN_THREAD_BROADCAST_SCOPE`]: errors.md#err_green_thread_broadcast_scope

0 commit comments

Comments
 (0)