Skip to content

fix(v3/linux): release WebKitURISchemeRequest on the GTK main context#5566

Merged
leaanthony merged 4 commits into
masterfrom
fix/gtk3-request-unref-5557
Jun 13, 2026
Merged

fix(v3/linux): release WebKitURISchemeRequest on the GTK main context#5566
leaanthony merged 4 commits into
masterfrom
fix/gtk3-request-unref-5557

Conversation

@leaanthony

@leaanthony leaanthony commented Jun 10, 2026

Copy link
Copy Markdown
Member

Description

request.Close() runs on the assetserver goroutine (AssetServer.processWebViewRequestwebViewAssetRequest.Close) 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 kills long-running apps with:

SIGSEGV: segmentation violation
signal arrived during cgo execution
_Cfunc_g_object_unref(...)
(*request).Close(...)  request_linux_gtk3.go:80

Fix

Schedule the unref onto the default GMainContext with g_main_context_invoke instead 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_ref in NewRequest is 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_invoke runs 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 vet with -tags gtk3 and default gtk4 tags pass on Linux (Ubuntu, WebKitGTK 4.1 + 6.0).
  • go test for ./internal/assetserver/... and ./pkg/application (under xvfb, gtk3 tags) passes.
  • A deterministic unit test isn't feasible here — the crash needs a live WebKit webview finalizing a real WebKitURISchemeRequest under load. The equivalent patch (unref via g_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

  • Bug fix (non-breaking change which fixes an issue)

Checklist:

  • My code follows the general coding style of this project
  • I have updated v3/UNRELEASED_CHANGELOG.md

Summary by CodeRabbit

  • Bug Fixes
    • Fixed an intermittent crash on Linux that could occur when closing in-app web requests, improving stability during asset serving.
  • Documentation
    • Added a changelog entry documenting the fix.

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
Copilot AI review requested due to automatic review settings June 10, 2026 21:27
@coderabbitai

coderabbitai Bot commented Jun 10, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: 97a6dfb1-1e73-4363-aebb-e493c707b94a

📥 Commits

Reviewing files that changed from the base of the PR and between 460175a and f71313e.

📒 Files selected for processing (2)
  • v3/internal/assetserver/webview/request_linux.go
  • v3/internal/assetserver/webview/request_linux_gtk3.go
🚧 Files skipped from review as they are similar to previous changes (2)
  • v3/internal/assetserver/webview/request_linux.go
  • v3/internal/assetserver/webview/request_linux_gtk3.go

Walkthrough

C helper functions were added to Linux WebKit request handlers to schedule g_object_unref on the GTK main context via g_main_context_invoke, and both (*request).Close() implementations now call that helper. UNRELEASED_CHANGELOG.md documents the fix referencing #5557.

Changes

GTK main context marshalling for WebKit request finalization

Layer / File(s) Summary
Add C helpers to invoke g_object_unref on GTK main context
v3/internal/assetserver/webview/request_linux.go, v3/internal/assetserver/webview/request_linux_gtk3.go
Adds C helper functions that schedule a callback with g_main_context_invoke which performs g_object_unref on a WebKitURISchemeRequest (includes null checks and one-shot callback semantics).
Call C helper from Close()
v3/internal/assetserver/webview/request_linux.go, v3/internal/assetserver/webview/request_linux_gtk3.go
Replaces direct g_object_unref calls in both (*request).Close() implementations with releaseRequestOnMainThread(r.req) so the unref runs on the GTK main thread.
Changelog documentation
v3/UNRELEASED_CHANGELOG.md
Adds an UNRELEASED_CHANGELOG.md entry noting the intermittent Linux SIGSEGV fix and that final g_object_unref is marshalled onto the GTK main context via g_main_context_invoke (refs #5557).

🎯 3 (Moderate) | ⏱️ ~20 minutes

Bug, go, v3, Linux

🐰 I hopped through threads both near and far,
Found an unref that tripped on where GTK are,
I marshalled it gently to the main thread's light,
Now objects fall safe in the day and night,
Hooray — no more SIGSEGV fright! 🎉

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly and concisely describes the main fix: releasing WebKitURISchemeRequest on the GTK main context to fix threading issues on Linux.
Description check ✅ Passed The PR description is comprehensive, explaining the problem, solution, rationale, verification, and references the fixed issue. However, the Type of change and Checklist sections were completed but testing platform checkboxes (Windows, macOS, Linux) and wails doctor output were not provided.
Linked Issues check ✅ Passed The PR successfully addresses all objectives from issue #5557: prevents intermittent SIGSEGVs by scheduling g_object_unref on the GTK main context via g_main_context_invoke, applies the fix to both GTK3 and GTK4 implementations, and includes proper verification steps.
Out of Scope Changes check ✅ Passed All changes are directly scoped to fixing issue #5557: modifications to request_linux.go and request_linux_gtk3.go implement the threading fix, and the changelog entry documents the fix. No unrelated changes are present.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch fix/gtk3-request-unref-5557

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.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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 WebKitURISchemeRequest final g_object_unref onto the default GMainContext via g_main_context_invoke for 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.

Comment thread v3/internal/assetserver/webview/request_linux.go
Comment thread v3/internal/assetserver/webview/request_linux_gtk3.go
leaanthony and others added 3 commits June 12, 2026 21:10
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
@leaanthony leaanthony merged commit e094d72 into master Jun 13, 2026
12 of 15 checks passed
@leaanthony leaanthony deleted the fix/gtk3-request-unref-5557 branch June 13, 2026 03:13
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.

[v3][Linux][GTK3] Intermittent SIGSEGV in assetserver when closing WebKitURISchemeRequest (g_object_unref)

2 participants