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
{{ message }}
Repository navigation
api: unawaited send() calls in crawl-status-ws polling loop cause unhandledRejection — crashes Node 15+ process on socket close #4243
Inside the loop() of crawl-status-ws.ts, send() is called for each newly-completed job but its returned Promise is never awaited or .catch()-ed. send() wraps ws.send() in a Promise. If the WebSocket closes between when the loop calls send() and when ws.send()'s internal callback fires, the callback receives an error, the Promise rejects, and — because no caller awaited or caught it — Node.js emits unhandledRejection. In Node.js 15+, the default unhandledRejection handler terminates the process, taking down all in-flight scrapes.
for(constjobofnewlyDoneJobs){if(job.returnvalue){send(ws,{// ← Promise returned by send() is discardedtype: "document",data: job.returnvalue,});}}
send() is defined as:
asyncfunctionsend(ws: WebSocket,data: object){if(ws.readyState!==1)return;// OPEN checkreturnnewPromise<null>((resolve,reject)=>{ws.send(JSON.stringify(data),(err)=>{if(err)reject(err);// ← rejection escapes if Promise not awaitedelseresolve(null);});});}
The readyState check races with the actual send: the socket can transition from OPEN to CLOSING between the check and the ws.send() call.
Failure scenario
The polling loop fetches 10 newly-completed jobs.
It starts iterating newlyDoneJobs, calling send() fire-and-forget for each.
After document 3 is sent, the client's TCP connection drops.
Documents 4–10: ws.readyState is still 1 at the time of the check (transition hasn't propagated), so ws.send() is called. Its async callback receives an error for each.
Each rejected Promise has no .catch() attached → 7 unhandledRejection events.
On Node.js 15+, the first one terminates the process. All running scrapes are aborted.
This bug compounds the missing ws.on('close') handler (#4242): even if the polling loop is fixed to stop on disconnect, the race window between the readyState check and the ws.send() callback means send() rejections can still escape during normal operation.
Activity