Skip to content

Commit a24ee0b

Browse files
committed
fix: Avoid EEXIST on race condition in createDirRecursively and copyFolderRecursiveSync
Replace custom recursive directory creation with built-in recursive mkdir in node addon loading When multiple packaged applications try to start simultaneously, they can encounter race conditions when creating temporary directories for loading native node addons. This manifests as an "EEXIST" error when calling `mkdirSync()`. The issue occurs in the native module loading code path where pkg needs to extract native addons to a temporary location before they can be loaded via `process.dlopen()`. The current implementation uses a custom `createDirRecursively()` function that has a race condition - it checks if a directory exists and then tries to create it, but another process could create the directory between the check and creation. Node.js has built-in support for recursive directory creation via the `recursive: true` option in `mkdirSync()`. This handles race conditions properly - if the directory already exists, it will not throw an error. This is exactly what we need.
1 parent e8f9863 commit a24ee0b

File tree

1 file changed

+2
-11
lines changed

1 file changed

+2
-11
lines changed

prelude/bootstrap.js

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -185,9 +185,7 @@ function copyFolderRecursiveSync(source, target) {
185185
const targetFolder = path.join(target, path.basename(source));
186186

187187
// Check if target folder needs to be created or integrated
188-
if (!fs.existsSync(targetFolder)) {
189-
fs.mkdirSync(targetFolder);
190-
}
188+
fs.mkdirSync(targetFolder, { recursive: true });
191189

192190
// Copy
193191
if (fs.lstatSync(source).isDirectory()) {
@@ -249,13 +247,6 @@ function copyFolderRecursiveSync(source, target) {
249247
}
250248
}
251249

252-
function createDirRecursively(dir) {
253-
if (!fs.existsSync(dir)) {
254-
createDirRecursively(path.join(dir, '..'));
255-
fs.mkdirSync(dir);
256-
}
257-
}
258-
259250
/*
260251
261252
// TODO move to some test
@@ -2218,7 +2209,7 @@ function payloadFileSync(pointer) {
22182209
// Example: /home/john/.cache/pkg/<hash>
22192210
const tmpFolder = path.join(homedir(), '.cache/pkg', hash);
22202211

2221-
createDirRecursively(tmpFolder);
2212+
fs.mkdirSync(tmpFolder, { recursive: true });
22222213

22232214
// Example: moduleFolder = /snapshot/appname/node_modules/sharp/build/Release
22242215
const parts = moduleFolder.split(path.sep);

0 commit comments

Comments
 (0)