Skip to content

Open-source AI teammates in Slack, Teams, Discord, Telegram and Mattermost. Self-host it, with any agent harness and any model.

Retry reading another host's ownership ticket on Windows (#1040)

Abel Dantasfb8b055575ab
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 09f905634..edc22e759 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -2072,6 +2072,10 @@ version of their own to them without also giving them a release of their own.
agent"; it was the only place Console showed that name.
#### Fixed
+- **Two agent hosts starting at once on Windows no longer crash each other.**
+ A host reading another host's ownership ticket while that host replaced or
+ deleted it got `EPERM` from Windows and exited. It now tries the read again
+ for a few seconds, as it already did for its own writes.
- **Settings → Agent providers keeps this computer's report current.** It
prefers the report from the server Console is on, rereads it while open and
after the controller reports again, and the install section says so when
diff --git a/console/packages/agent-providers/src/host/ownership-lock.ts b/console/packages/agent-providers/src/host/ownership-lock.ts
index b70896d11..43e608d08 100644
--- a/console/packages/agent-providers/src/host/ownership-lock.ts
+++ b/console/packages/agent-providers/src/host/ownership-lock.ts
@@ -31,9 +31,10 @@ function recordMode(): number {
/**
* `replaceFile` from `replace-file`, for any operation: on Windows a rename
* or link onto a record another contender has open is refused while it stays
- * open, so it is tried again for a few seconds.
+ * open, and so is opening a record another contender is replacing or
+ * deleting, so it is tried again for a few seconds.
*/
-async function waitingOutSharing(operation: () => Promise<void>): Promise<void> {
+async function waitingOutSharing<T>(operation: () => Promise<T>): Promise<T> {
const deadline = Date.now() + 5_000;
for (let wait = 10; ; wait = Math.min(wait * 2, 200)) {
try {
@@ -93,10 +94,8 @@ export async function withOwnershipLock<T>(root: string, action: () => Promise<T
if (!Number.isSafeInteger(pid) || pid <= 0) throw new Error('Invalid ownership ticket PID.');
if (!alive(pid)) continue;
try {
- result.push({
- id: name,
- value: ticketSchema.parse(JSON.parse(await readFile(join(directory, name), 'utf8'))),
- });
+ const text = await waitingOutSharing(() => readFile(join(directory, name), 'utf8'));
+ result.push({ id: name, value: ticketSchema.parse(JSON.parse(text)) });
} catch (error) {
if ((error as NodeJS.ErrnoException).code !== 'ENOENT') throw error;
}