Fix REQ abort logic

This commit is contained in:
Alex Gleason 2025-03-07 22:58:55 -06:00
parent 413056e841
commit e085082a76
No known key found for this signature in database
GPG key ID: 7211D1F99744FBB7

View file

@ -101,6 +101,10 @@ function connectStream(socket: WebSocket, ip: string | undefined, opts: ConnectS
// HACK: Due to a bug in Deno, we need to call the close handler manually. // HACK: Due to a bug in Deno, we need to call the close handler manually.
// https://github.com/denoland/deno/issues/27924 // https://github.com/denoland/deno/issues/27924
function closeSocket(code?: number, reason?: string): void { function closeSocket(code?: number, reason?: string): void {
for (const controller of controllers.values()) {
controller.abort();
}
send(['NOTICE', `closed: ${reason} (${code})`]);
socket.close(code, reason); socket.close(code, reason);
handleSocketClose(); handleSocketClose();
} }
@ -152,9 +156,10 @@ function connectStream(socket: WebSocket, ip: string | undefined, opts: ConnectS
const controller = new AbortController(); const controller = new AbortController();
controllers.get(subId)?.abort(); controllers.get(subId)?.abort();
controllers.set(subId, controller); controllers.set(subId, controller);
const signal = controller.signal;
try { try {
for await (const msg of relay.req(filters, { limit: 100, timeout: conf.db.timeouts.relay })) { for await (const msg of relay.req(filters, { limit: 100, signal, timeout: conf.db.timeouts.relay })) {
if (msg[0] === 'EVENT') { if (msg[0] === 'EVENT') {
const [, , event] = msg; const [, , event] = msg;
send(['EVENT', subId, purifyEvent(event)]); send(['EVENT', subId, purifyEvent(event)]);
@ -171,8 +176,8 @@ function connectStream(socket: WebSocket, ip: string | undefined, opts: ConnectS
} else { } else {
send(['CLOSED', subId, 'error: something went wrong']); send(['CLOSED', subId, 'error: something went wrong']);
} }
} finally {
controllers.delete(subId); controllers.delete(subId);
return;
} }
} }