From 6b8c24228228f6a1eb49d5fe082388b5dc2034eb Mon Sep 17 00:00:00 2001 From: Kai Date: Tue, 14 Apr 2026 12:04:54 +0200 Subject: [PATCH] fix(nip42): catch SendingOnClosedConnection during automatic AUTH Fixes #531. When automaticallyAuth is configured and a relay closes the connection between sending the AUTH challenge and the client sending the signed response, the SendingOnClosedConnection error was propagating as an unhandled promise rejection. This is a race condition that commonly occurs with relays that enforce WoT or whitelists - they accept the WebSocket, send an AUTH challenge, but close the connection before the client can respond. The fix catches the SendingOnClosedConnection error specifically in the AUTH case handler, preventing unhandled rejections while still propagating other errors. --- abstract-relay.ts | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/abstract-relay.ts b/abstract-relay.ts index 677b142..a88f7d7 100644 --- a/abstract-relay.ts +++ b/abstract-relay.ts @@ -476,7 +476,14 @@ export class AbstractRelay { case 'AUTH': { this.challenge = data[1] as string if (this.onauth) { - this.auth(this.onauth) + this.auth(this.onauth).catch(err => { + // If the connection closed before auth could be sent, just ignore it. + // This is a race condition when relays close connections quickly + // (e.g., WoT-enforced relays that reject unknown pubkeys). + if (!(err instanceof SendingOnClosedConnection)) { + throw err // re-throw other errors + } + }) } return }