Skip to content

Commit

Permalink
fix(sdk): improve simulator lockfile implementation (#6965)
Browse files Browse the repository at this point in the history
This changeset adds the following behavior to the simulator lockfile:

- If the lockfile gets compromised, it will try to acquire the lock
again
- If the lockfile gets compromised, the console will show a blue screen
of death with an explanation

Fixes #6949.
  • Loading branch information
skyrpex authored Jul 30, 2024
1 parent 30fd90e commit db3e003
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 3 deletions.
10 changes: 9 additions & 1 deletion apps/wing-console/console/server/src/utils/simulator.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { simulator } from "@winglang/sdk";
import { LogLevel, TraceType } from "@winglang/sdk/lib/std/test-runner.js";
import Emittery from "emittery";

import type { ResourceLifecycleEvent, Trace } from "../types.js";
Expand Down Expand Up @@ -62,7 +63,14 @@ export const createSimulator = (props?: CreateSimulatorProps): Simulator => {
});
instance.onTrace({
callback(trace) {
events.emit("trace", trace);
if (
trace.type === TraceType.SIMULATOR &&
trace.level === LogLevel.ERROR
) {
events.emit("error", new Error(trace.data.message));
} else {
events.emit("trace", trace);
}
},
});
instance.onResourceLifecycleEvent({
Expand Down
13 changes: 11 additions & 2 deletions libs/wingsdk/src/simulator/lockfile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,17 @@ export class Lockfile {
if (this.lastMtime) {
// Check if the lockfile got compromised because we were too late to update it.
if (Date.now() > this.lastMtime + STALE_THRESHOLD) {
this.markAsCompromised("Lockfile was not updated in time");
return;
// Try to acquire the lock again.
try {
fs.closeSync(this.lockfile);
this.lockfile = undefined;
this.lastMtime = undefined;
this.lock();
return;
} catch (error) {
this.markAsCompromised("Lockfile was not updated in time", error);
return;
}
}

// Check if the lockfile got compromised because access was lost or something else updated it.
Expand Down
12 changes: 12 additions & 0 deletions libs/wingsdk/src/simulator/simulator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,18 @@ export class Simulator {
if (error) {
console.error(error);
}
this.addTrace({
data: {
message: `Another process is already running the simulator on the same state directory.`,
error,
status: "failure",
},
type: TraceType.SIMULATOR,
level: LogLevel.ERROR,
sourcePath: "",
sourceType: "",
timestamp: new Date().toISOString(),
});
await this.stop();
},
});
Expand Down

0 comments on commit db3e003

Please sign in to comment.