wezterm icon indicating copy to clipboard operation
wezterm copied to clipboard

Fix PaneFocused notification storm on pane destruction and mux reconciliation

Open fculpo opened this issue 7 months ago • 4 comments

Fixes #4390

Picks up where #4737 left off. That PR was closed because unconditionally suppressing the PaneFocused notification from advise_focus_change broke wezterm cli activate-{tab,pane} over SSH and prevented focus updates from reaching other attached clients.

Problem

Three things conspire to create a runaway focus-notification loop:

  1. Destroyed-pane storm — when a unix-domain detach tears down many panes at once, the subscriber drains a burst of PaneFocused notifications whose pane ids are already gone. The handler in frontend.rs spawns a reconciliation task on the main thread for every one of them. Thousands of these pile up faster than the UI thread can service them, resulting in unresponsive mouse input, erratic focus changes, and a flood of "Error reconciling PaneFocused notification: pane X not found" in the log.

  2. Redundant re-notification — several call sites (Mux::add_pane_to_window, sessionhandler, tmux_commands) call set_active_pane and then immediately fire their own MuxNotification::PaneFocused. Because advise_focus_change also fires the same notification, every focus change emits the event twice. With two rapid user-initiated switches (pane 0 → 1 → 0), the doubled events interleave and the queue never drains — exactly the scenario described in #4390 and #4517.

  3. Client echo-back — when a remote PaneFocused arrives at a client pane, focus_pane_and_containing_tab calls focus_changed(true), which for a ClientPane advises the server of the "new" focus. This echoes the server's own notification right back as a fresh SetFocusedPane PDU, adding fuel to the loop.

Fix

Each layer gets a targeted guard rather than a blanket suppression:

  • frontend.rs — cheap synchronous get_pane() check before spawning the reconciliation task. Defunct notifications are dropped without touching the UI thread. A pane that disappears between the guard and the spawned task falls through to the existing error log.

  • tab.rs — new NotifyMux enum and set_active_pane_with_notify entry point. Call sites that fire their own notification pass NotifyMux::No to avoid the duplicate; everyone else (including the GUI and set_active_idx) still gets the notification via the default set_active_pane → NotifyMux::Yes path. This is the same NotifyMux idea from #4737, but applied only to the three call sites that already send the notification themselves, so SSH-attached clients and wezterm cli activate-* keep working.

  • clientpane.rs — pre-set focused_remote_pane_id before calling focus_pane_and_containing_tab, so the subsequent focus_changed(true) sees the pane as already focused on the server side and does not echo back a SetFocusedPane PDU.

Testing

Added set_active_pane_can_suppress_mux_notification unit test that verifies NotifyMux::No actually suppresses the notification.

Manually verified:

  • local pane focus switching (including rapid Alt+arrow spam)
  • wezterm cli activate-pane / activate-tab over unix domain
  • detaching a domain with many panes no longer floods the log

fculpo avatar Apr 28 '26 06:04 fculpo

Confirmed this fixes my reproducible pane-focus hang on macOS.

Before the patch, with pane_focus_follows_mouse = true and any registered format-tab-title callback—even one returning only "x"—rapidly moving the mouse between panes on the same tab reliably caused WezTerm to lock up at 100% CPU.

I cherry-picked this PR onto current upstream main, ran the added mux test, and built a release version. After applying the patch, the reproducer no longer hangs. I also restored my full tab-title formatter and repeated the stress test successfully.

Thanks for working on this!

Fail-Safe avatar Jun 20 '26 03:06 Fail-Safe

Hello @fculpo :)

Thanks again for the PR, are you willing to get it to the finish line and get it merged, and start answering comments from my last review ? 🤔

Or maybe @ankitson since you made https://github.com/ankitson/wezterm/commit/f3c52f3ba189b6e4c52fd28c02e081f5c1c353b8 that addresses most of my comments above, maybe you'd want to open a separate PR? 🤔

bew avatar Jun 20 '26 04:06 bew

Hi @bew :)

Yes there are a couple of bugs in Wezterm that annoyed me too much so I had to fix them.. i just wanted to review them a bit better before sending them to you - will open PRs for them soon!

ankitson avatar Jun 20 '26 04:06 ankitson

@bew My PR is ready on #7871

ankitson avatar Jun 20 '26 07:06 ankitson