Skip to content

api: unawaited send() calls in crawl-status-ws polling loop cause unhandledRejection — crashes Node 15+ process on socket close #4243

Open

Description

@harsh4vardhan

Summary

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.

Affected code

apps/api/src/controllers/v1/crawl-status-ws.ts lines 97–106

for (const job of newlyDoneJobs) {
  if (job.returnvalue) {
    send(ws, {          // ← Promise returned by send() is discarded
      type: "document",
      data: job.returnvalue,
    });
  }
}

send() is defined as:

async function send(ws: WebSocket, data: object) {
  if (ws.readyState !== 1) return;   // OPEN check
  return new Promise<null>((resolve, reject) => {
    ws.send(JSON.stringify(data), (err) => {
      if (err) reject(err);           // ← rejection escapes if Promise not awaited
      else resolve(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

  1. The polling loop fetches 10 newly-completed jobs.
  2. It starts iterating newlyDoneJobs, calling send() fire-and-forget for each.
  3. After document 3 is sent, the client's TCP connection drops.
  4. 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.
  5. Each rejected Promise has no .catch() attached → 7 unhandledRejection events.
  6. On Node.js 15+, the first one terminates the process. All running scrapes are aborted.

Relationship to #4242

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.

Proposed fix

Await each send() call and catch errors inline:

for (const job of newlyDoneJobs) {
  if (job.returnvalue) {
    await send(ws, { type: "document", data: job.returnvalue }).catch(() => {});
  }
}

Or make send() itself swallow the error (since a failed send to a closed socket is not actionable):

async function send(ws: WebSocket, data: object) {
  if (ws.readyState !== 1) return;
  return new Promise<null>((resolve) => {   // always resolve
    ws.send(JSON.stringify(data), (err) => {
      if (err) { /* log if desired */ }
      resolve(null);
    });
  });
}

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions