Repository navigation
fix(v3/linux): release WebKitURISchemeRequest on the GTK main context - #5566
Conversation
request.Close() runs on the assetserver goroutine and dropped what may be the last reference to the WebKitURISchemeRequest there. Finalizing a WebKit GObject off the GTK main thread violates WebKitGTK's threading rules and intermittently crashes long-running apps with a SIGSEGV in g_object_unref during cgo execution. Schedule the unref onto the default GMainContext with g_main_context_invoke instead, in both the GTK3 (webkit2gtk-4.1) and GTK4 (webkitgtk-6.0) request implementations, which share the pattern. g_object_ref in NewRequest is unchanged: ref/unref themselves are atomic; only the finalization triggered by the final unref is main-thread-only. Verified: gtk3 and gtk4 builds plus assetserver/application test suites pass on Linux (Ubuntu, WebKitGTK 4.1/6.0). The equivalent workaround was validated in production by the reporter over multi-day runs. Fixes #5557
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: Path: .coderabbit.yaml Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (2)
🚧 Files skipped from review as they are similar to previous changes (2)
WalkthroughC helper functions were added to Linux WebKit request handlers to schedule ChangesGTK main context marshalling for WebKit request finalization
🎯 3 (Moderate) | ⏱️ ~20 minutes
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches📝 Generate docstrings
🧪 Generate unit tests (beta)
Warning There were issues while running some tools. Please review the errors and either fix the tool's configuration or disable the tool if it's a critical failure. 🔧 golangci-lint (2.12.2)level=error msg="[linters_context] typechecking error: pattern ./...: directory prefix . does not contain main module or its selected dependencies" Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Pull request overview
Fixes an intermittent Linux crash in the v3 assetserver WebView request cleanup path by ensuring WebKitURISchemeRequest is unref’d on the GTK main context (avoiding off-main-thread finalization of a WebKit GObject), and documents the fix in the unreleased changelog.
Changes:
- Marshal
WebKitURISchemeRequestfinalg_object_unrefonto the defaultGMainContextviag_main_context_invokefor GTK4 (webkitgtk-6.0) builds. - Apply the same main-context unref scheduling for GTK3 (
webkit2gtk-4.1) builds. - Add an unreleased changelog entry describing the crash fix and linking to #5557.
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
| v3/internal/assetserver/webview/request_linux.go | Dispatches request unref onto the GTK main context for GTK4 builds to prevent off-thread finalization. |
| v3/internal/assetserver/webview/request_linux_gtk3.go | Mirrors the same main-context unref scheduling for GTK3 builds. |
| v3/UNRELEASED_CHANGELOG.md | Records the Linux SIGSEGV fix and references #5557. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Co-authored-by: Copilot Autofix powered by AI <[email protected]>
Co-authored-by: Copilot Autofix powered by AI <[email protected]>
… (#5640) * fix(v3/linux/gtk3): keep window IDs integer-typed to avoid GC stack-scan crash setupSignalHandlers, contextMenuSignals, enableDND and disableDND passed the window ID as signal user_data, but materialised it in a Go unsafe.Pointer local via unsafe.Pointer(uintptr(id)). The value is a small integer (e.g. window ID 1, 0x1), not a real pointer, so when the Go garbage collector scanned that pointer-typed stack slot during a stack copy it aborted with: runtime: bad pointer in frame ...setupSignalHandlers at 0x...: 0x1 fatal error: invalid pointer found on stack The GTK4 path already passes the ID as C.uintptr_t: #4958 switched the C signal functions to uintptr_t specifically to clear -race/checkptr errors from this same fake-pointer pattern, but only on the (then new) GTK4 files. When #5463 flipped GTK4 to default and renamed the legacy code to *_gtk3.go, the GTK3 path kept the old unsafe.Pointer form. Back-port the uintptr_t change here: signal_connect, enableDND and disableDND now take uintptr_t and cast to gpointer in C, so the ID is never a Go pointer-typed value. Refs #5631 * fix(v3/linux): complete WebKit asset requests on the GTK main thread The asset server completes WebKitURISchemeRequests from a pool of worker goroutines (assetserver_webview.go), calling webkit_uri_scheme_request_finish_* off the GTK main thread. WebKit2GTK is not thread-safe: these calls mutate and finalise GObjects owned by the main loop, racing g_application_run and corrupting refcounts. The result is intermittent native crashes, typically SIGSEGV inside g_object_unref during g_main_context_iteration (addr=0x28) on long-running apps under frequent asset/media loads. #5566 marshalled only the trailing g_object_unref of the request onto the main context; the finish call itself - where most of the WebKit object manipulation happens - was still run on the worker goroutine, so the crash persisted. Add invokeOnMainSync (mainthread_linux.go): a synchronous main-thread dispatch built on g_main_context_invoke + GCond, shared by the GTK3 and GTK4 cgo builds. Run the full response completion (webkit_uri_scheme_request_finish_with_response and finish_error, including response/header construction and unref) under it. The wait cannot deadlock the main loop: finish_with_response returns before the response stream is drained (WebKit reads it asynchronously), so the main thread never blocks waiting on the worker. Body streaming over the pipe stays off the main thread. The request-body input stream (read/close/unref) is intentionally left on the worker: it is transfer-full and owned solely by us, so its blocking read must not freeze the main loop and its unref finalises nothing in the main-loop object graph. Refs #5631, #5557 * fix(v3/linux): use a stable GError domain for asset-server finish errors finishWithError interned the per-request error message as the GError domain via g_quark_from_string(msg). GQuarks are never freed, so a long-running app with varying error messages grew the global quark table unboundedly, and the message string is the wrong value for a domain anyway. Intern a single static domain quark once (g_quark_from_static_string) instead; the error text is still carried in the GError message field. Applies to both the GTK3 and GTK4 builds. This pre-existing leak sat next to the finish_error call moved onto the main thread in the previous commit. Addresses Copilot review feedback on #5640. Refs #5631
Description
request.Close()runs on the assetserver goroutine (AssetServer.processWebViewRequest→webViewAssetRequest.Close) and dropped what may be the last reference to theWebKitURISchemeRequestthere. Finalizing a WebKit GObject off the GTK main thread violates WebKitGTK's threading rules and intermittently kills long-running apps with:Fix
Schedule the unref onto the default
GMainContextwithg_main_context_invokeinstead of unreffing inline. Applied to both the GTK3 (webkit2gtk-4.1) and GTK4 (webkitgtk-6.0) request implementations, which share the identical pattern.g_object_refinNewRequestis intentionally unchanged: GObject ref/unref are atomic and thread-safe in themselves — only the finalization triggered by dropping the final reference is main-thread-only.g_main_context_invokeruns the callback immediately if the caller already owns the main context, so this is also correct if Close ever runs on the UI thread.Verification
go build/go vetwith-tags gtk3and default gtk4 tags pass on Linux (Ubuntu, WebKitGTK 4.1 + 6.0).go testfor./internal/assetserver/...and./pkg/application(under xvfb, gtk3 tags) passes.WebKitURISchemeRequestunder load. The equivalent patch (unref viag_main_context_invoke) was validated by the reporter in production over multi-day runs (see [v3][Linux][GTK3] Intermittent SIGSEGV in assetserver when closing WebKitURISchemeRequest (g_object_unref) #5557).Fixes #5557
Type of change
Checklist:
v3/UNRELEASED_CHANGELOG.mdSummary by CodeRabbit