Skip to content

fix(linux): stop theme changes deadlocking the app mailbox - #46

Merged
melonamin merged 1 commit into
melonamin:linux-portfrom
n2tr2:fix/linux-theme-feed-deadlock
Oct 5, 2026
Merged

melonamin merged 1 commit into
melonamin:linux-portfrom
n2tr2:fix/linux-theme-feed-deadlock

Conversation

@n2tr2

@n2tr2 n2tr2 commented Oct 1, 2026

Copy link
Copy Markdown

Problem

On Linux, changing the theme with many sessions open can freeze agterm for good. The window stops repainting and the control socket stops answering, starting with theme set itself. It reproduced with 12 sessions in 5 of 5 manual runs and with 16 in the regression harness; the exact threshold depends on timing.

The cause is that the main thread waits on itself. applySettings fed theme OSC 10/11 into every surface. Each feed pushed color_change into libghostty's 64-slot app mailbox with a .forever fallback. Only the main thread drains that mailbox, in ghostty_app_tick. With enough surfaces the mailbox fills, the push blocks, and nothing is left to drain it.

Fix

Theme colors now reach surfaces only through config, as they do on macOS. The main thread no longer pushes a blocking message into its own mailbox.

  • Feeds removed: the live loop in LinuxSettingsController.applySettings, the creation feed, and the overlay feed in GhosttySurface are gone. So are GhosttySurface.feed, GhosttyApp.currentThemeOSC, and themeOSC / themeResetOSC. ghostty_app_update_config already propagates the theme to every surface.
  • OSC 11 latch overlay: once the feeds were gone, a hidden bug showed. The latch overlay restated the program's OSC 11 color as background=, which re-seeded the terminal default, so OSC 111 reset to the program's color instead of the theme. While the latch is set, the overlay now carries only opacity and font, matching the macOS fix in umputun/agterm#312 for umputun/agterm#309. The choice lives in a new pure LinuxBackgroundOverlayPolicy with unit tests.
  • libghostty patch (linux/patches/ghostty-embedded-opengl.patch):
    • The ghostty_surface_feed API is removed. It existed only in this patch.
    • renderer_health was the other main-thread .forever producer: on Linux GL, frameCompleted runs on the main thread. Under must_draw_from_app_thread it now pushes .instant and records the value as pending. App.tick retries pending values after drainMailbox. The latest pending value wins, and an older success never clears a newer one. Metal and every other path keep the original .forever push.
    • The pending logic is a small PendingHealth.zig helper with 7 Zig tests against a real 64-slot BlockingQueue.
    • The patch is regenerated against 0ba6250. CI's libghostty cache is keyed on the patch hash, so CI rebuilds once.

Testing locally: setup-linux.sh keeps any complete vendored libghostty, and verify-linux-vendor-cache.sh does not check patch contents. Remove agterm-linux/vendor/ghostty (the link or directory itself) and rerun scripts/setup-linux.sh before testing, or you test the old library. Then AGTERM_ATSPI_SCENARIO=theme-many-sessions scripts/test-linux-ui.sh runs the reproducer.

Behavior changes besides the hang

  • Existing rows' foreground updates on a theme change immediately. Before, it lagged one theme behind.
  • With no program OSC 11 override active, session background clear returns to the theme. Before, it stayed on the old color. A live program override is not cleared by config, the same as on macOS.
  • A program's OSC 11 color survives a theme change. Before, the feed overwrote it.

Guarding against regressions

  • New AT-SPI scenario theme-many-sessions, in the full suite:
    • It opens 16 sessions with live shells and makes three theme changes (light → dark → light), each followed by tree, every call bounded at 4s.
    • It then checks terminal colors with in-shell OSC 10/11 queries on surfaces created before and after the theme change, plus a reset to the built-in default, a program's OSC 11 surviving a theme change, OSC 111, and session colors.
    • On unmodified linux-port (b1ea700f) it fails at the hang step: theme set never answers, also with a 30s bound.
  • control_json / raw_control_json take a per-call timeout, and a TimeoutExpired is reported as a FAIL: line instead of a traceback.
  • .claude/rules/libghostty.md records the config-only theme path and the rule that nothing on the Linux main thread may push .forever into the app mailbox.

Testing

In the CI image (Swift 6.3.2, zig 0.16.0), with libghostty rebuilt from the new patch:

  • swift test: AgtermLinux 453 passed, agtermCore 3846 passed.
  • swiftlint lint --strict: clean.
  • scripts/test-linux-ui.sh: 41 scenarios passed, 0 failed.
  • scripts/test-linux-resources.sh and scripts/verify-linux-vendor-cache.sh: pass.
  • Zig: zig build test -Dapp-runtime=none -Dtest-filter="PendingHealth: ": 78/78, of which 7 are new.
  • Provenance: the vendored and staged libghostty.so have the same sha256, and neither exports ghostty_surface_feed.

