Skip to content

Commit 27042d3

Browse files
committed
src: roll back web lock grants on failure
release the reserved lock if the callback throws or its settlement handlers cannot be attached. wake waiters and resume queue processing so other ready requests can proceed. Signed-off-by: Fatih Güzel <fatihguzeldev@gmail.com>
1 parent e91b6d2 commit 27042d3

2 files changed

Lines changed: 78 additions & 0 deletions

File tree

‎src/node_locks.cc‎

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -469,6 +469,35 @@ void LockManager::ProcessQueue(Environment* env) {
469469
}
470470
}
471471

472+
// Until settlement takes over, an early return must undo the grant.
473+
bool release_on_failure = true;
474+
auto rollback_grant = OnScopeLeave([&] {
475+
if (!release_on_failure) return;
476+
477+
{
478+
Mutex::ScopedLock scoped_lock(mutex_);
479+
ReleaseLock(granted_lock.get());
480+
if (pending_queue_.empty()) return;
481+
482+
std::unordered_set<Environment*> envs_to_wake;
483+
for (const auto& request : pending_queue_) {
484+
Environment* target_env = request->env();
485+
if (target_env != env && request->name() == granted_lock->name() &&
486+
envs_to_wake.insert(target_env).second) {
487+
WakeEnvironment(target_env);
488+
}
489+
}
490+
}
491+
492+
// An early return can also leave requests for other names unprocessed.
493+
// Keep this Environment alive for one more queue pass.
494+
if (env->can_call_into_js()) {
495+
env->SetImmediate([this](Environment* env) {
496+
if (env->can_call_into_js()) ProcessQueue(env);
497+
});
498+
}
499+
});
500+
472501
// Create the lock info object.
473502
Local<Object> lock_info;
474503
if (!CreateLockInfoObject(env, *grantable_request).ToLocal(&lock_info)) {
@@ -543,6 +572,7 @@ void LockManager::ProcessQueue(Environment* env) {
543572
// Lock granted: waiting_promise resolves now with the promise returned
544573
// by the callback; on_fulfilled/on_rejected will release the lock when
545574
// that promise settles.
575+
release_on_failure = false;
546576
if (grantable_request->waiting_promise()
547577
->Resolve(context, callback_result)
548578
.IsNothing()) {
@@ -563,6 +593,7 @@ void LockManager::ProcessQueue(Environment* env) {
563593
// callback
564594
return;
565595
}
596+
release_on_failure = false;
566597
}
567598
}
568599
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
'use strict';
2+
3+
const common = require('../common');
4+
const assert = require('node:assert');
5+
const { once } = require('node:events');
6+
const { Worker } = require('node:worker_threads');
7+
8+
async function testCallbackThrows() {
9+
for (const steal of [false, true]) {
10+
const name = 'callback-throws';
11+
const error = new Error('callback failed');
12+
let next;
13+
14+
await assert.rejects(navigator.locks.request(name, { steal }, common.mustCall(() => {
15+
next = navigator.locks.request(name, common.mustCall(() => 'granted'));
16+
throw error;
17+
})), error);
18+
19+
const { held } = await navigator.locks.query();
20+
assert.deepStrictEqual(held, []);
21+
assert.strictEqual(await next, 'granted');
22+
}
23+
}
24+
25+
async function testQueueContinuesAfterThrow() {
26+
const worker = new Worker(`
27+
const { parentPort } = require('node:worker_threads');
28+
navigator.locks.request('a', () => navigator.locks.request('b', () => {
29+
parentPort.once('message', () => process.exit());
30+
parentPort.postMessage('held');
31+
return new Promise(() => {});
32+
}));
33+
`, { eval: true });
34+
await once(worker, 'message');
35+
36+
const error = new Error('callback failed');
37+
const first = assert.rejects(navigator.locks.request('a', common.mustCall(() => {
38+
throw error;
39+
})), error);
40+
const next = navigator.locks.request('b', common.mustCall(() => 'granted'));
41+
worker.postMessage('exit');
42+
43+
await first;
44+
assert.strictEqual(await next, 'granted');
45+
}
46+
47+
testCallbackThrows().then(testQueueContinuesAfterThrow).then(common.mustCall());

0 commit comments

Comments
 (0)