fix(node): soft error when response body is already locked#158
fix(node): soft error when response body is already locked#158
Conversation
📝 WalkthroughWalkthroughRefactors error handling in the streamBody function by adding an outer try/catch for synchronous errors, introducing nested error handling inside streamHandle to route errors to streamCancel, and explicitly managing error paths while preserving existing stream reading and response writing logic. Changes
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~20 minutes Poem
🚥 Pre-merge checks | ✅ 2 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (2 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing touches
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
commit: |
There was a problem hiding this comment.
Actionable comments posted: 0
🧹 Nitpick comments (1)
src/adapters/_node/send.ts (1)
123-124: Consider narrowing the catch scope to avoid hiding unrelated bugs.The empty catch block will swallow all synchronous errors, not just the intended locked stream errors. While this prevents crashes from mid-stream closures, it also masks potential bugs during development.
Consider filtering for the specific error types that are safe to ignore:
♻️ Suggested refinement
- // eslint-disable-next-line no-empty - } catch {} + } catch (error) { + // Ignore errors from locked/closed streams (e.g., browser closed mid-stream) + // Re-throw unexpected errors to avoid hiding bugs + if ( + error instanceof TypeError && + error.message.includes("locked") + ) { + return; + } + throw error; + }Alternatively, if all errors in this path are truly safe to ignore, at least add a brief comment explaining why:
- // eslint-disable-next-line no-empty - } catch {} + } catch { + // Errors here are expected when client disconnects mid-stream; safe to ignore + }
📜 Review details
Configuration used: Organization UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (1)
src/adapters/_node/send.ts
🧰 Additional context used
🧬 Code graph analysis (1)
src/adapters/_node/send.ts (1)
test/_fixture.ts (1)
error(44-49)
🔇 Additional comments (3)
src/adapters/_node/send.ts (3)
73-88: LGTM!The early destruction check and
streamCancelfunction are well-structured. The guard prevents unnecessary work when the response is already destroyed, and the cleanup logic properly handles both the reader and response.
90-110: LGTM!The nested try/catch properly routes synchronous errors to
streamCancel, and the drain handling for backpressure is correctly implemented.
112-122: LGTM!Event listeners are properly registered and cleaned up in
finally, ensuring no memory leaks. The promise-based completion handling is correct.
|
Can you please add a test to current fixture? Also i think we could reduce scope of |
|
closing until sufficient reproduction is found |
If the stream gets closed at some point (like if the browser is closed mid-stream) it can throw unhandled errors, which can crash NodeJS. ignoring these errors is completely fine and doesn't cause hidden consequences. its only a problem in the nodejs adapter and doesn't happen in deno/bun
i simply added a try catch to the whole
streamBodyfunction. the rest is just formatting differencesSummary by CodeRabbit
✏️ Tip: You can customize this high-level summary in your review settings.