Skip to content

fix(pty): prevent crash loop when closing app with running processes#679

Merged
rabanspiegel merged 5 commits intomainfrom
emdash/app-crash-75x
Jan 21, 2026
Merged

fix(pty): prevent crash loop when closing app with running processes#679
rabanspiegel merged 5 commits intomainfrom
emdash/app-crash-75x

Conversation

@rabanspiegel
Copy link
Contributor

@rabanspiegel rabanspiegel commented Jan 21, 2026

Summary

  • Guard IPC sends to destroyed WebContents with safeSendToOwner() helper
  • Add WebContents destruction listener to kill PTYs when window closes
  • Add before-quit handler to clean up all PTYs on app shutdown
  • Guard pty:started broadcast with per-window isDestroyed() check

Test plan

  • Start app with npm run dev
  • Create a task with a running agent (e.g., Claude)
  • Close the app (Cmd+Q) while the agent is active
  • Verify clean exit with no crash dialog
  • Verify pty:ownerDestroyed logs appear in terminal output

Fixes #676


Note

Improves PTY lifecycle safety to avoid crashes during window/app shutdown.

  • Adds safeSendToOwner() to guard IPC sends when WebContents is destroyed
  • Attaches per-PTY onData/onExit listeners that use safe sending and ignore stale onExit events
  • Registers WebContents destroyed handler to maybeMarkProviderFinish(), killPty(), and clear PTY ownership/listener state
  • Guards pty:started broadcast by checking webContents.isDestroyed() per window
  • On app before-quit, iterates all PTYs to maybeMarkProviderFinish(), killPty(), and clears owners/listeners
  • Minor: import app from electron to support shutdown handling

Written by Cursor Bugbot for commit 39e7aed. This will update automatically on new commits. Configure here.

Guard IPC sends to destroyed WebContents and clean up PTYs on window
close to prevent the "Object has been destroyed" crash loop.

- Add safeSendToOwner() helper that checks isDestroyed() before sending
- Add WebContents destruction listener to kill PTYs when window closes
- Add before-quit handler to clean up all PTYs on app shutdown
- Guard pty:started broadcast with per-window isDestroyed() check

Fixes #676
@vercel
Copy link

vercel bot commented Jan 21, 2026

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Review Updated (UTC)
docs Ready Ready Preview, Comment Jan 21, 2026 10:30pm

Request Review

cursor[bot]

This comment was marked as outdated.

@cursor
Copy link

cursor bot commented Jan 21, 2026

Bugbot Autofix prepared a fix for the bug found in the latest run.

  • ✅ Fixed: Stale destruction listener kills PTY when owner changes
    • Added ownership verification in the destruction listener to prevent stale listeners from killing PTYs reused by other windows.

View PR

Move listener outside if-block so each owner gets a listener, but
only the current owner's listener triggers cleanup. Prevents both
stale listeners killing PTYs and missing cleanup for new owners.
cursor[bot]

This comment was marked as outdated.

@cursor
Copy link

cursor bot commented Jan 21, 2026

Bugbot Autofix prepared a fix for the bug found in the latest run.

  • ✅ Fixed: Race condition: old PTY onExit corrupts new PTY state
    • Added stale check to onExit callback that verifies the PTY is still active (getPty(id) === proc) before modifying owners/listeners maps, preventing old PTY exit from corrupting new PTY state.

View PR

…#681)

The onExit callback now checks if the PTY is still active before
modifying the owners and listeners maps, preventing a race condition
where an old PTY's exit could corrupt a new PTY's state when they
share the same ID.

Co-authored-by: Cursor Agent <cursoragent@cursor.com>
Copy link

@cursor cursor bot left a comment

Choose a reason for hiding this comment

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

Cursor Bugbot has reviewed your changes and found 1 potential issue.

Bugbot Autofix is ON. A Cloud Agent has been kicked off to fix the reported issue.

Call maybeMarkProviderFinish before killPty in both the 'destroyed'
listener and 'before-quit' handler to ensure telemetry timers are
cleared and agent_run_finish events are captured when PTYs are
killed during window close or app shutdown, matching the pattern
established in the pty:kill handler.
@cursor
Copy link

cursor bot commented Jan 21, 2026

Bugbot Autofix prepared a fix for the bug found in the latest run.

  • ✅ Fixed: Missing telemetry cleanup in PTY shutdown handlers
    • Added maybeMarkProviderFinish calls before killPty in both the destroyed listener and before-quit handler to ensure telemetry cleanup matches the pty:kill pattern.

Create PR

@rabanspiegel rabanspiegel merged commit a550a46 into main Jan 21, 2026
4 checks passed
@rabanspiegel rabanspiegel deleted the emdash/app-crash-75x branch January 22, 2026 23:02
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.

[bug]: App Crashes When Closed

2 participants