Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

doc: clarify synchronous blocking of worker stdio #38658

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions doc/api/worker_threads.md
Original file line number Diff line number Diff line change
Expand Up @@ -1191,6 +1191,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