-
Notifications
You must be signed in to change notification settings - Fork 29.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
worker: prevent event loop starvation through MessagePorts
Limit the number of messages processed without interruption on a given `MessagePort` to prevent event loop starvation, but still make sure that all messages are emitted that were already in the queue when emitting began. This aligns the behaviour better with the web. Refs: #28030 PR-URL: #29315 Reviewed-By: Gus Caplan <me@gus.host> Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Jeremiah Senkpiel <fishrock123@rocketmail.com>
- Loading branch information
Showing
3 changed files
with
63 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
15 changes: 15 additions & 0 deletions
15
test/parallel/test-worker-message-port-close-while-receiving.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
'use strict'; | ||
const common = require('../common'); | ||
|
||
const { MessageChannel } = require('worker_threads'); | ||
|
||
// Make sure that closing a message port while receiving messages on it does | ||
// not stop messages that are already in the queue from being emitted. | ||
|
||
const { port1, port2 } = new MessageChannel(); | ||
|
||
port1.on('message', common.mustCall(() => { | ||
port1.close(); | ||
}, 2)); | ||
port2.postMessage('foo'); | ||
port2.postMessage('bar'); |
29 changes: 29 additions & 0 deletions
29
test/parallel/test-worker-message-port-infinite-message-loop.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
'use strict'; | ||
const common = require('../common'); | ||
const assert = require('assert'); | ||
|
||
const { MessageChannel } = require('worker_threads'); | ||
|
||
// Make sure that an infinite asynchronous .on('message')/postMessage loop | ||
// does not lead to a stack overflow and does not starve the event loop. | ||
// We schedule timeouts both from before the the .on('message') handler and | ||
// inside of it, which both should run. | ||
|
||
const { port1, port2 } = new MessageChannel(); | ||
let count = 0; | ||
port1.on('message', () => { | ||
if (count === 0) { | ||
setTimeout(common.mustCall(() => { | ||
port1.close(); | ||
}), 0); | ||
} | ||
|
||
port2.postMessage(0); | ||
assert(count++ < 10000, `hit ${count} loop iterations`); | ||
}); | ||
|
||
port2.postMessage(0); | ||
|
||
// This is part of the test -- the event loop should be available and not stall | ||
// out due to the recursive .postMessage() calls. | ||
setTimeout(common.mustCall(), 0); |