I'm working on a web application using a Node.js based backend with a file upload feature. The routing is handled by express and the file upload itself by express-fileupload. The code base worked perfectly as expected until I recently tried to use cluster in my application to gain performance. I'm encountering this error:
2024-03-06 16:35:26.385 ERROR [5] [main] Error: Unexpected end of form
at Multipart._final (/home/user/repo/backend/node_modules/busboy/lib/types/multipart.js:588:17)
at callFinal (node:internal/streams/writable:696:27)
at prefinish (node:internal/streams/writable:725:7)
at finishMaybe (node:internal/streams/writable:735:5)
at Multipart.Writable.end (node:internal/streams/writable:633:5)
at IncomingMessage.onend (node:internal/streams/readable:693:10)
at Object.onceWrapper (node:events:627:28)
at IncomingMessage.emit (node:events:525:35)
at endReadableNT (node:internal/streams/readable:1358:12)
at processTicksAndRejections (node:internal/process/task_queues:83:21)
I tried to activate the debug logs of express-fileupload:
Express-file-upload: New upload started 0->mini.pdf, bytes:0
Express-file-upload: Uploading 0->mini.pdf, bytes:6607...
Express-file-upload: Error: Unexpected end of form
Express-file-upload: Busboy error
I retried with the old version of my code (without cluster) and got these logs:
Express-file-upload: New upload started 0->mini.pdf, bytes:0
Express-file-upload: Uploading 0->mini.pdf, bytes:6542...
Express-file-upload: Upload finished 0->mini.pdf, bytes:6542
Express-file-upload: Upload 0->mini.pdf completed, bytes:6542.
Express-file-upload: Busboy finished parsing request.
It seems that the file is larger in the first case... Which is very strange, because after checking in Wireshark, the requests coming from the frontend (part of the code that hasn't changed at all) are strictly identical:
Frame 44: 7951 bytes on wire (63608 bits), 7951 bytes captured (63608 bits) on interface lo, id 0
Ethernet II, Src: 00:00:00_00:00:00 (00:00:00:00:00:00), Dst: 00:00:00_00:00:00 (00:00:00:00:00:00)
Internet Protocol Version 4, Src: 127.0.0.1, Dst: 127.0.0.1
Transmission Control Protocol, Src Port: 46566, Dst Port: 8080, Seq: 1, Ack: 1, Len: 7885
Hypertext Transfer Protocol
MIME Multipart Media Encapsulation, Type: multipart/form-data, Boundary: "---------------------------307926514434949316373068219198"
[Type: multipart/form-data]
First boundary: -----------------------------307926514434949316373068219198\r\n
Encapsulated multipart part: (application/pdf)
Content-Disposition: form-data; name="0"; filename="mini.pdf"\r\n
Content-Type: application/pdf\r\n\r\n
Media Type
Media type: application/pdf (6542 bytes)
Last boundary: \r\n-----------------------------307926514434949316373068219198--\r\n
I then tried to replace express-fileupload with multer, but I get exactly the same error.
So I'm wondering if there isn't something to be done in particular to make cluster and express-fileupload work together, as a kind of adapter like it's common to use with socket.io for example?