'Randomly bad request Nodejs and timeout error client side

Some of our users getting randomly aborted API request. With the exact same API endpoint URL and Payload, I never have this issue.

I think it is network related. But I've no clue anymore. So this is the scenario how it might be reproduced:

  1. User goes to the web app via mobile web view.
  2. User changes network (from WIFI to 4g hotspot for example)
  3. Doing the API request
  4. The server receives the request in the app.all part. I can see the req.url, which is correct.
  5. But it doesn't go to the app.post part.
  6. After several seconds (30s) Nodejs prompts BadRequest OnAborted.
  7. The client-side XHR goes to the error function and prompts Timeout.
  8. After refresh, it works again if they retry.

The bad Request error:

BadRequestError: request aborted
03:15:46 9|Middleware       |     at IncomingMessage.onAborted (/home/server/node_modules/raw-body/index.js:231:10)
03:15:46 9|Middleware       |     at IncomingMessage.emit (events.js:193:13)
03:15:46 9|Middleware       |     at abortIncoming (_http_server.js:447:9)
03:15:46 9|Middleware       |     at socketOnClose (_http_server.js:440:3)
03:15:46 9|Middleware       |     at Socket.emit (events.js:198:15)
03:15:46 9|Middleware       |     at TCP._handle.close (net.js:614:12)

But if I do this on my laptop, with good stable WIFI, I cannot reproduce this. I never get this error and the endpoint works properly. Even if I switch from WIFI to Hotspot WIFI, I still cannot reproduce this.

Additionally, some users with laptop and WIFI having this same problem SOMETIMES.

Research

This is some information I found about this Abort:

This error will occur when the request is 
aborted by the client before reading the body has finished.
 
The received property will be set to the number of 
bytes received before the request was aborted 
and the expected property is set to the number 
of expected bytes. 

The status property is set to 400 and type 
property is set to 'request.aborted'.

Network

So at multiple locations on different networks, we've found this in the modem log when the error occures:

27  2.295724  <IP>  <IP>  TCP  1482  [TCP Out-Of-Order] 4000 → 59995 [ACK] Seq=5713 Ack=1 Win=501 Len=1428 [TCP segment of a reassembled PDU]

When I look this up TCP is packet loss, delivery in wrong order or by other network paths.

And about the TCP segment of a ressembled PDU:

The "TCP segment of a reassembled PDU" message means that some protocol on top of TCP sent a PDU to the TCP layer which the TCP layer was not able to send to the IP layer in one segment (which has a maximum size called the maximum segment size or in short MSS).



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source