Skip to content

fix: handle graceful shutdown in safeSend to prevent worker crashes#1109

Open
haroldfabla2-hue wants to merge 1 commit intolivekit:mainfrom
haroldfabla2-hue:fix/safeSend-graceful-shutdown
Open

fix: handle graceful shutdown in safeSend to prevent worker crashes#1109
haroldfabla2-hue wants to merge 1 commit intolivekit:mainfrom
haroldfabla2-hue:fix/safeSend-graceful-shutdown

Conversation

@haroldfabla2-hue
Copy link

Summary

Fixes issue #1108: IPC safeSend should not throw unhandled errors on channel close during shutdown.

Problem

In src/ipc/job_proc_lazy_main.ts, the safeSend() function throws unhandled errors when the message channel closes during normal shutdown sequences. This occurs in production when:

  1. Agent job completes and initiates graceful shutdown
  2. Parent process closes the IPC channel
  3. Worker still attempts to send status updates via safeSend()
  4. Unhandled error crashes the worker process before cleanup completes

Solution

Wrap process.send() in a try-catch block that:

  • Returns false instead of throwing when channel is closed (expected during shutdown)
  • Logs at debug level for expected closures (not error level)
  • Only throws for unexpected errors (e.g., serialization errors)

This allows the job completion flow in src/ipc/job_proc_lazy_main.ts to continue gracefully even if the parent has already closed the channel.

Changes

  • Modified safeSend() function in agents/src/ipc/job_proc_lazy_main.ts
  • Added try-catch wrapper around process.send()
  • Added debug logging for channel close errors

Testing

The fix allows workers to exit cleanly without throwing when the IPC channel is already closed during shutdown sequence. Related issues: #1080, #1089.

- Wrap process.send in try-catch to handle channel close errors
- Return false instead of throwing when channel is closed (expected during shutdown)
- Log at debug level for expected closures, preserving for unexpected errors
- Allows job completion flow to continue gracefully

Fixes: livekit#1108, livekit#1080
@changeset-bot
Copy link

changeset-bot bot commented Mar 7, 2026

⚠️ No Changeset found

Latest commit: 01c3db6

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

@CLAassistant
Copy link

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you sign our Contributor License Agreement before we can accept your contribution.


Alberto Farah seems not to be a GitHub user. You need a GitHub account to be able to sign the CLA. If you have already a GitHub account, please add the email address used for this commit to your account.
You have signed the CLA already but the status is still pending? Let us recheck it.

Copy link
Contributor

@devin-ai-integration devin-ai-integration bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✅ Devin Review: No Issues Found

Devin Review analyzed this PR and found no potential bugs to report.

View in Devin Review to see 2 additional findings.

Open in Devin Review

@toubatbrian
Copy link
Contributor

toubatbrian commented Mar 9, 2026

Do we still need this PR given #1110? Seems they have overlapping changes

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants