From d2b7815dc0debbcb0ad055487f555a8070ecd5d0 Mon Sep 17 00:00:00 2001 From: Nico Flaig Date: Mon, 24 Jul 2023 11:26:51 +0200 Subject: [PATCH 1/2] Remove res.hijack() --- packages/api/src/beacon/server/events.ts | 6 ------ 1 file changed, 6 deletions(-) diff --git a/packages/api/src/beacon/server/events.ts b/packages/api/src/beacon/server/events.ts index fa8b857f8779..4eb90fc9562d 100644 --- a/packages/api/src/beacon/server/events.ts +++ b/packages/api/src/beacon/server/events.ts @@ -18,12 +18,6 @@ export function getRoutes(config: ChainForkConfig, api: ServerApi): ServerR const controller = new AbortController(); try { - // Prevent Fastify from sending the response, this is recommended before writing to the `.raw` stream - // and avoids "Cannot set headers after they are sent to the client" errors during shutdown or client aborts. - // See https://github.com/fastify/fastify/issues/3979, https://github.com/ChainSafe/lodestar/issues/5783 - // eslint-disable-next-line @typescript-eslint/no-floating-promises - res.hijack(); - // Add injected headers from other plugins. This is required for fastify-cors for example // From: https://github.com/NodeFactoryIo/fastify-sse-v2/blob/b1686a979fbf655fb9936c0560294a0c094734d4/src/plugin.ts Object.entries(res.getHeaders()).forEach(([key, value]) => { From 2b22d0a9ea546a7f11763725ec0b34ee321d5964 Mon Sep 17 00:00:00 2001 From: Nico Flaig Date: Mon, 24 Jul 2023 11:27:53 +0200 Subject: [PATCH 2/2] Listen on socket for close/end events --- packages/api/src/beacon/server/events.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/api/src/beacon/server/events.ts b/packages/api/src/beacon/server/events.ts index 4eb90fc9562d..3e45b104ecb5 100644 --- a/packages/api/src/beacon/server/events.ts +++ b/packages/api/src/beacon/server/events.ts @@ -48,8 +48,8 @@ export function getRoutes(config: ChainForkConfig, api: ServerApi): ServerR // In that case the BeaconNode class will call server.close() and end this connection. // The client may disconnect and we need to clean the subscriptions. - req.raw.once("close", () => resolve()); - req.raw.once("end", () => resolve()); + req.socket.once("close", () => resolve()); + req.socket.once("end", () => resolve()); req.raw.once("error", (err) => { if ((err as unknown as {code: string}).code === "ECONNRESET") { return reject(new ErrorAborted());