Skip to content

Open-source AI coworkers that each get a computer of their own: a browser, files and tools, with every action decided before it happens and recorded after. Bring any AG-UI agent.

fix(server): give a host stop the same clock as a host cancel (#738)

ANIRUDDHA ADAKcd888fbc0f3c
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 3e8e0745..df58c187 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -8,6 +8,14 @@ Newest first. `Unreleased` is what is on `main` and not yet tagged.
## Unreleased
+- Pressing Stop now stops the computer even when the desktop worker never collects the instruction.
+ A stop was queued with no timeout and the worker was the only thing that could ever remove it, so a
+ worker that stopped polling left the entry in memory for the life of the process and the Host
+ access panel showed an operation that could never finish, growing by one per press. Pressing Stop a
+ second time also withdrew the first Stop before the desktop could collect it, and rejected a request
+ that nobody was waiting for, which the server treats as fatal. A desktop that reconnects within the
+ operation timeout is still told to stop.
+
### A computer refuses the cloud metadata address written through the NAT64 prefix
A Bot's computer is now refused the cloud metadata address when it is written through the NAT64
diff --git a/server/src/host-access/broker.ts b/server/src/host-access/broker.ts
index 76a0f0c3..e457657d 100644
--- a/server/src/host-access/broker.ts
+++ b/server/src/host-access/broker.ts
@@ -106,6 +106,38 @@ export function createHostAccessBroker(
operations.set(cancelOperation.operationId, cancelState);
}
+ function queueStopFor(actorId: string) {
+ const stopOperation: HostAccessDesktopOperation = {
+ operationId: randomUUID(),
+ kind: "stop",
+ botId: "*",
+ actorId,
+ };
+ const stopState: OperationState = {
+ operation: stopOperation,
+ actorId,
+ botId: stopOperation.botId,
+ leasedUntil: null,
+ expiresAt: now() + operationTtlMs,
+ expiryTimer: null,
+ resolve: () => {},
+ reject: () => {},
+ settled: false,
+ };
+ // A stop is addressed to a desktop that may never come back for it, for the same reason a
+ // cancel is: nothing but `resolveDesktopOperation` ever removed this entry, so a worker that
+ // never polled left it here for the life of the process, growing `operations` by one per press
+ // of Stop and leaving `statusFor` reporting it to the person as pending for ever. Deleted rather
+ // than failed, because a stop has nobody left to reject: it used to be built by `enqueue` and
+ // discarded with `void`, so it carried a promise nobody held, and a second Stop failed it
+ // through that promise with no handler attached.
+ stopState.expiryTimer = setTimeout(() => {
+ operations.delete(stopOperation.operationId);
+ }, operationTtlMs);
+ unrefTimer(stopState.expiryTimer);
+ operations.set(stopOperation.operationId, stopState);
+ }
+
function failOperation(state: OperationState, reason: string) {
if (state.settled) return;
state.settled = true;
@@ -385,17 +417,14 @@ export function createHostAccessBroker(
}
const affected = [...operations.values()].filter(
(state) =>
- state.actorId === actorId && state.operation.kind !== "cancel",
+ state.actorId === actorId &&
+ state.operation.kind !== "cancel" &&
+ state.operation.kind !== "stop",
);
for (const state of affected) {
failOperation(state, "Host access was stopped.");
}
- void enqueue({
- operationId: randomUUID(),
- kind: "stop",
- botId: "*",
- actorId,
- });
+ queueStopFor(actorId);
},
statusFor(actorId: string): HostAccessStatus {
diff --git a/server/tests/host-access-broker.test.ts b/server/tests/host-access-broker.test.ts
index 2c0bfb4d..43767f76 100644
--- a/server/tests/host-access-broker.test.ts
+++ b/server/tests/host-access-broker.test.ts
@@ -398,4 +398,60 @@ describe("host access broker", () => {
await new Promise((resolve) => setTimeout(resolve, 150));
expect(broker.statusFor("user-a").pending).toEqual([]);
}, 10_000);
+
+ test("a stop nobody collects does not stay pending for the life of the process", async () => {
+ const broker = createHostAccessBroker(Date.now, {
+ // Long enough that the lease is not what ends the operation, so this is the operation's
+ // own timeout giving up on the stop.
+ desktopLeaseMs: 60_000,
+ operationTtlMs: 40,
+ });
+
+ broker.stop("user-a");
+ expect(broker.nextDesktopOperation()?.operations[0]).toMatchObject({
+ kind: "stop",
+ actorId: "user-a",
+ });
+
+ // That desktop never comes back for it. A stop is addressed to a worker that may never
+ // answer, exactly as a cancel is, so it cannot outlive the chance to deliver it: nothing else
+ // ever removed this entry, so `operations` grew by one per press of Stop and `statusFor` kept
+ // reporting it to the person as pending.
+ await new Promise((resolve) => setTimeout(resolve, 150));
+ expect(broker.statusFor("user-a").pending).toEqual([]);
+ }, 10_000);
+
+ test("a second Stop neither drops the queued stop nor rejects a promise nobody holds", async () => {
+ const broker = createHostAccessBroker(Date.now, {
+ desktopLeaseMs: 60_000,
+ operationTtlMs: 1_000,
+ });
+
+ const unhandled: unknown[] = [];
+ const onUnhandled = (reason: unknown) => {
+ unhandled.push(reason);
+ };
+ process.on("unhandledRejection", onUnhandled);
+ try {
+ broker.stop("user-a");
+ broker.stop("user-a");
+
+ // Both stops are still queued for a desktop that has not collected either. The second must
+ // not fail the first: a stop is the instruction that makes the desktop stop, so failing it
+ // would withdraw the instruction the person just asked for.
+ const pending = broker.statusFor("user-a").pending;
+ expect(pending).toHaveLength(2);
+ expect(pending.every((operation) => operation.kind === "stop")).toBe(
+ true,
+ );
+
+ // And nothing rejects. The stop used to be built by `enqueue` and discarded with `void`, so
+ // the promise behind it had no handler attached, and failing it rejected that promise into
+ // the void -- which this process treats as fatal.
+ await new Promise((resolve) => setTimeout(resolve, 50));
+ expect(unhandled).toEqual([]);
+ } finally {
+ process.off("unhandledRejection", onUnhandled);
+ }
+ }, 10_000);
});