fix(stream): handle S3 upload errors without crashing the process#220
Merged
fix(stream): handle S3 upload errors without crashing the process#220
Conversation
When an S3 upload fails (e.g., RequestTimeout), the error flows through
toS3()'s .catch() handler which calls toS3Stream.emit("error", err).
If no error listener is attached to that stream, emit("error") throws,
and since the throw happens inside a .catch() handler, it creates a new
unhandled promise rejection that crashes the Lambda runtime.
Three changes:
1. toS3() .catch(): Stop calling emit("error") on the stream. Just
store the error and set the emittedError flag. The error propagates
through the _final callback when the stream is ended.
2. leos3.js newStream(): Add an error listener on the internal s3
upload stream so any error events have a listener and don't throw.
3. leos3.js submitStream(): Listen for both "error" and "finish"
events with a callback guard. Previously only "finish" was handled,
but _final(callback) with a truthy error emits "error" not "finish".
Task: ES-2976
Made-with: Cursor
✅ Snyk checks have passed. No issues have been found so far.
💻 Catch issues earlier using the plugins for VS Code, JetBrains IDEs, Visual Studio, and Eclipse. |
Revert the leo-stream.js change from 9f65041. The original behavior of emitting the error on toS3Stream is correct — the fix in leos3.js adds the missing error listener that catches it. Task: ES-2976
czirker
approved these changes
Apr 2, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
leos3.jssoemit("error")fromtoS3()has a handler and doesn't throwerrorandfinishevents insubmitStream()with a callback guard, since_final(callback)with a truthy error emitserrornotfinishTask Reference
Changes Made
lib/stream/helper/leos3.js— Add.on('error')handler when creating the S3 stream innewStream(), and handle botherror/finishevents insubmitStream()Root Cause
When an S3 upload fails (e.g.,
RequestTimeout),toS3()'s.catch()callstoS3Stream.emit("error", err). Inleos3.js, the internals3 = ls.toS3(...)stream had no error listener, soemit("error")throws (Node.js EventEmitter behavior). Since the throw happens inside a.catch()handler, it creates a new unhandled promise rejection →Runtime.ExitError→ Lambda crash.Testing