-
-
Notifications
You must be signed in to change notification settings - Fork 289
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: prevent eventstream errors on shutdown or client aborts #5784
Conversation
Performance Report✔️ no performance regression detected Full benchmark results
|
b53faa9
to
8239979
Compare
8239979
to
905c4b9
Compare
@@ -37,9 +43,6 @@ export function getRoutes(config: ChainForkConfig, api: ServerApi<Api>): ServerR | |||
await new Promise<void>((resolve, reject) => { | |||
void api.eventstream(req.query.topics, controller.signal, (event) => { | |||
try { | |||
// If the request is already aborted, we don't need to send any more events. | |||
if (req.raw.destroyed) return; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Removed as it did not resolve the issue (added in #5722)
// 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 abrupt 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 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
hijack()
is not async, see reply.js#L116
905c4b9
to
24843a6
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Seems very tricky, but as per the referenced issue it seems to works.
I researched a bit more to make sure "hijacking" the response ( The most important consideration is about not execution hooks in our case as closing the evenstream api via There are custom hooks that are skipped now, namely All other hooks are called before handler is invoked, meaning CORS headers are still set by plugin and |
🎉 This PR is included in v1.10.0 🎉 |
Motivation
Closes #5783
Description
Before writing to the
.raw
stream it is recommended to call.hijack
, see fastify/fastify#3979 (comment).This prevents Fastify from sending the response and avoids "Error: Cannot set headers after they are sent to the client" errors during shutdown or client aborts.
See mention of
.hijack()
in Lifecycle.md. Further details can be found in Reply.md#hijackThe previous changes done in #5722 and changes of this PR will fix all the errors reported in #5323. We might consider closing this issue as well after checking with user that reported it.