Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: eventstream listen on socket for close/end event #5795

Merged
merged 2 commits into from
Jul 24, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 2 additions & 8 deletions packages/api/src/beacon/server/events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,6 @@ export function getRoutes(config: ChainForkConfig, api: ServerApi<Api>): 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]) => {
Expand Down Expand Up @@ -54,8 +48,8 @@ export function getRoutes(config: ChainForkConfig, api: ServerApi<Api>): 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());
Expand Down
Loading