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
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"changes": [
{
"packageName": "@microsoft/rush",
"comment": "Delete the `build-cache-restore-*` staging folders that an interrupted build cache restore left in a project's `.rush/temp` folder.",
"type": "patch"
}
],
"packageName": "@microsoft/rush",
"email": "178374507+HerambInamke@users.noreply.github.com"
}
57 changes: 56 additions & 1 deletion libraries/rush-lib/src/logic/buildCache/StagedRestore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ import type { ITerminal } from '@rushstack/terminal';
*/
export const STAGING_FOLDER_PREFIX: string = 'build-cache-restore-';

// A staging folder is named `build-cache-restore-<process id>-<random>`, so that a folder left by a killed process can
// be recognized.
const STAGING_FOLDER_NAME_REGEXP: RegExp = new RegExp(`^${STAGING_FOLDER_PREFIX}(\\d+)-`);

const MOVE_CONCURRENCY: number = 10;

/**
Expand Down Expand Up @@ -77,7 +81,10 @@ export async function tryRestoreThroughStagingFolderAsync(
}

await mkdir(projectRushTempFolder, { recursive: true });
stagingFolderPath = await mkdtemp(path.join(projectRushTempFolder, STAGING_FOLDER_PREFIX));
await deleteStaleStagingFoldersAsync(terminal, projectRushTempFolder);
stagingFolderPath = await mkdtemp(
path.join(projectRushTempFolder, `${STAGING_FOLDER_PREFIX}${process.pid}-`)
);
terminal.writeVerboseLine(
`Extracting the cache entry into "${stagingFolderPath}", to move the output folders into place from there.`
);
Expand Down Expand Up @@ -116,6 +123,54 @@ export async function tryRestoreThroughStagingFolderAsync(
}
}

// Deletes the staging folders that a process left when it was killed or exited on a signal during a restore, since its
// finally block didn't run. A folder of a process that is still running may be in use, so it is kept: several phases of
// one project can restore at the same time. A folder whose name has no process id is kept for the same reason.
async function deleteStaleStagingFoldersAsync(
terminal: ITerminal,
projectRushTempFolder: string
): Promise<void> {
let names: string[];
try {
names = await readdir(projectRushTempFolder);
} catch (error) {
terminal.writeVerboseLine(
`Unable to look for stale staging folders in "${projectRushTempFolder}": ${error}`
);
return;
}

await Promise.all(
names.map(async (name: string) => {
const match: RegExpExecArray | null = STAGING_FOLDER_NAME_REGEXP.exec(name);
if (!match || isProcessRunning(Number(match[1]))) {
return;
}
const folderPath: string = path.join(projectRushTempFolder, name);
try {
await rm(folderPath, { recursive: true, force: true });
terminal.writeVerboseLine(`Deleted the stale staging folder "${folderPath}".`);
} catch (error) {
terminal.writeVerboseLine(`Unable to delete the stale staging folder "${folderPath}": ${error}`);
}
})
);
}

function isProcessRunning(pid: number): boolean {
if (pid === process.pid) {
return true;
}
try {
// Signal 0 only checks whether the process exists.
process.kill(pid, 0);
return true;
} catch (error) {
// EPERM means that the process exists but belongs to another user.
return (error as NodeJS.ErrnoException).code !== 'ESRCH';
}
}

// Returns why the output folders can't be restored through a staging folder, or undefined if they can, in which case
// it fills relativeFolderPaths with the normalized project-relative paths of the output folders.
function getUnsupportedReason(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license.
// See LICENSE in the project root for license information.

import { spawnSync } from 'node:child_process';
import * as fs from 'node:fs';
import * as os from 'node:os';
import * as path from 'node:path';
Expand Down Expand Up @@ -566,4 +567,28 @@ describe('OperationBuildCache restores through a staging folder', () => {
expect(output).toContain(SKIP_LINE);
expect(untarMock).toHaveBeenCalledTimes(1);
});

it('U8: deletes the staging folders of processes that are no longer running', async () => {
// A process that has exited, like a Rush process that was killed during a restore.
const exitedPid: number = spawnSync(process.execPath, ['-e', '']).pid;
const staleFolder: string = `${STAGING_FOLDER_PREFIX}${exitedPid}-abc123`;
// A process that is still running may be restoring into its folder.
const runningFolder: string = `${STAGING_FOLDER_PREFIX}${process.ppid}-def456`;
// Without a process id, there is no way to tell whether the folder is in use.
const legacyFolder: string = `${STAGING_FOLDER_PREFIX}ghi789`;
const rushTempFolder: string = path.join(projectFolder, '.rush', 'temp');
for (const folderName of [staleFolder, runningFolder, legacyFolder]) {
writeFiles(path.join(rushTempFolder, folderName), { 'lib/plugin.js': 'stale' });
}
writeFiles(projectFolder, OLD_OUTPUTS);
seedEntry();

const { result } = await restoreAsync(createSubject());

expect(result).toBe(true);
expect(path.basename(untarMock.mock.calls[0][0].outputFolderPath)).toMatch(
new RegExp(`^${STAGING_FOLDER_PREFIX}${process.pid}-`)
);
expect(getStagingFolders().sort()).toEqual([legacyFolder, runningFolder].sort());
});
});
Loading