Skip to content

fix(linux): stop promoting a split survivor by reparenting it - #39

Merged
melonamin merged 2 commits into
melonamin:linux-portfrom
n2tr2:linux-paned-reparent-pr
Sep 3, 2026
Merged

melonamin merged 2 commits into
melonamin:linux-portfrom
n2tr2:linux-paned-reparent-pr

Conversation

@n2tr2

@n2tr2 n2tr2 commented Aug 31, 2026

Copy link
Copy Markdown

The crash

A split session whose primary pane shell exited crashed with a SIGSEGV in gtk_widget_get_request_mode. closePrimaryPane promoted the survivor by clearing both GtkPaned slots and re-adding the survivor's host:

gtk_paned_set_start_child(paned, nil)
gtk_paned_set_end_child(paned, nil)               // sole ref dropped
gtk_paned_set_start_child(paned, W(survivorHost)) // freed memory relinked

A GTK4 container owns the only reference to a sunk child, so clearing the slot finalized the host and the re-add linked freed memory into the live tree; the next frame-clock layout called through a class pointer g_type_free_instance had NULLed. Confirmed against a coredump: paned start_child a dead pointer, end_child NULL, preceded by six GTK_IS_WIDGET assertion failures.

The exception is a container's focus child, which carries a second reference — which is why this only reproduced with the primary focused, and why it looked intermittent.

Why the obvious fix isn't the fix

Holding a reference across the reparent stops the crash but leaves the promoted pane blank. Unparenting a live GtkGLArea unrealizes it, GTK destroys its GdkGLContext, and the re-add realizes a new one while libghostty keeps drawing into the dead one — GhosttySurface connects realize but no unrealize, and realize()'s createSurface() no-ops on a live surface, so refresh() cannot repair it. Measured on that build: 0.0 ink, and a byte-identical framebuffer after typing into the pane.

So promotion no longer reparents at all. A pane host's paned slot is fixed for its lifetime:

  • collapseSplit frees the dead host's slot and shows the survivor.
  • layoutSplit is the single placement site and derives the slot pairing from GTK (gtk_paned_get_end_child(paned) == primaryWidget) rather than storing it, so a second promotion alternates slots with the same code.

Measured after the change: 0.470 ink, 0.0049 repaint diff.

Consequences that had to be handled

Because the primary can now occupy the end slot, two role-named contracts had to follow the model rather than the screen:

  • splitRatio converts through panedFraction at all three GTK boundaries so it always means the primary's share. Previously it inverted after a promotion, which mis-aimed --split-ratio and --grow-primary and came back mirrored after a relaunch.
  • focusPane splits into a physical left: overload (arrow keys, palette) and a model wantSplit: overload (dashboard, notification reveal, Ctrl+1/Ctrl+2, matching upstream macOS).

Two related visibility faults are fixed alongside: promoting from a maximized primary, and closing a maximized split, each left a hidden pane host with no later layoutSplit pass to re-show it — an empty content area over a live shell.

withWidgetRefHeld is the one scoped ref hold for any remaining reparent, and grep -rn withWidgetRefHeld is the site inventory. The hand-rolled ref/unref pairs in the zoom and pane-overlay paths are converted to it. The rule is recorded in .claude/rules/main-loop.md, with the GLArea consequence owned by libghostty.md.

Coverage

  • New split-primary-exit AT-SPI scenario drives the crash path over the control API through a re-split and a second promotion, pinning pane identity by the spawn-baked AGTERM_PANE_ID. It must focus the left pane before exit — with the split focused, the focus-child reference hides the bug and the scenario passes on a broken build.
  • GhosttySurface logs when a GLArea is re-realized over a live surface. This is the only automated signal for the blank-pane class, which AT-SPI cannot see (it asserts structure, never pixels).
  • Runner gains a GTK_IS_WIDGET assertion tripwire.

Known and out of scope

  • Terminal zoom blanks its surface the same way, by the same mechanism. Measured unchanged on builds predating this work, so it is pre-existing and not addressed here — filed in docs/backlog/linux-zoom-blanks-the-surface.md with two candidate fixes.
  • After a promotion the survivor keeps its physical side, so a new split lands on the freed side. Model addressing (--pane left|right) is unchanged.

Testing

