Skip to content

Commit 61a67c1

Browse files
committed
doc: clarify synchronous blocking of Worker stdio
Fixes: #25630 Signed-off-by: James M Snell <jasnell@gmail.com> PR-URL: #38658 Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
1 parent 16cb4f7 commit 61a67c1

File tree

1 file changed

+39
-0
lines changed

1 file changed

+39
-0
lines changed

doc/api/worker_threads.md

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

11941194
## Notes
11951195

1196+
### Synchronous blocking of stdio
1197+
1198+
`Worker`s utilize message passing via {MessagePort} to implement interactions
1199+
with `stdio`. This means that `stdio` output originating from a `Worker` can
1200+
get blocked by synchronous code on the receiving end that is blocking the
1201+
Node.js event loop.
1202+
1203+
```mjs
1204+
import {
1205+
Worker,
1206+
isMainThread,
1207+
} from 'worker_threads';
1208+
1209+
if (isMainThread) {
1210+
new Worker(new URL(import.meta.url));
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+
1218+
```cjs
1219+
'use strict';
1220+
1221+
const {
1222+
Worker,
1223+
isMainThread,
1224+
} = require('worker_threads');
1225+
1226+
if (isMainThread) {
1227+
new Worker(__filename);
1228+
for (let n = 0; n < 1e10; n++) {}
1229+
} else {
1230+
// This output will be blocked by the for loop in the main thread.
1231+
console.log('foo');
1232+
}
1233+
```
1234+
11961235
### Launching worker threads from preload scripts
11971236
11981237
Take care when launching worker threads from preload scripts (scripts loaded

0 commit comments

Comments
 (0)