fix: bind embedder userdata to surface lifetime
Summary
Fix the remaining lifetime race in the bounded renderer action path used by cmux.
An app-action lease can intentionally keep an embedded C surface alive after ghostty_surface_free returns. The Swift embedder previously released its userdata bridge when that function returned, so a lease acquired before teardown could later dispatch GHOSTTY_ACTION_RENDER and dereference freed userdata.
This adds an owned-userdata surface constructor without changing ghostty_surface_config_s. On successful creation, Ghostty owns the userdata release callback and invokes it exactly once from final surface destruction, after core teardown stops internal callbacks and after the last app-action lease returns. Failed creation leaves ownership with the caller.
This is the Ghostty-side lifetime completion for https://github.com/manaflow-ai/cmux/issues/8808 and https://github.com/manaflow-ai/cmux/pull/8848.
Tests
Red commit 4e45e6c77 failed only the new owned-userdata assertion: 72/73 tests passed, expected one release and observed zero.
Green commit 289097387 passes:
- owned userdata after the final action lease
- surface action lifetime final-reference behavior
- redraw action lease teardown behavior
- Zig formatting and diff checks
The regression also calls the userdata finalizer a second time and verifies the release callback remains exactly-once.
Need help on this PR? Tag @codesmith-bot with what you need. Autofix is disabled.
Summary by cubic
Bind embedder userdata to the surface lifetime to prevent use-after-free during app actions and host callbacks. Userdata releases exactly once after core teardown and the final in-flight lease returns.
-
New Features
- Added
ghostty_surface_new_with_owned_userdata(..., ghostty_userdata_release_cb)to transfer ownership ofconfig->userdata; on failure, ownership stays with the caller. - Introduced a refcounted userdata wrapper that provides leases and invokes the release callback once at final surface destruction;
ghostty_surface_newis unchanged.
- Added
-
Bug Fixes
- Acquired a userdata lease across every path that can call into the embedder (IO write/tee, renderer events, render-presented, clipboard, tmux, close) and during app/surface actions; includes setter-installed PTY tee callbacks via
ghostty_surface_set_pty_tee_cb. - Deferred release to
Surface.deinitaftercore_surface.deinit, and only after the final in-flight lease completes.
- Acquired a userdata lease across every path that can call into the embedder (IO write/tee, renderer events, render-presented, clipboard, tmux, close) and during app/surface actions; includes setter-installed PTY tee callbacks via
Summary by CodeRabbit
-
New Features
- Added a surface creation option that transfers ownership of embedder-provided userdata.
- Added a release callback invoked once after the surface is fully torn down and all in-flight actions finish.
- Preserved userdata availability during host callbacks and supported callbacks running on any thread.
-
Bug Fixes
- Ensured userdata remains owned by the caller, with no release callback invoked, when surface creation fails.