Skip to content

Commit

Permalink
lock file
Browse files Browse the repository at this point in the history
  • Loading branch information
midleman committed Nov 28, 2024
1 parent f64ba98 commit 50a7404
Showing 1 changed file with 22 additions and 1 deletion.
23 changes: 22 additions & 1 deletion test/automation/src/playwrightBrowser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ import { URI } from 'vscode-uri';
import { Logger, measureAndLog } from './logger';
import type { LaunchOptions } from './code';
import { PlaywrightDriver } from './playwrightDriver';
// --- Start Positron ---
import * as fs from 'fs';
// --- End Positron ---

const root = join(__dirname, '..', '..', '..');

Expand Down Expand Up @@ -47,6 +50,7 @@ async function launchServer(options: LaunchOptions) {
...process.env,
};

const lockFilePath = join(agentFolder, 'node.lock');
const maxRetries = 10;
let serverProcess: ChildProcess | null = null;
let endpoint: string | undefined;
Expand All @@ -59,7 +63,13 @@ async function launchServer(options: LaunchOptions) {
logger.log(`Attempting to start server on port ${currentPort}`);
logger.log(`Command: '${serverLocation}' ${args.join(' ')}`);

let lockFile: fs.promises.FileHandle | null = null;

try {
// create and acquire the lock
lockFile = await fs.promises.open(lockFilePath, 'w'); // open lock file
await lockFile.write('locked'); // write to lock file to signal lock acquisition

serverProcess = await startServer(serverLocation, args, env, logger);
endpoint = await measureAndLog(
() => waitForEndpoint(serverProcess!, logger),
Expand All @@ -73,9 +83,20 @@ async function launchServer(options: LaunchOptions) {
if ((error as Error).message.includes('EADDRINUSE')) {
logger.log(`Port ${currentPort} is already in use. Retrying...`);
serverProcess?.kill();
} else if ((error as Error).message.includes('EBUSY')) {
logger.log('Node.js binary is busy. Waiting for lock...');
await new Promise(resolve => setTimeout(resolve, 500));
} else {
throw error; // Rethrow non-port-related errors
throw error;
}
} finally {
// ensure the lock file is properly closed and cleaned up
if (lockFile) {
await lockFile.close();
}
await fs.promises.unlink(lockFilePath).catch(() => {
logger.log(`Failed to remove lock file: ${lockFilePath}`);
});
}
}

Expand Down

0 comments on commit 50a7404

Please sign in to comment.