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 bug where sse response hangs if handler threw an error. #1668

Merged
merged 5 commits into from
Jan 28, 2025
Merged

Conversation

taeold
Copy link
Contributor

@taeold taeold commented Jan 25, 2025

When callable function handler throws an error, the existing implementation doesn't capture the error. Instead the error is throw, killing the process:

>  {"severity":"ERROR","message":"Unhandled error ReferenceError: datat is not defined\n    at /Users/[REDACTED_USER_NAME]/google/cf3-streaming-demo/functions/lib/index.js:90:9\n    at async /Users/[REDACTED_USER_NAME]/google/cf3-streaming-demo/functions/nod
e_modules/firebase-functions/lib/common/providers/https.js:544:26\n    at async runFunction (/Users/[REDACTED_USER_NAME]/google/firebase-tools/lib/emulator/functionsEmulatorRuntime.js:506:9)\n    at async runHTTPS (/Users/[REDACTED_USER_NAME]/google/firebase
-tools/lib/emulator/functionsEmulatorRuntime.js:531:5)\n    at async /Users/[REDACTED_USER_NAME]/google/firebase-tools/lib/emulator/functionsEmulatorRuntime.js:694:21"}
⚠  functions: Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client
    at new NodeError (node:internal/errors:405:5)
    at ServerResponse.setHeader (node:_http_outgoing:648:11)
    at ServerResponse.header (/node_modules/express/lib/response.js:795:10)
    at ServerResponse.contentType (/node_modules/express/lib/response.js:625:15)
    at ServerResponse.send (/Users/[REDACTED_USER_NAME]/google/firebase-tools/node_modules/express/lib/response.js:150:14)
    at /Users/[REDACTED_USER_NAME]/google/cf3-streaming-demo/functions/node_modules/firebase-functions/lib/common/providers/https.js:575:25
    at async runFunction (/Users/[REDACTED_USER_NAME]/google/firebase-tools/lib/emulator/functionsEmulatorRuntime.js:506:9)
    at async runHTTPS (/Users/[REDACTED_USER_NAME]/google/firebase-tools/lib/emulator/functionsEmulatorRuntime.js:531:5)
    at async /Users/[REDACTED_USER_NAME]/google/firebase-tools/lib/emulator/functionsEmulatorRuntime.js:694:21
⚠  Your function was killed because it raised an unhandled error.
i  functions: Finished "us-central1-genStreamError" in 5044.977417ms

I needed to make some changes to the MockResponse class since it incorrectly implemented how Response object actually works for node.js.

spec/helper.ts Outdated
const body =
typeof this.sentBody === "undefined"
? toSend
: this.sentBody + ((toSend as string) || "");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: consider String(toSend || "") if you want to both silence errors and have defined behavior at runtime

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done.

@taeold taeold enabled auto-merge January 28, 2025 15:44
@taeold taeold added this pull request to the merge queue Jan 28, 2025
Merged via the queue into master with commit 8f46a4f Jan 28, 2025
16 of 17 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants