You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I have an endpoint for accepting huge files, so instead of loading them at once into memory, I choose to consume incoming as stream and process it with busboy. The happy path works properly, but the server crashes when I start to do stream error handling:
import{pipeline}from"stream/promises";import{Busboy}from"@fastify/busboy";import{HttpBindings}from"@hono/node-server";import{Hono}from"hono";constapp=newHono<{Bindings: HttpBindings}>();app.post("/",async(c)=>{const{ incoming, outgoing }=c.env;constbusboy=Busboy({headers: incoming.headers,});busboy.on("file",async(fieldname,file,filename,encoding,mimetype)=>{busboy.emit("error",newError("Something bad happened"));}try{awaitpipeline(incoming,busboy);returnc.html("never reached, as stream/promises/pipeline will throw on error event");}catch(err: Error){outgoing.writeHead(500,{Connection: "close"});outgoing.end(err.message);returnRESPONSE_ALREADY_SENT;})
The whole server crashes with the above code after responding to the client. The error is caused by https://github.com/nodejs/undici/blob/main/lib/web/fetch/body.js#L191, which basically happen when a disturbed stream is being used to initial a request body (cancelled is considered as disturbed in this case). And since those node apis are out of our control, I wonder if we should
wrap newRequestFromIncoming with some try catch logic?
don't initialize the request when RESPONSE_ALREADY_SENT is returned? or at least make req.body empty?
most importantly, we shouldn't let the whole app crash in any case, app.onError should have caught the error.
Thank you!
The text was updated successfully, but these errors were encountered:
@Markyiptw@yusukebe this snippet of code works but when this endpoint is after a middleware which has await operation. For some unknown reason the requested appears streamed and Busboy is unable to read anything
the problem came with incoming, instead await pipeline(c.req.raw.body, busboy); resolve the problem
@Markyiptw@yusukebe this snippet of code works but when this endpoint is after a middleware which has await operation. For some unknown reason the requested appears streamed and Busboy is unable to read anything
the problem came with incoming, instead await pipeline(c.req.raw.body, busboy); resolve the problem
I have an endpoint for accepting huge files, so instead of loading them at once into memory, I choose to consume
incoming
as stream and process it withbusboy
. The happy path works properly, but the server crashes when I start to do stream error handling:The whole server crashes with the above code after responding to the client. The error is caused by https://github.com/nodejs/undici/blob/main/lib/web/fetch/body.js#L191, which basically happen when a disturbed stream is being used to initial a request body (cancelled is considered as disturbed in this case). And since those node apis are out of our control, I wonder if we should
newRequestFromIncoming
with some try catch logic?RESPONSE_ALREADY_SENT
is returned? or at least makereq.body
empty?app.onError
should have caught the error.Thank you!
The text was updated successfully, but these errors were encountered: