Let sessions outlive the backend, and never resume one twice
Three `claude` processes ended up running against this checkout on 2026-08-29, and the account hit its session limit. One cause, several ways in. An agent imported the Claude Code session it was *itself* running in. That is an ordinary import, and importing runs `--resume` -- so a second CLI attached to a file the first was still writing. The whole 65 MB conversation, 154 embedded screenshots included, was re-appended to the transcript under a new prompt id; both copies then read each other's writes as work done elsewhere, and the adopted one was billed for re-reading all of it. Meanwhile `shutdown_all` asked each session to stop and the process exited immediately, so the SIGKILL timer died with the runtime, the stop was unreliable, and whatever survived was orphaned with nothing written down to find it by. The processes leaked either way. So leak them on purpose, and be able to pick them back up. A session's process now outlives the backend and is adopted again on the way up, which is worth having for its own sake: restarting the server no longer ends a turn somebody is waiting on. Its stdio lives in the session directory -- a fifo opened read-write so the process is its own last writer and never reads EOF, plus stdout/stderr logs read from a byte offset. `session::process` records the pid *and* the kernel's start time for it, because a pid alone is reused and adopting a stranger's would mean never resuming the real conversation. That makes the fix structural rather than a check: everything goes through `ClaudeDriver::launch`, which adopts if it can and starts if it cannot, and `--resume` is reachable only on the second path. `Driver` gains two ways out where it had one -- `detach` (coming back) and `stop` (the session is being deleted, so the process must not survive). Importing a session that is open is now refused outright. Claude Code keeps `~/.claude/sessions/<pid>.json` for every live session, so this is a measurement rather than a guess; it reports no/yes/unknown, because a machine that keeps no such record cannot answer and "could not check" is not "nobody is using it". `SessionStatus` gains `Unknown` for the same reason. Also here, found on the way: - A reconnecting phone was sent the entire backlog. Opening a session was bounded to a page but reconnecting was not, so a long disconnect delivered thousands of events one frame at a time. Past `CATCH_UP_LIMIT` the stream sends a `reset` frame and the newest window, and the client rebuilds from it as it does on open -- without the reset the window is spliced onto rows no longer adjacent to it. - A session's status was assumed idle at launch. Read from the transcript instead, so a restart stops claiming an exited session is waiting for you. - `llama-server`'s stdout was piped and never drained, so a chatty one blocked on a full pipe buffer mid-load. It goes to a log now. - A turn that exited or errored never emitted `Idle`, so the queue stayed "running" for good: every later message was held forever and, since a message is only recorded when taken, vanished with nothing on screen. - Two doc comments had drifted onto the wrong functions. Verified by killing the server mid-turn: the process survived, finished its turn unattended (12.8 KB of output nothing was reading), and the restarted server adopted it -- one process, all 700 lines in the transcript, no hole, and it still took a new message afterwards. Deleting a session stops its process; a 266-event backlog resets while a 16-event one streams. 46 tests, clippy and rustfmt clean, app compiles and lints. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01VETa8afmpWaYezLCqJhDB8
This commit is contained in:
1 parent
9791afcfd6
commit
362d436d4f
23 files changed
+1934
-345
No files matched your search
+62
-26
@@ -11,7 +11,9 @@
|
||||
//! DELETE /setups/{id} remove, refused while sessions use it
|
||||
//! GET /sessions list (id, provider, title, model, status, last activity)
|
||||
//! POST /sessions spawn {setup, provider, title?, model?, cwd?, params?}
|
||||
//! GET /sessions/{id}/events?after=N SSE: transcript replay from N, then live
|
||||
//! GET /sessions/{id}/events?after=N SSE: backlog after N, then live
|
||||
//! (a backlog past CATCH_UP_LIMIT arrives as a
|
||||
//! `reset` frame plus the newest window)
|
||||
//! POST /sessions/{id}/message {text, attachmentIds?}
|
||||
//! POST /sessions/{id}/answer {questionId, answer} (questions and permissions)
|
||||
//! POST /sessions/{id}/interrupt
|
||||
@@ -29,7 +31,7 @@
|
||||
//! branch on the session kind (that's what drivers are for).
|
||||
|
||||
use std::convert::Infallible;
|
||||
use std::path::PathBuf;
|
||||
use std::path::{Path, PathBuf};
|
||||
use std::sync::Arc;
|
||||
|
||||
use anyhow::Context;
|
||||
@@ -45,7 +47,7 @@ use tokio::sync::{broadcast, mpsc};
|
||||
use tokio_stream::StreamExt;
|
||||
use tokio_stream::wrappers::ReceiverStream;
|
||||
|
||||
use crate::session::transcript::{SeqEvent, read_after};
|
||||
use crate::session::transcript::{CATCH_UP_LIMIT, CatchUp, SeqEvent, catch_up};
|
||||
use crate::session::{LiveSession, SessionInfo, SessionManager, SpawnSpec};
|
||||
|
||||
pub fn router(manager: Arc<SessionManager>) -> Router {
|
||||
@@ -462,6 +464,20 @@ async fn spawn_session(
|
||||
only this app's view of it."
|
||||
)));
|
||||
}
|
||||
// Refused rather than warned about, because there is nothing
|
||||
// useful on the other side of it. Importing an open session
|
||||
// puts a second `--resume` on a file the first one is still
|
||||
// writing: the conversation gets duplicated into it, each copy
|
||||
// replays the other's writes as work done elsewhere, and the
|
||||
// adopted one is billed for re-reading the whole thing. On
|
||||
// 2026-08-29 that was 65 MB and 154 screenshots.
|
||||
if chosen.in_use == crate::session::import::InUse::Yes {
|
||||
return Err(ApiError::BadRequest(format!(
|
||||
"{want} is open in a terminal right now. Importing it would put a second \
|
||||
Claude Code on the same conversation, which duplicates it and re-reads the \
|
||||
whole thing. Close it there first, then import it here."
|
||||
)));
|
||||
}
|
||||
let events = crate::session::import::replay(&transport, &chosen.path)
|
||||
.await
|
||||
.map_err(bad_request)?;
|
||||
@@ -802,23 +818,8 @@ async fn stream_session(
|
||||
mut live: broadcast::Receiver<SeqEvent>,
|
||||
tx: mpsc::Sender<SseEvent>,
|
||||
) {
|
||||
// Synchronous file reads from an async task: transcript lines are
|
||||
// small and local; revisit if daily use produces transcripts where
|
||||
// this shows (phase 6 territory).
|
||||
let catch_up = |after: u64| match read_after(&transcript, after) {
|
||||
Ok(entries) => Some(entries),
|
||||
Err(err) => {
|
||||
tracing::error!("transcript replay failed: {err:#}");
|
||||
None
|
||||
}
|
||||
};
|
||||
|
||||
let Some(replay) = catch_up(last) else { return };
|
||||
for entry in replay {
|
||||
last = entry.seq;
|
||||
if send_event(&tx, &entry).await.is_err() {
|
||||
return;
|
||||
}
|
||||
if !send_backlog(&transcript, &mut last, &tx).await {
|
||||
return;
|
||||
}
|
||||
loop {
|
||||
match live.recv().await {
|
||||
@@ -832,12 +833,8 @@ async fn stream_session(
|
||||
}
|
||||
}
|
||||
Err(broadcast::error::RecvError::Lagged(_)) => {
|
||||
let Some(missed) = catch_up(last) else { return };
|
||||
for entry in missed {
|
||||
last = entry.seq;
|
||||
if send_event(&tx, &entry).await.is_err() {
|
||||
return;
|
||||
}
|
||||
if !send_backlog(&transcript, &mut last, &tx).await {
|
||||
return;
|
||||
}
|
||||
}
|
||||
Err(broadcast::error::RecvError::Closed) => return,
|
||||
@@ -845,6 +842,45 @@ async fn stream_session(
|
||||
}
|
||||
}
|
||||
|
||||
/// Sends everything after `last`, advancing it, and answers whether the
|
||||
/// subscriber is still there.
|
||||
///
|
||||
/// A [`CatchUp::Restart`] is preceded by the `reset` frame that tells the
|
||||
/// client to drop what it holds. Without it the window would be spliced
|
||||
/// onto rows that are no longer adjacent to it, which reads as ordinary
|
||||
/// output rather than as a gap -- which is why a bounded backlog cannot
|
||||
/// simply be "the newest events".
|
||||
///
|
||||
/// Both ways into a backlog come through here -- the first replay and the
|
||||
/// recovery from a lapped broadcast -- because either can be arbitrarily
|
||||
/// far behind and owes the client the same answer.
|
||||
///
|
||||
/// Synchronous file reads from an async task: transcript lines are small
|
||||
/// and local; revisit if daily use produces transcripts where this shows
|
||||
/// (phase 6 territory).
|
||||
async fn send_backlog(transcript: &Path, last: &mut u64, tx: &mpsc::Sender<SseEvent>) -> bool {
|
||||
let entries = match catch_up(transcript, *last, CATCH_UP_LIMIT) {
|
||||
Ok(CatchUp::Continue(entries)) => entries,
|
||||
Ok(CatchUp::Restart(entries)) => {
|
||||
if tx.send(SseEvent::default().event("reset")).await.is_err() {
|
||||
return false;
|
||||
}
|
||||
entries
|
||||
}
|
||||
Err(err) => {
|
||||
tracing::error!("transcript replay failed: {err:#}");
|
||||
return false;
|
||||
}
|
||||
};
|
||||
for entry in entries {
|
||||
*last = entry.seq;
|
||||
if send_event(tx, &entry).await.is_err() {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
true
|
||||
}
|
||||
|
||||
async fn send_event(
|
||||
tx: &mpsc::Sender<SseEvent>,
|
||||
entry: &SeqEvent,
|
||||
|
||||
Reference in new issue
Block a user