Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 6 additions & 5 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -358,18 +358,19 @@ gpui_platform = { version = "=0.3.9", package = "gpui-pre-platform", features =
# default features, so the colored-diff console stack stays out.
insta = { version = "=1.49.0", default-features = false }

# The Job Object that ends an SSH tunnel's ssh.exe when DBDelve exits, however
# it exits: Windows has no pipe-closing watchdog like the `sh` one on Unix.
# Pinned to the version already locked, so naming it adds no copy.
# The Job Object that ends an SSH tunnel's ssh.exe when DBDelve exits, and
# MoveFileExW for atomic no-overwrite query renames. Pinned to the version
# already locked, so naming it adds no copy.
[target.'cfg(windows)'.dependencies]
windows-sys = { version = "=0.61.2", features = [
"Win32_Security",
"Win32_System_JobObjects",
"Win32_System_Threading",
"Win32_Storage_FileSystem",
] }

# The Unix side of the same: `pthread_sigmask`, so the ssh a background thread
# starts still dies when told to. Pinned to the version already locked.
# pthread_sigmask for SSH shutdown and native no-overwrite query renames.
# Pinned to the version already locked.
[target.'cfg(unix)'.dependencies]
libc = "=0.2.189"

Expand Down
6 changes: 6 additions & 0 deletions docs/agents/dev-databases.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,12 @@ its target, a dictionary, the `analytics` database, the `edit_*` tables
healthcheck counts the two rows of `type_gallery`, so a half-seeded container
is not healthy. The live tests are `cargo test clickhouse -- --include-ignored`.

The ClickHouse HTTP mock releases a completed execution's session before its
complete response becomes observable, including a zero-length body's headers.
Releasing after socket shutdown races the client's next request and invents a
`SESSION_IS_LOCKED`; delayed or stalled executions still hold their session
until they finish or are stopped.

Snowflake has no container. Its unit tests need nothing, and neither do its
mock tests: `snowflake/mock.rs` is a loopback HTTP server replaying responses
recorded from a real account (`dev/snowflake/fixtures`), with scripted
Expand Down
33 changes: 30 additions & 3 deletions docs/agents/session.md
Original file line number Diff line number Diff line change
Expand Up @@ -263,10 +263,37 @@ The shape a change to the main pane has to fit (`session.rs`, with the
on one empty buffer instead. `active` may
then name a tab that is gone, which every lookup by id already answers with
`None`, and focus falls back to the window so the bindings that open a tab
still work. Deleting the saved query in a tab closes that tab.
still work.
- **Closing a tab never deletes a saved query.** The strip holds open tabs
only; every query file a profile has is listed in the sidebar's Queries
group and the palette, open or not (`Session::saved_queries`). Closing a
saved query's tab writes its text to its file and keeps the file; only an
explicit Delete (the sidebar row's menu, or "Delete saved query…" in the
palette, both through one dialog) removes it. Deleting a query whose tab is
open closes that tab, its text with it; a tab holding cell edits nobody
applied asks first, as any close does (`PendingDiscard::deleting`), and the
file stays until it is told. A file already removed outside the app counts
as deleted, so its stale library row and open tab can still be removed.
Rename… on a sidebar row turns the row into a field
(`Session::query_rename`) that renames through `store::save_query_as`,
the tab's own path, so an open tab follows the name. A rename is one native
atomic no-overwrite move, never a write under the new name and a delete of
the old one: on a case-insensitive filesystem `daily` and `Daily` are one
file. A name taken in any case is refused. New saves also install their
target without replacing an existing entry; a missed directory listing is
not permission to overwrite it. Staging files are private to each writer.
- **A named buffer persists to its query file, an unnamed one to its own
`.scratch-{id}.sql`.** Every buffer is written on quit, not just the visible
one. `store::read_scratch(id, 0)` migrates the single `.scratch.sql` an older
`.scratch-{id}.sql`.** Edits stay in memory until Save, a connection switch,
opening another query, or normal quit/window close persists the buffers.
Closing a saved query's tab also writes it before the tab is removed.
Writes are synchronous at those boundaries; typing schedules no disk
writes, timers or retries. There are no background buffer writes to order
against rename or deletion, so neither needs a per-tab write gate.
A crash or forced termination can lose edits since the last write.
Giving an unnamed tab a query name removes that tab's old scratch only
after the query write succeeds. A cleanup error stays visible without
undoing the saved query, and a failed query write keeps the scratch.
`store::read_scratch(id, 0)` migrates the single `.scratch.sql` an older
build left behind, and `StoredProfile::open_query` is still read for the same
reason and never written.
- **Grids are snapshotted to disk and restored on relaunch.** The first edit on
Expand Down
25 changes: 19 additions & 6 deletions src/db/clickhouse/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -453,7 +453,7 @@ impl Mock {
struct Decision {
response: Response,
tag: Option<String>,
/// Held while the response is written; the session is free after.
/// Held until execution ends, before a complete response is observable.
session: Option<String>,
query_id: Option<String>,
}
Expand Down Expand Up @@ -609,19 +609,23 @@ fn serve(stream: TcpStream, state: &Mutex<State>) {
let Some(request) = read_request(&stream, state) else {
return;
};
let decision = {
let mut decision = {
let mut state = state
.lock()
.unwrap_or_else(std::sync::PoisonError::into_inner);
state.requests.push(request.clone());
decide(&request, &mut state)
};
let _ = write_response(stream, &decision, state);
if let Some(session) = &decision.session {
let _ = write_response(stream, &mut decision, state);
release_session(&mut decision.session, state);
}

fn release_session(session: &mut Option<String>, state: &Mutex<State>) {
if let Some(session) = session.take() {
let mut state = state
.lock()
.unwrap_or_else(std::sync::PoisonError::into_inner);
if let Some(held) = state.sessions.get_mut(session) {
if let Some(held) = state.sessions.get_mut(&session) {
*held = false;
}
}
Expand Down Expand Up @@ -717,14 +721,15 @@ fn late_exception(message: &str, tag: Option<&str>) -> Vec<u8> {

fn write_response(
mut stream: TcpStream,
decision: &Decision,
decision: &mut Decision,
state: &Mutex<State>,
) -> std::io::Result<()> {
let response = &decision.response;
let query_id = decision.query_id.as_deref();
let tag = decision.tag.as_deref();
let mut aftermath = None;
if let Delivery::Dropped = response.delivery {
release_session(&mut decision.session, state);
return stream.shutdown(Shutdown::Both);
}
if let Delivery::Delayed(pause) = response.delivery {
Expand Down Expand Up @@ -760,6 +765,12 @@ fn write_response(
false => head.push_str(&format!("Content-Length: {}\r\n", body.len())),
}
head.push_str("\r\n");
// A client can send its next request as soon as Content-Length bytes
// arrive (just the headers for an empty body), before this thread runs
// again. A completed execution must already have released its session.
if !matches!(response.delivery, Delivery::Stalled(..)) || aftermath.is_some() {
release_session(&mut decision.session, state);
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
}
stream.write_all(head.as_bytes())?;

let write = |stream: &mut TcpStream, bytes: &[u8]| -> std::io::Result<()> {
Expand Down Expand Up @@ -798,8 +809,10 @@ fn write_response(
write(&mut stream, &body[..sent.min(body.len())])?;
stream.flush()?;
if let Some(aftermath) = wait(pause, response, query_id, state) {
release_session(&mut decision.session, state);
return end_killed(stream, aftermath, tag, write, finish);
}
release_session(&mut decision.session, state);
stream.shutdown(Shutdown::Both)
}
}
Expand Down
Loading
Loading