Skip to content

The fastest browser for AI agents to run browser automation, built for sharing your logged-in browser state with your AI agents, like Codex or Claude Code, without disturbing you. Zero cost, zero config.

fix(ego-browser): refresh the download waiter marker while a wait is pending

Zhuowei Wang483280d07114
diff --git a/package/ego-browser/src/driver/downloads.test.mjs b/package/ego-browser/src/driver/downloads.test.mjs
index 0ebff71..f1be8fe 100644
--- a/package/ego-browser/src/driver/downloads.test.mjs
+++ b/package/ego-browser/src/driver/downloads.test.mjs
@@ -8,6 +8,7 @@ import {
readFile,
rename,
rm,
+ utimes,
writeFile,
} from "node:fs/promises";
import { join } from "node:path";
@@ -538,6 +539,33 @@ test("a marker left by a dead process does not block the default behavior", asyn
disposeDownloadArtifacts();
});
+test("a marker that stopped aging is stale even when its pid is alive", async () => {
+ // The runner's pid is alive, but a marker it has not refreshed for over an
+ // hour belongs to a dead process whose pid was reused.
+ const reusedMarker = waiterMarker(process.ppid);
+ await writeFile(reusedMarker, JSON.stringify({ pid: process.ppid }));
+ const old = new Date(Date.now() - 2 * 60 * 60 * 1_000);
+ await utimes(reusedMarker, old, old);
+ const services = recordingServices();
+ const interception = preparePageDownload(services, "target-reused", {
+ timeoutMs: 1_000,
+ });
+ await interception.ready("session-reused");
+ beginDownload(services, "target-reused", "guid-reused", "reused.txt");
+ const download = await interception.event;
+ await writeFile(join(sharedDirectory, "guid-reused"), "reused");
+ completeDownload(services, "target-reused", "guid-reused");
+ await download.path();
+
+ assert.deepEqual(behaviorCalls(services.calls).at(-1), {
+ behavior: "default",
+ sessionId: "session-reused",
+ });
+ await assert.rejects(access(reusedMarker));
+ await download.delete();
+ disposeDownloadArtifacts();
+});
+
test("disposing the SDK removes only this process's download files", async () => {
const services = recordingServices();
const interception = preparePageDownload(services, "target-dispose", {
diff --git a/package/ego-browser/src/driver/downloads.ts b/package/ego-browser/src/driver/downloads.ts
index 6752ee1..ccd0760 100644
--- a/package/ego-browser/src/driver/downloads.ts
+++ b/package/ego-browser/src/driver/downloads.ts
@@ -53,10 +53,12 @@ export type DownloadInterception = {
// no process on this machine still waits for a download.
const WAITERS_DIRECTORY = "waiters";
const STALE_WAITER_MS = 60 * 60 * 1_000;
+const WAITER_REFRESH_MS = 10 * 60 * 1_000;
const STALE_FILE_MS = 24 * 60 * 60 * 1_000;
const FILE_POLL_INTERVAL_MS = 50;
let sharedDirectory: string | undefined;
+let waiterRefreshTimer: ReturnType<typeof setInterval> | undefined;
const ownedFiles = new Set<string>();
const armedInterceptions = new Set<symbol>();
const activeInterceptions = new Set<() => void>();
@@ -361,8 +363,25 @@ class PageDownloadArtifact implements DownloadArtifact {
// Every process that waits for a download leaves `waiters/<pid>` in the shared
// directory while it waits. The last live waiter restores the default
// behavior; the others leave it alone so their downloads keep landing here.
+// A marker is rewritten while its process waits, so one that stopped aging
+// belongs to a process that died and whose pid another process now holds.
function claimSharedBehavior(token: symbol): void {
armedInterceptions.add(token);
+ writeWaiterMarker();
+ if (!waiterRefreshTimer) {
+ waiterRefreshTimer = setInterval(writeWaiterMarker, WAITER_REFRESH_MS);
+ waiterRefreshTimer.unref?.();
+ }
+}
+
+function releaseSharedBehavior(token: symbol): boolean {
+ armedInterceptions.delete(token);
+ if (armedInterceptions.size > 0) return false;
+ stopWaiterMarker();
+ return !hasOtherLiveWaiter();
+}
+
+function writeWaiterMarker(): void {
try {
writeFileSync(
waiterMarkerPath(),
@@ -373,11 +392,10 @@ function claimSharedBehavior(token: symbol): void {
}
}
-function releaseSharedBehavior(token: symbol): boolean {
- armedInterceptions.delete(token);
- if (armedInterceptions.size > 0) return false;
+function stopWaiterMarker(): void {
+ if (waiterRefreshTimer) clearInterval(waiterRefreshTimer);
+ waiterRefreshTimer = undefined;
rmSync(waiterMarkerPath(), { force: true });
- return !hasOtherLiveWaiter();
}
function waiterMarkerPath(): string {
@@ -461,7 +479,7 @@ export function disposeDownloadArtifacts(): void {
activeDownloadTargets.clear();
if (armedInterceptions.size > 0) {
armedInterceptions.clear();
- if (sharedDirectory) rmSync(waiterMarkerPath(), { force: true });
+ if (sharedDirectory) stopWaiterMarker();
}
for (const path of ownedFiles) {
rmSync(path, { force: true });