[BUG] Cancelling a response body stream returned from handleRequest
results in an uncaught exception
#2436
Labels
bug
Something isn't working
Describe the bug
When a platform plugin calls
handleRequest
, Hydrogen returns aResponse
object. This response object has a readable stream property (resp.body
). If this stream is cancelled, for example because the user has preemptively hung up the connection, Hydrogen will crash the process with an uncaught promise rejection error.To Reproduce
I was going to write you an integration test to demonstrate the issue, but I realized you don't have integration tests for
handleRequest
. As such, I don't know how to best write a reproduction for you here.If there is a file with
handleRequest
tests that I can modify, please point me at it.Expected behaviour
I expect the process to not crash with an uncaught rejection.
Root cause
I have not thoroughly investigated, but I am about 90% confident you are incorrectly using a
WritableStream
somewhere in Hydrogen (this is where this class of errors most often comes from). What is likely happening is that you are creating aTransformStream
, and picking off thereadable
andwritable
end of this stream, passing thereadable
end as thenew Response
body. You are then callingwritable.getWriter()
, and are then callingwriter.write()
without handling the promise returned fromwriter.write()
. You need to handle promise rejections here! Because you are not doing this, there is now a floating promise that upon rejection takes down the entire process. A cursory grep of the repo suggests that the culprits may be one or all of these writes:I suggest you either await these writes, or add catch handlers to their returned promises.
The text was updated successfully, but these errors were encountered: