Skip to content

fix(pane_tree): prevent nil pane access in symmetric layouts#127

Open
tdragon wants to merge 1 commit intoMLFlexer:mainfrom
tdragon:fix/duplicate-pane-processing
Open

fix(pane_tree): prevent nil pane access in symmetric layouts#127
tdragon wants to merge 1 commit intoMLFlexer:mainfrom
tdragon:fix/duplicate-pane-processing

Conversation

@tdragon
Copy link
Copy Markdown

@tdragon tdragon commented Nov 3, 2025

Summary

Fixes #98 - Resolves nil pane access error that occurs during workspace switching with symmetric pane layouts

Problem

When saving workspace state with symmetric pane layouts (e.g., 2x2 grid or perfect cross layout), the following error occurs:

attempt to index a nil value (field 'pane')
at pane_tree.lua:85 in root.pane:get_domain_name()

Root Cause

In the recursive insert_panes() function, symmetric pane layouts can cause the same pane node to be encountered multiple times through different branches:

  1. A pane is processed through one branch (e.g., right branch)
  2. After processing, root.pane is set to nil (line 122) to avoid memory leaks
  3. The same pane node is encountered again through another branch (e.g., bottom branch)
  4. Attempting to call root.pane:get_domain_name() on the already-nil pane causes the crash

This happens because in symmetric layouts, a pane can satisfy both is_right() and is_bottom() conditions from the same parent node, causing it to appear in both branch collections.

Add guard clause to skip panes that have already been processed by
another branch in symmetric layouts (e.g., perfect cross layout).
This prevents nil pane access errors when a pane appears in both
right and bottom branches.

Fixes MLFlexer#98
@tdragon tdragon force-pushed the fix/duplicate-pane-processing branch from 6679791 to ec66651 Compare November 3, 2025 20:56
micimize added a commit to micimize/resurrect.wezterm that referenced this pull request Mar 5, 2026
Addresses a crash loop caused by periodic auto-save capturing ghost SSH
domain tabs (stuck in "Connecting..." with empty cwd and zero dimensions)
into workspace JSON. On restore, these spawn broken tabs that cascade
into UI crashes.

Save-side safeguards:
- Add is_pane_healthy() to filter panes with nil/empty cwd, zero cell
  dimensions, or non-spawnable domains before tree construction
- Skip tabs with no healthy panes in window state serialization
- Skip windows with no valid tabs in workspace save
- Skip periodic save entirely when workspace is degraded
- Create .bak backup before overwriting state files

Restore-side safeguards:
- Add validate_pane_tree() to check cwd and domain validity, with
  recursive pruning of invalid subtrees
- Validate each tab before restoring, skip invalid ones
- Track restored_count independently from loop index for correct
  first-tab reuse logic
- Wrap per-window restore in pcall with state reset on failure
- Guard against empty tabs[1] access when spawning windows
- Fall back to .bak file when primary state file is invalid
- Nil guard on active_tab:activate() (latent upstream bug)

Upstream fixes incorporated:
- PR MLFlexer#127: nil pane guard in insert_panes() for symmetric layouts
- PR MLFlexer#123: use direct require("resurrect.state_manager") to fix
  circular module dependency in save actions
- PR MLFlexer#118: set active workspace after restore_workspace()

Wrap io.lines() in pcall in load_json() to handle missing files
gracefully instead of throwing.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
micimize added a commit to micimize/resurrect.wezterm that referenced this pull request Mar 5, 2026
Tests cover:
- is_pane_healthy: 10 cases (nil pane, nil/empty cwd, zero dims, domains)
- create_pane_tree: 6 cases (filtering, empty list, sorting)
- validate_pane_tree: 13 cases (nil/empty cwd, domains, subtree pruning)
- geometry helpers: 7 cases (is_right, is_bottom, sorting, spatial layouts)
- file_io: 7 cases (backup-before-overwrite, load_json error handling)
- sanitize_json: 4 cases (control chars, null bytes, tabs, newlines)
- state_manager: 4 cases (load_state backup fallback)
- PR MLFlexer#127 nil guard: 1 case (2x2 symmetric grid)

All 52 tests pass under lua5.4.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
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.

possible json sanitize bug on workspace switching

1 participant