Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/canonicalIdentityLock.js
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,8 @@ function prepareOwnerFile(lockDir, token) {
mode: PRIVATE_FILE_MODE,
flag: 'wx',
});
descriptor = fs.openSync(preparedOwnerFile, 'r');
// Windows requires a writable handle for FlushFileBuffers/fdatasync.
descriptor = fs.openSync(preparedOwnerFile, 'r+');
fs.fdatasyncSync(descriptor);
fs.closeSync(descriptor);
descriptor = null;
Expand Down
22 changes: 22 additions & 0 deletions test/canonicalIdentityLock.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,28 @@ test('partial owner preparation failure never exposes a canonical lock', () => {
}
});

test('prepares owner files with a writable descriptor for fdatasync', () => {
const root = fs.mkdtempSync(path.join(os.tmpdir(), 'canonical-lock-fdatasync-'));
const nodeIdFile = path.join(root, 'node_id');
const lock = require('../src/canonicalIdentityLock');
const originalOpenSync = fs.openSync;
let stagedOwnerFlags = null;

try {
fs.openSync = function captureStagedOwnerFlags(file, flags, ...args) {
if (String(file).endsWith('.tmp')) stagedOwnerFlags = flags;
return originalOpenSync.call(fs, file, flags, ...args);
};

const release = lock.acquireCanonicalIdentityLock(nodeIdFile);
release();
assert.equal(stagedOwnerFlags, 'r+');
} finally {
fs.openSync = originalOpenSync;
fs.rmSync(root, { recursive: true, force: true });
}
});

test('release cleanup failure leaves canonical path available to a successor', () => {
const root = fs.mkdtempSync(path.join(os.tmpdir(), 'canonical-lock-release-'));
const nodeIdFile = path.join(root, 'node_id');
Expand Down
Loading