From 5ee1fc1b7fc8eaf07cc8ad5f990e388fb0cf77e5 Mon Sep 17 00:00:00 2001 From: Alex Gleason Date: Fri, 28 Mar 2025 17:50:51 -0500 Subject: [PATCH] Fix relay always sending a CLOSED message after the client sends CLOSE --- packages/ditto/controllers/nostr/relay.ts | 4 +++- packages/ditto/storages/DittoPgStore.ts | 4 ++-- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/packages/ditto/controllers/nostr/relay.ts b/packages/ditto/controllers/nostr/relay.ts index bbec9b5c..af233c86 100644 --- a/packages/ditto/controllers/nostr/relay.ts +++ b/packages/ditto/controllers/nostr/relay.ts @@ -165,6 +165,8 @@ function connectStream(socket: WebSocket, ip: string | undefined, opts: ConnectS try { for await (const msg of relay.req(filters, { limit: 100, signal, timeout: conf.db.timeouts.relay })) { + if (!controllers.has(subId)) break; + if (msg[0] === 'EVENT') { const [, , event] = msg; send(['EVENT', subId, purifyEvent(event)]); @@ -186,8 +188,8 @@ function connectStream(socket: WebSocket, ip: string | undefined, opts: ConnectS send(['CLOSED', subId, 'error: something went wrong']); } } finally { - controllers.get(subId)?.abort(); controllers.delete(subId); + controller.abort(); } } diff --git a/packages/ditto/storages/DittoPgStore.ts b/packages/ditto/storages/DittoPgStore.ts index e8a969be..a1499f1d 100644 --- a/packages/ditto/storages/DittoPgStore.ts +++ b/packages/ditto/storages/DittoPgStore.ts @@ -324,7 +324,7 @@ export class DittoPgStore extends NPostgres { machina.push(['EOSE', subId]); }).catch((error) => { - if (error instanceof Error && error.message.includes('timeout')) { + if (error instanceof Error && (error.name === 'TimeoutError' || error.message.includes('timeout'))) { machina.push(['CLOSED', subId, 'error: the relay could not respond fast enough']); } else { machina.push(['CLOSED', subId, 'error: something went wrong']); @@ -361,7 +361,7 @@ export class DittoPgStore extends NPostgres { yield msg; } } catch (e) { - if (e instanceof Error && e.name === 'AbortError') { + if (e instanceof Error && (e.name === 'TimeoutError' || e.message.includes('timeout'))) { yield ['CLOSED', subId, 'error: the relay could not respond fast enough']; } else { yield ['CLOSED', subId, 'error: something went wrong'];