macOS: free surface synchronously in deinit on main thread - #13364
Conversation
|
Seems related to #9760, too. I'm curious if the approach here covers all of the complications I discovered while working on that one. |
mitchellh
left a comment
There was a problem hiding this comment.
I'm not confident in this change. If I'm understanding properly, the problem is sometimes ghostty_app_free happens BEFORE all the surface frees? That's fundamentally it? If so, this doesn't seem right. If not, I don't understand what this is doing. It doesn't seem like a surface should have any business freeing the entire app (which owns N surfaces).
I need more info.
Sh*t, you're right, I used And for the context for I assume the second
For the previous undo crash mentioned in #9512, I reproduced it again the backtrace is exactly the same as |
| // Since the renderer thread now emits scrollbar events | ||
| // on almost every frame, there's always a `.scrollbar` message | ||
| // for the dying surface in the app mailbox. | ||
| // |
There was a problem hiding this comment.
Even if this ends up being the right approach, I don't think we should frame the rationale like this. This is a point-in-time description of the current state rather than a lifecycle contract.
That's a (the?) reason why #9760 is as involved as it is: it avoids making assumptions about the mailbox, etc. and instead makes the contract "signal and wait for the apprt to finish" before we finalize the termination of the (macOS) app itself.
I acknowledge that the issue you're going after here (#13359, #13359) is in the tab+surface close path rather than the whole app termination path, but there are similarities.
There was a problem hiding this comment.
Make sense to me! Do you have a suggestion how should we phrase this? I don't think I have better understanding of the whole lifecycle including the renderer and the mailbox🤔


Since the renderer thread now emits scrollbar events on almost every frame, there's always a
.scrollbarmessage for the dying surface in the app mailbox.The OS runtime seems to schedule
appTickandghostty_surface_freedifferently across macOS pre-26, 26 and 27.On macOS 26.x,
ghostty_app_freehappens afterApp.scrollbar(_:target:v:), leavingsurface.userdatapointing at a freedSurfaceView.When
deinitruns on the main thread, free the surface synchronously instead of detaching to a task. This fixes both crashes mentioned in #9512 and #13359.AI Disclosure
I used Claude to analyze the backtrace, but the code is written and tested by myself.