swift build clean; agterm-linux and agtermCore unit tests at their pre-existing baselines; make lint zero findings; split-primary-exit, split-exit, dashboard-modal, hidden-toolbar and the full AT-SPI suite run, with all failures matching the recorded pre-existing list for this host. Painting was verified by framebuffer capture in an isolated Xvfb instance, since no automated test can assert it.

Note notification-focus is flaky on the development host in a way that bisects to a file it never executes (adding a blank line flips it green) — a binary-layout timing cliff on a time.sleep(1.0) readiness, not a regression from this change. CI is the authority.

make test-app is the macOS-hosted gate and was not runnable here.

🤖 Generated with Claude Code

@n2tr2
n2tr2 requested a review from melonamin as a code owner August 31, 2026 12:42
n2tr2 and others added 2 commits September 2, 2026 21:58
A split session whose PRIMARY pane shell exited crashed with a SIGSEGV in
`gtk_widget_get_request_mode`. `closePrimaryPane` promoted the survivor by
clearing both `GtkPaned` slots and re-adding the survivor's host:

    gtk_paned_set_start_child(paned, nil)
    gtk_paned_set_end_child(paned, nil)               // sole ref dropped
    gtk_paned_set_start_child(paned, W(survivorHost)) // freed memory relinked

A GTK4 container owns the only reference to a sunk child, so clearing the slot
finalized the host and the re-add linked freed memory into the live tree; the
next frame-clock layout called through a class pointer `g_type_free_instance`
had NULLed. The exception is a container's FOCUS child, which carries a second
reference — which is why the crash only reproduced with the primary focused,
and why it looked intermittent.

Holding a reference across the reparent stops the crash but leaves the promoted
pane BLANK: unparenting a live `GtkGLArea` unrealizes it, GTK destroys its
`GdkGLContext`, and the re-add realizes a new one while libghostty keeps drawing
into the dead one — `GhosttySurface` connects `realize` but no `unrealize`, and
`realize()`'s `createSurface()` no-ops on a live surface, so `refresh()` cannot
repair it. Measured: 0.0 ink, and a byte-identical framebuffer after typing.

So promotion no longer reparents at all. A pane host's paned slot is fixed for
its lifetime: `collapseSplit` frees the DEAD host's slot and shows the survivor,
and `layoutSplit` is the single placement site, deriving the slot pairing from
GTK rather than storing it. A second promotion alternates slots with the same
code. Measured after the change: 0.470 ink, 0.0049 repaint diff.

Because the primary can now occupy the end slot, two role-named contracts had to
follow the model rather than the screen: `splitRatio` converts through
`panedFraction` at all three GTK boundaries so it always means the primary's
share (it previously inverted after a promotion, mis-aiming `--split-ratio` and
`--grow-primary` and coming back mirrored after a relaunch), and `focusPane`
splits into a physical `left:` overload for the arrow keys and palette and a
model `wantSplit:` overload for the dashboard, notification reveal and Ctrl+1/2.

Two related visibility faults are fixed alongside: promoting from a maximized
primary, and closing a maximized split, each left a hidden pane host with no
later `layoutSplit` pass to re-show it — an empty content area over a live shell.

`withWidgetRefHeld` is the one scoped ref hold for any remaining reparent;
`grep -rn withWidgetRefHeld` is the site inventory. The hand-rolled ref/unref
pairs in the zoom and pane-overlay paths are converted to it.

Coverage: a new `split-primary-exit` AT-SPI scenario drives the crash path over
the control API through a re-split and a second promotion, pinning pane identity
by the spawn-baked `AGTERM_PANE_ID`. It must focus the left pane before `exit` —
with the split focused the focus-child reference hides the bug and the scenario
passes on a broken build. `GhosttySurface` now logs when a GLArea is re-realized
over a live surface, which is the only automated signal for the blank-pane class
(AT-SPI asserts structure, never pixels), and the runner gains a `GTK_IS_WIDGET`
assertion tripwire.

Terminal zoom blanks its surface the same way, unchanged on builds predating
this work. It is out of scope here and filed in docs/backlog/.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
@melonamin
melonamin force-pushed the linux-paned-reparent-pr branch from 54c4265 to 4dbad33 Compare September 3, 2026 02:31
@melonamin
melonamin merged commit 41c942d into melonamin:linux-port Sep 3, 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.

2 participants