Skip to content

Record your screen, ship a demo. Free and open-source, GPU-accelerated, no watermarks, no subscriptions. Windows, macOS, Linux. Actively maintained.

fix(diagnostics): finish short writes and ignore a clock stepped back

EtienneLescotedd1648b5954
diff --git a/electron/diagnostics/main-log-file.test.ts b/electron/diagnostics/main-log-file.test.ts
index f39531034..2cb1cebc1 100644
--- a/electron/diagnostics/main-log-file.test.ts
+++ b/electron/diagnostics/main-log-file.test.ts
@@ -196,17 +196,21 @@ describe("MainLogFile", () => {
expect(lines()).toHaveLength(2);
});
- it("keeps the current log when main.old.log cannot be read", () => {
- silenceConsole();
- // A directory opens but does not read, so only the older half of the tail fails.
- fs.mkdirSync(path.join(dir, "main.old.log"));
- const log = make();
- log.install(dir, "launch");
- console.info("current");
+ // Windows reads a directory as an empty file, so there is nothing to fail there.
+ it.skipIf(process.platform === "win32")(
+ "keeps the current log when main.old.log cannot be read",
+ () => {
+ silenceConsole();
+ // A directory opens but does not read, so only the older half of the tail fails.
+ fs.mkdirSync(path.join(dir, "main.old.log"));
+ const log = make();
+ log.install(dir, "launch");
+ console.info("current");
- expect(log.readTail(1000)).toContain("info current");
- expect(log.lastError).toBeTruthy();
- });
+ expect(log.readTail(1000)).toContain("info current");
+ expect(log.lastError).toBeTruthy();
+ },
+ );
it("still calls the console when an argument cannot be turned into text", () => {
silenceConsole();
@@ -321,6 +325,15 @@ describe("MainLogFile", () => {
]);
});
+ it("a clock stepped back does not drain the bucket", () => {
+ silenceConsole();
+ make().install(dir, "launch");
+ vi.setSystemTime(new Date("2026-10-18T11:59:00Z"));
+ for (let i = 0; i < 10; i += 1) console.warn(`line ${i}`);
+
+ expect(texts()).toHaveLength(1 + 10);
+ });
+
it("writes pending counts before the tail is read", () => {
silenceConsole();
const log = make();
diff --git a/electron/diagnostics/main-log-file.ts b/electron/diagnostics/main-log-file.ts
index 193454b3d..f42648f0a 100644
--- a/electron/diagnostics/main-log-file.ts
+++ b/electron/diagnostics/main-log-file.ts
@@ -181,10 +181,9 @@ export class MainLogFile {
this.lastKey = key;
this.lastLevel = level;
- this.tokens = Math.min(
- BURST_LINES,
- this.tokens + ((now - this.tokensAt) / 1000) * LINES_PER_SECOND,
- );
+ // A clock stepped back must not drain the bucket.
+ const elapsedMs = Math.max(0, now - this.tokensAt);
+ this.tokens = Math.min(BURST_LINES, this.tokens + (elapsedMs / 1000) * LINES_PER_SECOND);
this.tokensAt = now;
if (this.tokens < 1) {
// A line that was not written cannot be "repeated": its copies are suppressed too.
@@ -242,7 +241,13 @@ export class MainLogFile {
const data = Buffer.from(line, "utf-8");
if (this.bytes > 0 && this.bytes + data.length > this.maxFileBytes) this.rotate();
if (this.fd === null) return;
- fs.writeSync(this.fd, data);
+ // writeSync may write less than asked (a full disk, a signal): finish the line.
+ let written = 0;
+ while (written < data.length) {
+ const n = fs.writeSync(this.fd, data, written, data.length - written);
+ if (n <= 0) throw new Error("main.log: write made no progress");
+ written += n;
+ }
this.bytes += data.length;
} catch (error) {
this.fail(error);