diagnostics_channel: add diagnostics channels for web locks by IlyasShabi · Pull Request #62123 · nodejs/node · GitHub
Skip to content
Merged
Show file tree
Hide file tree
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
46 changes: 46 additions & 0 deletions doc/api/diagnostics_channel.md
60 changes: 57 additions & 3 deletions lib/internal/locks.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,13 @@ const {
createEnumConverter,
createDictionaryConverter,
} = require('internal/webidl');
const dc = require('diagnostics_channel');

const locks = internalBinding('locks');
const lockRequestStartChannel = dc.channel('locks.request.start');
const lockRequestGrantChannel = dc.channel('locks.request.grant');
const lockRequestMissChannel = dc.channel('locks.request.miss');
const lockRequestEndChannel = dc.channel('locks.request.end');

const kName = Symbol('kName');
const kMode = Symbol('kMode');
Expand Down Expand Up @@ -113,6 +118,30 @@ function convertLockError(error) {
return error;
}

function publishLockRequestStart(name, mode) {
if (lockRequestStartChannel.hasSubscribers) {
lockRequestStartChannel.publish({ name, mode });
}
}

function publishLockRequestGrant(name, mode) {
if (lockRequestGrantChannel.hasSubscribers) {
lockRequestGrantChannel.publish({ name, mode });
}
}

function publishLockRequestMiss(name, mode, ifAvailable) {
if (ifAvailable && lockRequestMissChannel.hasSubscribers) {
lockRequestMissChannel.publish({ name, mode });
}
}

function publishLockRequestEnd(name, mode, ifAvailable, steal, error) {
if (lockRequestEndChannel.hasSubscribers) {
lockRequestEndChannel.publish({ name, mode, ifAvailable, steal, error });
}
}

// https://w3c.github.io/web-locks/#api-lock-manager
class LockManager {
constructor(symbol = undefined) {
Expand Down Expand Up @@ -192,6 +221,7 @@ class LockManager {
}

const clientId = `node-${process.pid}-${threadId}`;
publishLockRequestStart(name, mode);

// Handle requests with AbortSignal
if (signal) {
Expand All @@ -212,6 +242,8 @@ class LockManager {
return undefined;
}
lockGranted = true;
publishLockRequestGrant(name, mode);

return callback(createLock(lock));
});
};
Expand All @@ -228,27 +260,49 @@ class LockManager {

// When released promise settles, clean up listener and resolve main promise
SafePromisePrototypeFinally(
PromisePrototypeThen(released, resolve, (error) => reject(convertLockError(error))),
PromisePrototypeThen(
released,
(result) => {
publishLockRequestEnd(name, mode, ifAvailable, steal, undefined);
resolve(result);
},
(error) => {
const convertedError = convertLockError(error);
publishLockRequestEnd(name, mode, ifAvailable, steal, convertedError);
reject(convertedError);
},
),
() => signal.removeEventListener('abort', abortListener),
);
} catch (error) {
signal.removeEventListener('abort', abortListener);
reject(convertLockError(error));
const convertedError = convertLockError(error);
publishLockRequestEnd(name, mode, ifAvailable, steal, convertedError);
reject(convertedError);
}
});
}

// When ifAvailable: true and lock is not available, C++ passes null to indicate no lock granted
const wrapCallback = (internalLock) => {
if (internalLock === null) {
publishLockRequestMiss(name, mode, ifAvailable);
} else {
publishLockRequestGrant(name, mode);
}
const lock = createLock(internalLock);
return callback(lock);
};

// Standard request without signal
try {
return await locks.request(name, clientId, mode, steal, ifAvailable, wrapCallback);
const result = await locks.request(name, clientId, mode, steal, ifAvailable, wrapCallback);
publishLockRequestEnd(name, mode, ifAvailable, steal, undefined);

return result;
} catch (error) {
const convertedError = convertLockError(error);
publishLockRequestEnd(name, mode, ifAvailable, steal, convertedError);
throw convertedError;
}
}
Expand Down
167 changes: 167 additions & 0 deletions test/parallel/test-diagnostics-channel-web-locks.js
Loading