Skip to content

Commit 5c38a55

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 c0deeea commit 5c38a55

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
@@ -1191,6 +1191,45 @@ 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+
```mjs
1202+
import {
1203+
Worker,
1204+
isMainThread,
1205+
} from 'worker_threads';
1206+
1207+
if (isMainThread) {
1208+
new Worker(new URL(import.meta.url));
1209+
for (let n = 0; n < 1e10; n++) {}
1210+
} else {
1211+
// This output will be blocked by the for loop in the main thread.
1212+
console.log('foo');
1213+
}
1214+
```
1215+
1216+
```cjs
1217+
'use strict';
1218+
1219+
const {
1220+
Worker,
1221+
isMainThread,
1222+
} = require('worker_threads');
1223+
1224+
if (isMainThread) {
1225+
new Worker(__filename);
1226+
for (let n = 0; n < 1e10; n++) {}
1227+
} else {
1228+
// This output will be blocked by the for loop in the main thread.
1229+
console.log('foo');
1230+
}
1231+
```
1232+
11941233
### Launching worker threads from preload scripts
11951234
11961235
Take care when launching worker threads from preload scripts (scripts loaded

0 commit comments

Comments
 (0)