Skip to content

Prevent system theme sync from crashing after Pi reloads - #3

Open
GodGardensGuns wants to merge 1 commit into
ferologics:mainfrom
GodGardensGuns:fix-stale-context-after-session-shutdown
Open

Prevent system theme sync from crashing after Pi reloads#3
GodGardensGuns wants to merge 1 commit into
ferologics:mainfrom
GodGardensGuns:fix-stale-context-after-session-shutdown

Conversation

@GodGardensGuns

Copy link
Copy Markdown

Issue

Pi now marks extension contexts as stale after session replacement or reload. pi-system-theme keeps a polling interval that captures the ctx from session_start. If Pi reloads, forks, switches sessions, or otherwise shuts down the current extension runtime while an appearance detection poll is in flight, that async poll can resume later and access ctx.ui.

That raises:

Error: This extension ctx is stale after session replacement or reload.

In interactive mode this can crash Pi via an uncaught exception.

Fix

  • track the active session with a per-session token
  • clear polling and invalidate the token on session_shutdown
  • re-check the token after async appearance detection before reading or writing ctx.ui
  • keep /system-theme save/apply behavior guarded against stale sessions as well

Testing

  • npx --yes vitest run

Also added a regression test that simulates shutdown while appearance detection is in flight and verifies the stale ctx.ui is not accessed.

MainasuK added a commit to MainasuK/pi-system-theme that referenced this pull request Sep 6, 2026
…ction

Fix upstream issues ferologics#3/ferologics#4: polling captured a session ExtensionContext and
resumed async detection on contexts pi had invalidated after session
replacement (/new, fork, switchSession) or /reload, throwing
'This extension ctx is stale...' and crashing pi.

- ctx is never captured in the interval; each poll reads the current
  session context behind a generation token
- generation is re-checked after every await before touching ctx.ui
- polling stops on session_shutdown and restarts on session_start;
  beginSession claims the context before the config-load await and
  clears stale polling/in-flight state
- sync/poll errors are caught and logged, never uncaught

Add configurable appearance source ('os' | 'env' | 'force-dark' |
'force-light') for SSH/headless hosts where OS appearance APIs describe
the host GUI session, not the terminal the user sees. env uses COLORFGBG
(the only client-terminal signal extensions can access). Document that
for exact terminal-following over SSH pi's built-in 'light/dark' auto
theme is the reliable mechanism.

Adds 12 regression tests (21 total); verified in a real interactive pi
run across /new and /reload with no stale-context errors.
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.

1 participant