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

refactor: move abort signal handling #25

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ const tests = [
'deadlock',
'recursive-deadlock',
'thread-main',
'signal',
];

(async () => {
Expand Down
33 changes: 33 additions & 0 deletions test/signal.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
'use strict';

const assert = require('assert').strict;
const { locks, AbortController } = require('..');

const sleep = msec =>
new Promise(resolve => {
setTimeout(() => {
resolve();
}, msec);
});

module.exports = async () => {
const startTs = Date.now();

const abortController = new AbortController();
const options = { mode: 'exclusive', signal: abortController.signal };
let hasErrorOccurred = false;

setTimeout(() => abortController.abort(), 500);

await locks
.request('A', options, async () => {
await sleep(1000);
})
.catch(() => {
hasErrorOccurred = true;
});

const endTs = Date.now();
assert.strictEqual(endTs - startTs < 1000, true, 'Lock should be aborted');
assert.strictEqual(hasErrorOccurred, true, 'Error should occur');
};
32 changes: 15 additions & 17 deletions web-locks.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ class Lock {
if (!buffer) Atomics.store(this.flag, 0, UNLOCKED);
}

enter(handler) {
return new Promise(resolve => {
this.queue.push({ handler, resolve });
enter(handler, signal) {
return new Promise((resolve, reject) => {
this.queue.push({ handler, resolve, reject, signal });
this.trying = true;
setTimeout(() => {
this.tryEnter();
Expand All @@ -38,11 +38,18 @@ class Lock {
if (prev === LOCKED) return;
this.owner = true;
this.trying = false;
const { handler, resolve } = this.queue.shift();
handler(this).finally(() => {

const { handler, resolve, reject, signal } = this.queue.shift();

const finalize = error => {
this.leave();
resolve();
});
if (error) reject(error);
else resolve();
};

if (signal) signal.on('abort', finalize);

handler(this).finally(finalize);
}

leave() {
Expand Down Expand Up @@ -100,16 +107,7 @@ class LockManager {
locks.send(message);
}

const finished = lock.enter(handler);
let aborted = null;
if (signal) {
aborted = new Promise((resolve, reject) => {
signal.on('abort', reject);
});
await Promise.race([finished, aborted]);
} else {
await finished;
}
await lock.enter(handler, signal);

setTimeout(() => {
lock.tryEnter();
Expand Down