GhosttyHost: guard the remaining native callbacks against exceptions crossing the boundary#444
Merged
Merged
Conversation
…crossing the boundary OnAction was hardened against managed exceptions unwinding across the unmanaged ABI into Zig, but the other callbacks libghostty invokes directly on its own thread had the same exposure: - OnCloseSurface calls GCHandle.FromIntPtr, which throws on a bad handle. - OnConfirmReadClipboard decodes a raw C string (Marshal.PtrToStringUTF8). - OnWriteClipboard walks native memory via the content marshaller. - OnReadClipboard and OnWakeup share the same boundary. Wrap each synchronous body in a try/catch that logs and absorbs the exception (OnReadClipboard returns 0 = "not handled"). The clipboard bridge's deferred dispatcher work already self-guards on the UI thread, so only the native-thread portion needed protecting here.
…afely
Review follow-up. Document that returning 0 ("not handled") cannot strand a
clipboard request, since HandleRead only throws on its synchronous prefix
before it takes on the completion obligation.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Follow-up to the OnAction native-boundary guard. The other five callbacks libghostty invokes directly on its own thread (the delegates registered as function pointers in the ctor) had the same exposure: a managed exception unwinding across the ABI back into Zig is undefined behavior.
The synchronous bodies that can throw:
OnCloseSurfacecallsGCHandle.FromIntPtr, which throws on a bad/freed handle.OnConfirmReadClipboarddecodes a raw C string viaMarshal.PtrToStringUTF8.OnWriteClipboardwalks native memory via the content marshaller.OnReadClipboardandOnWakeupare lower risk but sit on the same boundary.Each synchronous body is now wrapped in a
try/catchthat logs and absorbs (OnReadClipboardreturns0= "not handled"). Guarding at the callback level also covers the clipboard bridge's synchronous decode, since that runs inline before the work is dispatched. The bridge's deferred dispatcher lambdas already self-guard on the UI thread, soClipboardBridge.csis untouched.OnWakeup's body only hops to the dispatcher and is unlikely to throw, but it is guarded too so every registered callback has the same invariant rather than leaving one boundary uncovered.