Skip to content

Commit 42c14c8

Browse files
committed
Fix Hub adapter: await wsPromise so gateway doesn't restart, 5s startup delay, .catch for safety
1 parent 3622e68 commit 42c14c8

File tree

2 files changed

+19
-5
lines changed

2 files changed

+19
-5
lines changed

extensions/hub/src/monitor.ts

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,9 @@ export async function monitorHubProvider(opts: HubMonitorOptions): Promise<{ sto
4242
? AbortSignal.any([opts.abortSignal, ac.signal])
4343
: ac.signal;
4444

45-
connectHubWebSocket({
45+
console.error("[HUB-MONITOR-DEBUG] calling connectHubWebSocket");
46+
// Block until abort signal — gateway expects startAccount to stay alive
47+
const wsPromise = connectHubWebSocket({
4648
url: account.url,
4749
agentId: account.agentId,
4850
secret: account.secret,
@@ -97,9 +99,11 @@ export async function monitorHubProvider(opts: HubMonitorOptions): Promise<{ sto
9799
`[${account.accountId}] started Hub provider (${account.url}, agent=${account.agentId})`,
98100
);
99101

100-
return {
101-
stop: () => {
102-
ac.abort();
103-
},
102+
// Return stop handle but keep the promise chain alive
103+
// Gateway expects startAccount to stay running — awaiting wsPromise blocks until abort
104+
const stopFn = () => {
105+
ac.abort();
104106
};
107+
await wsPromise;
108+
return { stop: stopFn };
105109
}

extensions/hub/src/ws.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,19 @@ export async function connectHubWebSocket(opts: WsHubOptions): Promise<void> {
2727
const { url, agentId, secret, abortSignal, onMessages, onError, onConnected } = opts;
2828
let attempt = 0;
2929

30+
// Initial delay on first attempt to allow Hub server to start
31+
if (attempt === 0) {
32+
try {
33+
await setTimeout(5_000, undefined, { signal: abortSignal });
34+
} catch {
35+
return;
36+
}
37+
}
38+
3039
while (!abortSignal?.aborted) {
3140
try {
3241
const wsUrl = `${httpToWs(url)}/agents/${encodeURIComponent(agentId)}/ws`;
42+
console.error(`[HUB-WS-DEBUG] connecting to ${wsUrl}`);
3343
const ws = new WebSocket(wsUrl);
3444

3545
await new Promise<void>((resolve, reject) => {

0 commit comments

Comments
 (0)