fix(tsunpacker): use synchronous fs operations in tsunpacker to avoid readdir race conditions
This commit is contained in:
@@ -90,9 +90,9 @@ export class TsUnpacker {
|
||||
* 2. Moving contents of the nested source folder up to the dest dir
|
||||
* 3. Removing the now-empty nested source folder
|
||||
*
|
||||
* This approach never creates temporary sibling directories and never
|
||||
* removes the destination directory itself, avoiding filesystem race
|
||||
* conditions observed with the previous rename-rm-rename pattern.
|
||||
* Uses synchronous fs operations to avoid race conditions with
|
||||
* async readdir returning partial/stale results under signal pressure
|
||||
* or XFS metadata lag (observed in process-group environments like gitzone).
|
||||
*
|
||||
* Returns true if unpacking was performed, false if skipped.
|
||||
*/
|
||||
@@ -108,32 +108,24 @@ export class TsUnpacker {
|
||||
const nestedPath = this.getNestedPath();
|
||||
|
||||
// Step 1: Remove sibling entries (everything in dest except the source folder)
|
||||
// Loop handles partial readdir results from signal-interrupted getdents64
|
||||
let destEntries = await fs.promises.readdir(this.destDir);
|
||||
while (destEntries.some(e => e !== this.sourceFolderName)) {
|
||||
for (const entry of destEntries) {
|
||||
if (entry !== this.sourceFolderName) {
|
||||
await fs.promises.rm(path.join(this.destDir, entry), { recursive: true, force: true });
|
||||
}
|
||||
const destEntries = fs.readdirSync(this.destDir);
|
||||
for (const entry of destEntries) {
|
||||
if (entry !== this.sourceFolderName) {
|
||||
fs.rmSync(path.join(this.destDir, entry), { recursive: true, force: true });
|
||||
}
|
||||
destEntries = await fs.promises.readdir(this.destDir);
|
||||
}
|
||||
|
||||
// Step 2: Move all contents from nested dir up to dest dir
|
||||
// Loop handles partial readdir results from signal-interrupted getdents64
|
||||
let nestedEntries = await fs.promises.readdir(nestedPath);
|
||||
while (nestedEntries.length > 0) {
|
||||
for (const entry of nestedEntries) {
|
||||
await fs.promises.rename(
|
||||
path.join(nestedPath, entry),
|
||||
path.join(this.destDir, entry),
|
||||
);
|
||||
}
|
||||
nestedEntries = await fs.promises.readdir(nestedPath);
|
||||
const nestedEntries = fs.readdirSync(nestedPath);
|
||||
for (const entry of nestedEntries) {
|
||||
fs.renameSync(
|
||||
path.join(nestedPath, entry),
|
||||
path.join(this.destDir, entry),
|
||||
);
|
||||
}
|
||||
|
||||
// Step 3: Remove the now-empty nested directory
|
||||
await fs.promises.rmdir(nestedPath);
|
||||
fs.rmdirSync(nestedPath);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user