-
Notifications
You must be signed in to change notification settings - Fork 394
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
Prevent sending response headers if already sent in default error han… #2006
Prevent sending response headers if already sent in default error han… #2006
Conversation
…dler This change addresses a bug where the defaultProcessEventErrorHandler could attempt to send HTTP response headers after they've already been sent, leading to a server error. The fix adds a check for the response.headersSent property before attempting to write headers or end the response. The update ensures that the error handler behaves correctly even if ack() has been called previously during request processing.
Thanks for the contribution! Before we can merge this, we need @suhailgupta03 to sign the Salesforce Inc. Contributor License Agreement. |
Codecov ReportAttention:
Additional details and impacted files@@ Coverage Diff @@
## main #2006 +/- ##
==========================================
- Coverage 82.21% 82.00% -0.22%
==========================================
Files 18 18
Lines 1524 1528 +4
Branches 438 439 +1
==========================================
Hits 1253 1253
- Misses 175 178 +3
- Partials 96 97 +1 ☔ View full report in Codecov by Sentry. |
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.
This is a good catch! Thanks for sending this PR. Could you amend the logging part a little?
src/receivers/HTTPModuleFunctions.ts
Outdated
|
||
// Check if the response headers have already been sent | ||
if (response.headersSent) { | ||
logger.error('Headers already sent, cannot send another response'); |
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.
Can you adjust the logging for consistency with the following logic?
logger.error('Headers already sent, cannot send another response'); | |
logger.error('An unhandled error occurred after ack() called in a listener'); | |
logger.debug(`Error details: ${error}, storedResponse: ${storedResponse}`); |
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.
@seratch Updated.
Also, perhap adding unit tests for this pattern may not be an easy task. It's okay to skip the codecov error this time |
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.
Thank you! Looks good to me 👍
Summary
This pull request introduces a safeguard in the
defaultProcessEventErrorHandler
to prevent attempts to send HTTP response headers after they have already been transmitted. This situation can occur whenack()
is invoked before an error is thrown during event processing, leading to potential conflicts in the response handling.The goal of this PR is to enhance the robustness of the error handling mechanism by ensuring that the server does not encounter
ERR_HTTP_HEADERS_SENT
errors, which can disrupt the normal flow of Slack event processing. By checking theresponse.headersSent
flag before setting headers or ending the response, we maintain the integrity of the HTTP transaction and provide a more reliable experience for developers using Bolt.js.Example Scenario
Consider a custom event handler that acknowledges an event and then performs an asynchronous operation which fails:
Requirements (place an
x
in each[ ]
)