Skip to content

Commit b19e023

Browse files
jasnelltargos
authored andcommitted
doc: clarify synchronous blocking of Worker stdio
Fixes: #25630 Signed-off-by: James M Snell <[email protected]> PR-URL: #38658 Reviewed-By: Anna Henningsen <[email protected]> Reviewed-By: Antoine du Hamel <[email protected]>
1 parent bcf539c commit b19e023

File tree

1 file changed

+39
-0
lines changed

1 file changed

+39
-0
lines changed

doc/api/worker_threads.md

+39
Original file line numberDiff line numberDiff line change
@@ -968,6 +968,45 @@ active handle in the event system. If the worker is already `unref()`ed calling
968968

969969
## Notes
970970

971+
### Synchronous blocking of stdio
972+
973+
`Worker`s utilize message passing via {MessagePort} to implement interactions
974+
with `stdio`. This means that `stdio` output originating from a `Worker` can
975+
get blocked by synchronous code on the receiving end that is blocking the
976+
Node.js event loop.
977+
978+
```mjs
979+
import {
980+
Worker,
981+
isMainThread,
982+
} from 'worker_threads';
983+
984+
if (isMainThread) {
985+
new Worker(new URL(import.meta.url));
986+
for (let n = 0; n < 1e10; n++) {}
987+
} else {
988+
// This output will be blocked by the for loop in the main thread.
989+
console.log('foo');
990+
}
991+
```
992+
993+
```cjs
994+
'use strict';
995+
996+
const {
997+
Worker,
998+
isMainThread,
999+
} = require('worker_threads');
1000+
1001+
if (isMainThread) {
1002+
new Worker(__filename);
1003+
for (let n = 0; n < 1e10; n++) {}
1004+
} else {
1005+
// This output will be blocked by the for loop in the main thread.
1006+
console.log('foo');
1007+
}
1008+
```
1009+
9711010
### Launching worker threads from preload scripts
9721011
9731012
Take care when launching worker threads from preload scripts (scripts loaded

0 commit comments

Comments
 (0)