Skip to content

Commit 1cc0e16

Browse files
ochafikclaude
andcommitted
fix(basic-host): be resilient to individual server connection failures
Use Promise.allSettled instead of Promise.all when connecting to servers, so that a single server failure doesn't crash the entire UI. Failed connections are logged as warnings but the UI continues with the servers that connected successfully. Also fixes video-resource-server missing server-utils.ts. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
1 parent f99592f commit 1cc0e16

2 files changed

Lines changed: 25 additions & 2 deletions

File tree

examples/basic-host/src/index.tsx

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -350,7 +350,27 @@ class ErrorBoundary extends Component<ErrorBoundaryProps, ErrorBoundaryState> {
350350
async function connectToAllServers(): Promise<ServerInfo[]> {
351351
const serverUrlsResponse = await fetch("/api/servers");
352352
const serverUrls = (await serverUrlsResponse.json()) as string[];
353-
return Promise.all(serverUrls.map((url) => connectToServer(new URL(url))));
353+
354+
// Use allSettled to be resilient to individual server failures
355+
const results = await Promise.allSettled(
356+
serverUrls.map((url) => connectToServer(new URL(url)))
357+
);
358+
359+
const servers: ServerInfo[] = [];
360+
for (let i = 0; i < results.length; i++) {
361+
const result = results[i];
362+
if (result.status === "fulfilled") {
363+
servers.push(result.value);
364+
} else {
365+
console.warn(`[HOST] Failed to connect to ${serverUrls[i]}:`, result.reason);
366+
}
367+
}
368+
369+
if (servers.length === 0 && serverUrls.length > 0) {
370+
throw new Error(`Failed to connect to any servers (${serverUrls.length} attempted)`);
371+
}
372+
373+
return servers;
354374
}
355375

356376
createRoot(document.getElementById("root")!).render(

src/app.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1027,7 +1027,10 @@ export class App extends Protocol<AppRequest, AppNotification, AppResult> {
10271027
* @see {@link PostMessageTransport} for the typical transport implementation
10281028
*/
10291029
override async connect(
1030-
transport: Transport = new PostMessageTransport(window.parent, window.parent),
1030+
transport: Transport = new PostMessageTransport(
1031+
window.parent,
1032+
window.parent,
1033+
),
10311034
options?: RequestOptions,
10321035
): Promise<void> {
10331036
await super.connect(transport);

0 commit comments

Comments
 (0)