Hang check (three theme changes, each followed by tree):

sessions before (b1ea700f) after
16 hangs on the first theme set OK
24 — OK

Pixel check under Xvfb (llvmpipe, prefer-dark), reading renderer-drawn pixels. Each cell is background / foreground (a reverse-video row) / palette red. The program's OSC 11 color is #aa0000, and the session color is #335577. These are representative rows; the full runs also show the Dracula foreground fix on a second session and on an Atom → Dracula switch.

step before after
Atom One Light, existing surface f9f9f9 / ffffff / de3e35 f9f9f9 / 2a2c33 / de3e35
Dracula 282a36 / 2a2c33 / ff5555 282a36 / f8f8f2 / ff5555
session color cleared 335577 / 2a2c33 / de3e35 f9f9f9 / 2a2c33 / de3e35
program OSC 11, then Dracula 282a36 / f8f8f2 / ff5555 aa0000 / f8f8f2 / ff5555
OSC 111 reset 282a36 / f8f8f2 / ff5555 282a36 / f8f8f2 / ff5555

At 85% opacity, the two builds' backgrounds differ only in the session-color-cleared and OSC 11 rows. Foreground and red rows keep each build's full-opacity values.

Not verified: a theme preview in the picker followed by cancel, and a live system light/dark switch with a theme pair configured.

Known limitations, not addressed here

  • A program that sends OSC 11 and OSC 111 in the same write, on a pane with its own session color, ends on the theme background instead of the session color. This predates the change; the old feed masked it. It recovers on the next OSC 111 or session-color change.
  • Separate search-thread and IO-mailbox wait cycles on the main thread remain out of scope.

Merges cleanly with #45.

🤖 Generated with Claude Code

https://claude.ai/code/session_01UXsFSAnftzk65uhqTTKixZ

@n2tr2
n2tr2 requested a review from melonamin as a code owner October 1, 2026 15:41
With many sessions open, a theme change froze the app: the main thread fed
theme OSC 10/11 into every surface, each feed pushed color_change into
libghostty's 64-slot app mailbox with a .forever fallback, and only the main
thread drains that mailbox.

- Theme colors reach surfaces only through config; the host feeds, the
  feed helpers and themeOSC/themeResetOSC are removed.
- The OSC 11 latch overlay carries only opacity and font (macOS
  umputun#312), selected by the new LinuxBackgroundOverlayPolicy.
- libghostty patch: drop ghostty_surface_feed; under
  must_draw_from_app_thread, renderer_health pushes .instant and a
  PendingHealth value is retried from App.tick after the drain.
- AT-SPI scenario theme-many-sessions guards the hang and the color cases;
  control calls take a per-call timeout.
- libghostty.md records the config-only path and the app mailbox rule.

Claude-Session: https://claude.ai/code/session_01UXsFSAnftzk65uhqTTKixZ
@n2tr2
n2tr2 force-pushed the fix/linux-theme-feed-deadlock branch from 2107d72 to 0808b7a Compare October 1, 2026 15:48
@yeroo

yeroo commented Oct 5, 2026

Copy link
Copy Markdown

A field report that looks like this same hang, but without any theme change. Issues are off on this repo, so I'm posting it here; this PR seems the right home.

Setup: agterm-linux 0.33.1-1 (deb), Ubuntu 26.04 x86_64, GNOME on Wayland, restore mode rerun, not live. The terminal is driven by automation (agworkbench) through agtermctl. That means many session new --command … calls (often --no-select, followed by a select and back to realize them), session split on, session type --stdin, session text and tree --json polled every few seconds by about 10 processes, plus session restore and session close. About 15–20 sessions and 17 renderer threads were live, with sessions created and closed all night. The theme was never touched.

Symptom (about 02:00, found at 06:15): the control socket stopped answering, timeout 5 agtermctl version returned 124, and everything that polls hung. The process was idle (1.8% CPU), had 1757 threads, and nearly all of them were in futex_do_wait:

comm wchan threads
agterm-linux.bi futex_do_wait 1757
renderer io_cqring_wait 17
io-reader futex_do_wait 15
io io_cqring_wait 15
io-gather poll_schedule_timeout.constprop.0 8
io-gather futex_do_wait 7

I couldn't get a backtrace (ptrace_scope=1). The thread pile looks like every later control request stacked up behind a blocked main thread. In the 4 h after a restart, with the same workload, the count stayed flat at 74–82 threads, so this isn't a slow leak; something blocked once and requests piled up behind it.

This PR's analysis fits: a main-thread .forever push into the 64-slot app mailbox that only the main thread drains. Our workload has no theme change, but it creates surfaces constantly, so the creation feed this PR removes, or the renderer_health producer, looks like a plausible trigger. Happy to run a build of this branch under the same workload and report back.

@melonamin
melonamin merged commit 976d942 into melonamin:linux-port Oct 5, 2026
5 checks passed
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.

3 participants