Skip to content

Commit

Permalink
fix : Synchronize read and writes to avoid race condition reading tra…
Browse files Browse the repository at this point in the history
…nspiled files from disk cache

First add a maker file to check if the previous write was fully completed. Then again in writefile section change the write mode to wx which uses exclusive lock and if two writes happen same time and we get EEXist error then ignore that
FIX for microsoft#30577
  • Loading branch information
sunilsurana committed Apr 27, 2024
1 parent 1f63cbf commit 733ee09
Showing 1 changed file with 23 additions and 11 deletions.
34 changes: 23 additions & 11 deletions packages/playwright/src/transform/compilationCache.ts
Expand Up @@ -126,23 +126,35 @@ export function getFromCompilationCache(filename: string, hash: string, moduleUr
const codePath = cachePath + '.js';
const sourceMapPath = cachePath + '.map';
const dataPath = cachePath + '.data';
try {
const cachedCode = fs.readFileSync(codePath, 'utf8');
const serializedCache = _innerAddToCompilationCacheAndSerialize(filename, { codePath, sourceMapPath, dataPath, moduleUrl });
return { cachedCode, serializedCache };
} catch {
const markerFile = codePath + '-marker';
if (fs.existsSync(markerFile)) {
try {
const cachedCode = fs.readFileSync(codePath, 'utf8');
const serializedCache = _innerAddToCompilationCacheAndSerialize(filename, { codePath, sourceMapPath, dataPath, moduleUrl });
return { cachedCode, serializedCache };
} catch {
}
}

return {
addToCache: (code: string, map: any | undefined | null, data: Map<string, any>) => {
if (isWorkerProcess())
return {};
fs.mkdirSync(path.dirname(cachePath), { recursive: true });
if (map)
fs.writeFileSync(sourceMapPath, JSON.stringify(map), 'utf8');
if (data.size)
fs.writeFileSync(dataPath, JSON.stringify(Object.fromEntries(data.entries()), undefined, 2), 'utf8');
fs.writeFileSync(codePath, code, 'utf8');
try {
fs.mkdirSync(path.dirname(cachePath), { recursive: true });
if (map)
fs.writeFileSync(sourceMapPath, JSON.stringify(map), { encoding: 'utf8', flag: 'wx' });
if (data.size)
fs.writeFileSync(dataPath, JSON.stringify(Object.fromEntries(data.entries()), undefined, 2), { encoding: 'utf8', flag: 'wx' });
fs.writeFileSync(codePath, code, 'utf8');
fs.closeSync(fs.openSync(markerFile, 'wx'));
}
catch (error) {
if (error.code === 'EEXIST') {
} else {
throw error;
}
}
const serializedCache = _innerAddToCompilationCacheAndSerialize(filename, { codePath, sourceMapPath, dataPath, moduleUrl });
return { serializedCache };
}
Expand Down

0 comments on commit 733ee09

Please sign in to comment.