Hmbown/CodeWhale · error

web run state should be present until write-back

Error message

web run state should be present until write-back

What it means

WebRunStateGuard::new takes the session/page maps out of the RwLock write guards and stashes them in Option state; Drop writes them back. state_mut() panics if called after the state was already consumed (e.g. during or after write-back), which would indicate a double-take or use-after-drop bug in the guard, not bad input.

Source

Thrown at crates/tui/src/tools/web_run.rs:761

    fn new(
        mut sessions: RwLockWriteGuard<'a, HashMap<String, WebRunSessionState>>,
        mut pages: RwLockWriteGuard<'a, HashMap<String, StoredWebPage>>,
    ) -> Self {
        let state = WebRunState {
            sessions: std::mem::take(&mut *sessions),
            pages: std::mem::take(&mut *pages),
        };
        Self {
            sessions,
            pages,
            state: Some(state),
        }
    }

    fn state_mut(&mut self) -> &mut WebRunState {
        self.state
            .as_mut()
            .expect("web run state should be present until write-back")
    }

    fn write_back(mut self) {
        self.restore();
    }

    fn restore(&mut self) {
        if let Some(state) = self.state.take() {
            *self.sessions = state.sessions;
            *self.pages = state.pages;
        }
    }
}

impl Drop for WebRunStateWriteBack<'_> {
    fn drop(&mut self) {
        self.restore();
    }

View on GitHub (pinned to 0c42157ee5)

Solutions

  1. Only call state_mut() while the guard is alive and write-back has not run
  2. Return Option/&mut WebRunState via a taking method instead of expecting, if callers can legitimately observe the taken state
  3. Add a debug assertion with a clearer message distinguishing drop-time from mid-use consumption
Defensive patterns

Strategy: type-guard

When it happens

Trigger: Thrown at crates/tui/src/tools/web_run.rs:761 when the library encounters an invalid state.

Common situations: See trigger scenarios.


AI-assisted analysis of Hmbown/CodeWhale@0c42157ee5 (2026-08-20). Data as JSON: /api/errors/a724a2a8fcbbfd5b. Report an issue: GitHub.