Skip to content

Commit

Permalink
doc: clarify synchronous blocking of Worker stdio
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
jasnell authored and targos committed Jun 11, 2021
1 parent 8809ef9 commit 2e8dfee
Showing 1 changed file with 39 additions and 0 deletions.
39 changes: 39 additions & 0 deletions doc/api/worker_threads.md
Expand Up @@ -968,6 +968,45 @@ active handle in the event system. If the worker is already `unref()`ed calling

## Notes

### Synchronous blocking of stdio

`Worker`s utilize message passing via {MessagePort} to implement interactions
with `stdio`. This means that `stdio` output originating from a `Worker` can
get blocked by synchronous code on the receiving end that is blocking the
Node.js event loop.

```mjs
import {
Worker,
isMainThread,
} from 'worker_threads';

if (isMainThread) {
new Worker(new URL(import.meta.url));
for (let n = 0; n < 1e10; n++) {}
} else {
// This output will be blocked by the for loop in the main thread.
console.log('foo');
}
```
```cjs
'use strict';

const {
Worker,
isMainThread,
} = require('worker_threads');

if (isMainThread) {
new Worker(__filename);
for (let n = 0; n < 1e10; n++) {}
} else {
// This output will be blocked by the for loop in the main thread.
console.log('foo');
}
```
### Launching worker threads from preload scripts
Take care when launching worker threads from preload scripts (scripts loaded
Expand Down

0 comments on commit 2e8dfee

Please sign in to comment.