serverless-express icon indicating copy to clipboard operation
serverless-express copied to clipboard

bug: write method is not returning a boolean value

Open realityfilter opened this issue 1 year ago • 1 comments

The implementation of the write method is missing a boolean return value indicating the need of drain https://github.com/CodeGenieApp/serverless-express/blob/mainline/src/response.js.

See https://nodejs.org/api/stream.html#writablewritechunk-encoding-callback

'The return value is true if the internal buffer is less than the highWaterMark configured when the stream was created after admitting chunk. If false is returned, further attempts to write data to the stream should stop until the 'drain' event is emitted.'

With newer versions of trpc (11.0.0-rc.419) this leads to a hang. The trpc middleware interprets void as false and waits for the 'drain' event that is never showing up. The lambda runtime is shutting down the event handling with an internal server error.

See https://github.com/trpc/trpc/blob/fbb4a2fb4b71d2f5ab7ee97617d951f3a92517c8/packages/server/src/adapters/node-http/nodeHTTPRequestHandler.ts#L85

In response.js the write method should return true.

See https://github.com/realityfilter/serverless-express-write-bugreport for a test case.

realityfilter avatar Jul 04 '24 09:07 realityfilter

This might be enough? https://github.com/KATT/serverless-express/commit/9ba260ec7148d81a7caf2db84e62a1ba38b61925

KATT avatar Jul 04 '24 11:07 KATT

Ran into this problem when packaging SvelteKit with adapter-node and polka, the effect confusingly caused the Lambda to return status code 200 with null as the body.

I can confirm that @KATT's fix provided does work, thanks!

jimdigriz avatar Jul 03 '25 16:07 jimdigriz

Ran into this problem when trying to upgrade to Next.js 14.0.0. It also interprets void as falsey and waits for the stream 'drain' event that never happens. Patching my serverless-express version with this fix, solved the problem 👍

joshlperry avatar Jul 10 '25 15:07 joshlperry

@brettstack Could we get @KATT's fix in?

joshlperry avatar Jul 10 '25 15:07 joshlperry

If someone wants to submit this as a PR I can merge it in @joshlperry @KATT

brettstack avatar Jul 11 '25 06:07 brettstack

@brettstack I tried to make a PR originally but I lacked permissions and couldn't push up to create a remote branch, did I miss something? Happy to create a PR

joshlperry avatar Jul 17 '25 13:07 joshlperry