Repository navigation
src: wake pending web locks when workers exit #67305
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
Open
fatihguzeldev
wants to merge
2
commits into
nodejs:main
Choose a base branch
from
fatihguzeldev:web-locks-worker-exit
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
This file contains hidden or 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,47 @@ | ||
| 'use strict'; | ||
|
|
||
| const common = require('../common'); | ||
| const assert = require('node:assert'); | ||
| const { once } = require('node:events'); | ||
| const { Worker } = require('node:worker_threads'); | ||
|
|
||
| async function testCallbackThrows() { | ||
| for (const steal of [false, true]) { | ||
| const name = 'callback-throws'; | ||
| const error = new Error('callback failed'); | ||
| let next; | ||
|
|
||
| await assert.rejects(navigator.locks.request(name, { steal }, common.mustCall(() => { | ||
| next = navigator.locks.request(name, common.mustCall(() => 'granted')); | ||
| throw error; | ||
| })), error); | ||
|
|
||
| const { held } = await navigator.locks.query(); | ||
| assert.deepStrictEqual(held, []); | ||
| assert.strictEqual(await next, 'granted'); | ||
| } | ||
| } | ||
|
|
||
| async function testQueueContinuesAfterThrow() { | ||
| const worker = new Worker(` | ||
| const { parentPort } = require('node:worker_threads'); | ||
| navigator.locks.request('a', () => navigator.locks.request('b', () => { | ||
| parentPort.once('message', () => process.exit()); | ||
| parentPort.postMessage('held'); | ||
| return new Promise(() => {}); | ||
| })); | ||
| `, { eval: true }); | ||
| await once(worker, 'message'); | ||
|
|
||
| const error = new Error('callback failed'); | ||
| const first = assert.rejects(navigator.locks.request('a', common.mustCall(() => { | ||
| throw error; | ||
| })), error); | ||
| const next = navigator.locks.request('b', common.mustCall(() => 'granted')); | ||
| worker.postMessage('exit'); | ||
|
|
||
| await first; | ||
| assert.strictEqual(await next, 'granted'); | ||
| } | ||
|
|
||
| testCallbackThrows().then(testQueueContinuesAfterThrow).then(common.mustCall()); |
This file contains hidden or 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,181 @@ | ||
| 'use strict'; | ||
|
|
||
| const common = require('../common'); | ||
| const assert = require('node:assert'); | ||
| const { once } = require('node:events'); | ||
| const { test } = require('node:test'); | ||
| const { Worker } = require('node:worker_threads'); | ||
|
|
||
| async function waitForGrant(request) { | ||
| let timeout; | ||
| try { | ||
| return await Promise.race([ | ||
| request, | ||
| new Promise((resolve, reject) => { | ||
| timeout = setTimeout(() => { | ||
| reject(new Error('Lock request was not granted after worker exit')); | ||
| }, common.platformTimeout(5000)); | ||
| }), | ||
| ]); | ||
| } finally { | ||
| clearTimeout(timeout); | ||
| } | ||
| } | ||
|
|
||
| for (const exitType of ['exit', 'throw', 'resolve-exit', 'release']) { | ||
| for (const waiterThread of ['main', 'worker']) { | ||
| test(`${exitType} wakes a pending lock request in ${waiterThread}`, async (t) => { | ||
| const name = `${exitType}-${waiterThread}`; | ||
| const holder = new Worker(` | ||
| const { parentPort, workerData } = require('node:worker_threads'); | ||
| navigator.locks.request(workerData.name, () => { | ||
| return new Promise((resolve) => { | ||
| parentPort.once('message', () => { | ||
| switch (workerData.exitType) { | ||
| case 'throw': | ||
| throw new Error('lock holder failed'); | ||
| case 'resolve-exit': | ||
| resolve(); | ||
| process.exit(); | ||
| break; | ||
| case 'release': | ||
| resolve(); | ||
| break; | ||
| default: | ||
| process.exit(); | ||
| } | ||
| }); | ||
| parentPort.postMessage('held'); | ||
| }); | ||
| }); | ||
| `, { eval: true, workerData: { name, exitType } }); | ||
| t.after(() => holder.terminate()); | ||
| let workerError; | ||
| holder.on('error', (err) => { workerError = err; }); | ||
| assert.deepStrictEqual(await once(holder, 'message'), ['held']); | ||
|
|
||
| let request; | ||
| if (waiterThread === 'worker') { | ||
| const waiter = new Worker(` | ||
| const { parentPort, workerData } = require('node:worker_threads'); | ||
| // A pending lock request alone does not keep the worker alive. | ||
| parentPort.on('message', () => {}); | ||
| navigator.locks.request(workerData, () => { | ||
| parentPort.postMessage('granted'); | ||
| }); | ||
| parentPort.postMessage('queued'); | ||
| `, { eval: true, workerData: name }); | ||
| t.after(() => waiter.terminate()); | ||
| assert.deepStrictEqual(await once(waiter, 'message'), ['queued']); | ||
| request = once(waiter, 'message').then(([message]) => message); | ||
| } else { | ||
| request = navigator.locks.request(name, common.mustCall(() => 'granted')); | ||
| const { pending } = await navigator.locks.query(); | ||
| assert.strictEqual(pending.length, 1); | ||
| assert.strictEqual(pending[0].name, name); | ||
| } | ||
|
|
||
| const exited = new Promise((resolve) => holder.once('exit', resolve)); | ||
| holder.postMessage('exit'); | ||
| assert.strictEqual(await exited, exitType === 'throw' ? 1 : 0); | ||
| if (exitType === 'throw') { | ||
| assert.strictEqual(workerError.message, 'lock holder failed'); | ||
| } else { | ||
| assert.strictEqual(workerError, undefined); | ||
| } | ||
| assert.strictEqual(await waitForGrant(request), 'granted'); | ||
| }); | ||
| } | ||
| } | ||
|
|
||
| test('exiting with a pending exclusive request unblocks a shared request', async (t) => { | ||
| const name = 'pending-worker-exit'; | ||
| await navigator.locks.request(name, { mode: 'shared' }, common.mustCall(async () => { | ||
| const worker = new Worker(` | ||
| const { parentPort, workerData } = require('node:worker_threads'); | ||
| parentPort.once('message', () => process.exit()); | ||
| navigator.locks.request(workerData, () => { | ||
| throw new Error('Exclusive lock must stay blocked'); | ||
| }); | ||
| parentPort.postMessage('queued'); | ||
| `, { eval: true, workerData: name }); | ||
| t.after(() => worker.terminate()); | ||
| assert.deepStrictEqual(await once(worker, 'message'), ['queued']); | ||
|
|
||
| const request = navigator.locks.request( | ||
| name, { mode: 'shared' }, common.mustCall(() => 'granted')); | ||
| const { pending } = await navigator.locks.query(); | ||
| assert.strictEqual(pending.length, 1); | ||
| assert.strictEqual(pending[0].mode, 'shared'); | ||
|
|
||
| const exited = once(worker, 'exit'); | ||
| worker.postMessage('exit'); | ||
| assert.deepStrictEqual(await exited, [0]); | ||
| assert.strictEqual(await waitForGrant(request), 'granted'); | ||
| })); | ||
| }); | ||
|
|
||
| test('concurrent holder exits wake multiple requests and workers', async (t) => { | ||
| const name = 'concurrent-worker-exit'; | ||
| const active = new Int32Array(new SharedArrayBuffer(4)); | ||
| const holders = Array.from({ length: 2 }, () => new Worker(` | ||
| const { parentPort, workerData } = require('node:worker_threads'); | ||
| navigator.locks.request(workerData, { mode: 'shared' }, () => { | ||
| parentPort.once('message', () => process.exit()); | ||
| parentPort.postMessage('held'); | ||
| return new Promise(() => {}); | ||
| }); | ||
| `, { eval: true, workerData: name })); | ||
| t.after(() => Promise.all(holders.map((worker) => worker.terminate()))); | ||
| assert.deepStrictEqual( | ||
| await Promise.all(holders.map((worker) => once(worker, 'message'))), | ||
| [['held'], ['held']]); | ||
|
|
||
| // Queue worker waiters first so cleanup wakes competing Environments. | ||
| const waiters = Array.from({ length: 8 }, () => new Worker(` | ||
| const assert = require('node:assert'); | ||
| const { parentPort, workerData } = require('node:worker_threads'); | ||
| const active = new Int32Array(workerData.counter); | ||
| parentPort.on('message', () => {}); | ||
| navigator.locks.request(workerData.name, () => { | ||
| assert.strictEqual(Atomics.add(active, 0, 1), 0); | ||
| return new Promise(resolve => { | ||
| parentPort.once('message', () => { | ||
| assert.strictEqual(Atomics.sub(active, 0, 1), 1); | ||
| resolve(); | ||
| }); | ||
| parentPort.postMessage('granted'); | ||
| }); | ||
| }).then(() => parentPort.postMessage('released')); | ||
| parentPort.postMessage('queued'); | ||
| `, { eval: true, workerData: { name, counter: active.buffer } })); | ||
| t.after(() => Promise.all(waiters.map((worker) => worker.terminate()))); | ||
| assert.deepStrictEqual( | ||
| await Promise.all(waiters.map((worker) => once(worker, 'message'))), | ||
| Array.from({ length: 8 }, () => ['queued'])); | ||
| const granted = waiters.map((worker) => new Promise((resolve, reject) => { | ||
| worker.on('message', (message) => { | ||
| if (message === 'granted') worker.postMessage('release'); | ||
| if (message === 'released') resolve('granted'); | ||
| }); | ||
| worker.once('error', reject); | ||
| })); | ||
|
|
||
| const requests = []; | ||
| for (const value of [0, 1]) { | ||
| requests.push(navigator.locks.request(name, common.mustCall(() => { | ||
| assert.strictEqual(Atomics.add(active, 0, 1), 0); | ||
| assert.strictEqual(Atomics.sub(active, 0, 1), 1); | ||
| return value; | ||
| }))); | ||
| } | ||
| const { pending } = await navigator.locks.query(); | ||
| assert.strictEqual(pending.length, 2); | ||
|
|
||
| const exited = holders.map((worker) => once(worker, 'exit')); | ||
| const completed = Promise.all([...exited, ...requests, ...granted]); | ||
| for (const worker of holders) worker.postMessage('exit'); | ||
| assert.deepStrictEqual(await waitForGrant(completed), | ||
| [[0], [0], 0, 1, ...Array(8).fill('granted')]); | ||
| assert.strictEqual(Atomics.load(active, 0), 0); | ||
| }); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since
granted_lockis now inserted intoheld_locks_up front here (and on line 461 forsteal), any earlyreturnbelow before the settlement handlers run or are attached (e.g.CreateLockInfoObjectfailing,callback->Call(...)throwing synchronously, orpromise->Then(...)failing) will leavegranted_lockstuck inheld_locks_for as long asenvstays alive and permanently block that lock name.Could we guard
granted_lockwith anOnScopeLeaverollback that removes it fromheld_locks_unless the handler attachment / synchronous release succeeds?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
thanks, rudolf. i added the rollback and a regression test.