Skip to content

Commit 01396c4

Browse files
committed
doc: clarify synchronous blocking of Worker stdio
Fixes: #25630 Signed-off-by: James M Snell <[email protected]>
1 parent 184e0f7 commit 01396c4

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

doc/api/worker_threads.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1191,6 +1191,30 @@ active handle in the event system. If the worker is already `unref()`ed calling
11911191

11921192
## Notes
11931193

1194+
### Synchronous blocking of stdio
1195+
1196+
`Worker`s utilize message passing via {MessagePort} to implement interactions
1197+
with `stdio`. This means that `stdio` output originating from a `Worker` can
1198+
get blocked by synchronous code on the receiving end that is blocking the
1199+
Node.js event loop.
1200+
1201+
```js
1202+
'use strict';
1203+
1204+
const {
1205+
Worker,
1206+
isMainThread,
1207+
} = require('worker_threads');
1208+
1209+
if (isMainThread) {
1210+
new Worker(__filename);
1211+
for (let n = 0; n < 1e10; n++) {}
1212+
} else {
1213+
// This output will be blocked by the for loop in the main thread
1214+
console.log('foo');
1215+
}
1216+
```
1217+
11941218
### Launching worker threads from preload scripts
11951219

11961220
Take care when launching worker threads from preload scripts (scripts loaded

0 commit comments

Comments
 (0)