Add locks when dispatching a worker request
MODIFY
packages/@romejs/cli-diagnostics/DiagnosticsPrinter.ts
packages/@romejs/cli-diagnostics/DiagnosticsPrinter.ts
@@ -21,7 +21,6 @@ import {
DiagnosticsPrinterFlags,
DiagnosticsPrinterOptions,
} from "./types";
-
import {
formatAnsi,
markup,

MODIFY
packages/@romejs/core/master/Master.ts
packages/@romejs/core/master/Master.ts
@@ -66,6 +66,7 @@ import MasterReporter from "./MasterReporter";
import VirtualModules from "./fs/VirtualModules";
import {DiagnosticsProcessorOptions} from "@romejs/diagnostics/DiagnosticsProcessor";
import {toKebabCase} from "@romejs/string-utils";
+import Locker from "../common/utils/Locker";
const STDOUT_MAX_CHUNK_LENGTH = 100_000;
@@ -221,6 +222,8 @@ export default class Master {
},
);
+ this.fileLocker = new Locker();
+
this.connectedReporters = new MasterReporter(this);
this.connectedClientsListeningForLogs = new Set();
@@ -279,7 +282,7 @@ export default class Master {
cache: Cache;
connectedReporters: MasterReporter;
logger: Logger;
-
+ fileLocker: Locker<string>;
connectedClients: Set<MasterClient>;
connectedLSPServers: Set<LSPServer>;
connectedClientsListeningForLogs: Set<MasterClient>;

MODIFY
packages/@romejs/core/master/MasterRequest.ts
packages/@romejs/core/master/MasterRequest.ts
@@ -826,6 +826,8 @@ export default class MasterRequest {
): Promise<T> {
const {master} = this;
const owner = await master.fileAllocator.getOrAssignOwner(path);
+ const startMtime = master.memoryFs.maybeGetMtime(path);
+ const lock = await master.fileLocker.getLock(path.join());
const ref = master.projectManager.getTransportFileReference(path);
const marker = this.startMarker({
@@ -869,6 +871,13 @@ export default class MasterRequest {
// We don't want to tamper with these
throw err;
}
+ } finally {
+ lock.release();
+
+ const endMtime = this.master.memoryFs.maybeGetMtime(path);
+ if (endMtime !== startMtime) {
+ return this.wrapRequestDiagnostic(method, path, factory);
+ }
}
}

MODIFY
packages/@romejs/core/master/bundler/BundleRequest.ts
packages/@romejs/core/master/bundler/BundleRequest.ts
@@ -182,8 +182,6 @@ export default class BundleRequest {
assetPath,
};
- const lock = await this.bundler.compileLocker.getLock(source);
-
const res: WorkerCompileResult = await this.bundler.request.requestWorkerCompile(
path,
"compileForBundle",
@@ -193,8 +191,6 @@ export default class BundleRequest {
{},
);
- lock.release();
-
if (!res.cached) {
this.cached = false;
}

MODIFY
packages/@romejs/core/master/bundler/Bundler.ts
packages/@romejs/core/master/bundler/Bundler.ts
@@ -27,7 +27,6 @@ import {Dict} from "@romejs/typescript-helpers";
import {readFile} from "@romejs/fs";
import {flipPathPatterns} from "@romejs/path-match";
import {markup} from "@romejs/string-markup";
-import Locker from "@romejs/core/common/utils/Locker";
import {stringifyJSON} from "@romejs/codec-json";
export type BundlerEntryResoluton = {
@@ -43,12 +42,9 @@ export default class Bundler {
this.request = req;
this.entries = [];
-
- this.compileLocker = new Locker();
this.graph = new DependencyGraph(req, config.resolver);
}
- compileLocker: Locker<string>;
graph: DependencyGraph;
master: Master;
request: MasterRequest;

MODIFY
packages/@romejs/core/master/fs/MemoryFileSystem.ts
packages/@romejs/core/master/fs/MemoryFileSystem.ts
@@ -568,7 +568,16 @@ export default class MemoryFileSystem {
};
}
- getMtime(path: AbsoluteFilePath) {
+ maybeGetMtime(path: AbsoluteFilePath): undefined | number {
+ const stats = this.getFileStats(path);
+ if (stats === undefined) {
+ return undefined;
+ } else {
+ return stats.mtime;
+ }
+ }
+
+ getMtime(path: AbsoluteFilePath): number {
const stats = this.getFileStats(path);
if (stats === undefined) {
throw new Error(`File ${path.join()} not in database, cannot get mtime`);

Add locks when dispatching a worker request (#503)