Skip to content
Open
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
9 changes: 6 additions & 3 deletions docs/agents/ssh-tunnels.md
Original file line number Diff line number Diff line change
Expand Up @@ -269,9 +269,12 @@ Remote termination is best effort with its own three-second budget; local
teardown never waits for AWS.

SSM cleanup reports a bounded one-time termination result through the runtime
connection attempt when an explicitly disconnected tunnel is finally dropped.
Disconnect subscribes before cancelling an in-flight connection too, so a
session whose ID has not arrived still produces an uncertainty notice.
connection attempt when its tunnel is finally dropped. The workspace subscribes
for each SSM connect and form test, independently of the form and profile
generation, so cancellation, failure, and reconnect cannot discard the result.
Background cleanup waits up to one second for a late stdout session ID, ending
early on stdout EOF; local teardown does not wait for that drain. A session
whose ID never arrives still produces an uncertainty notice.
The window keeps unconfirmed results separately from other notices;
profile and region are configured settings, not verified identity. No result
is persisted, and the local watchdog never waits for the remote request.
3 changes: 2 additions & 1 deletion docs/ssm-tunnels.md
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,8 @@ Job Object. Local cleanup is independent of a bounded, best-effort remote
remote session visible until AWS expires it. A dropped tunnel never falls
back to a direct database connection or replays a query.

If AWS does not confirm the termination request after an explicit disconnect,
If AWS does not confirm termination after a connection test, failed or cancelled
connection, reconnect, or disconnect,
DBDelve shows the observed session ID, if available, and the configured profile
and region in a notice. Copy the ID to locate the session in the AWS
Session Manager console and choose the correct account and region before ending
Expand Down
91 changes: 84 additions & 7 deletions src/db/ssm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ impl ConnectAttempt {
if let Some(send) = cleanup.sender.take() {
let _ = send.send(result);
} else {
// Startup can fail before Disconnect installs the observer.
// Preserve delivery even when cleanup beats observer registration.
cleanup.result = Some(result);
}
}
Expand Down Expand Up @@ -295,6 +295,7 @@ struct Output {
session: Option<String>,
last_diagnostic: Option<String>,
closed: bool,
stdout_done: bool,
}
impl Output {
fn line(&mut self, line: &str, stdout: bool) {
Expand Down Expand Up @@ -433,9 +434,27 @@ fn drain(reader: impl Read + Send + 'static, output: Arc<Mutex<Output>>, stdout:
.unwrap_or_else(|p| p.into_inner())
.line(&String::from_utf8_lossy(&line), stdout);
}
if stdout {
output.lock().unwrap_or_else(|p| p.into_inner()).stdout_done = true;
}
});
}

// Only the background cleanup waits: closing the local tunnel never waits on
// output readers, including a descendant that keeps stdout open indefinitely.
fn cleanup_session(output: &Mutex<Output>, timeout: Duration) -> Option<String> {
let deadline = Instant::now() + timeout;
loop {
{
let output = output.lock().unwrap_or_else(|p| p.into_inner());
if output.session.is_some() || output.stdout_done || Instant::now() >= deadline {
return output.session.clone();
}
}
thread::sleep(Duration::from_millis(10));
}
}

pub(super) struct Tunnel {
local: SocketAddr,
process: Arc<Mutex<process::Process>>,
Expand Down Expand Up @@ -622,14 +641,10 @@ impl Drop for Tunnel {
.lock()
.unwrap_or_else(|p| p.into_inner())
.stop();
let session = self
.output
.lock()
.unwrap_or_else(|p| p.into_inner())
.session
.clone();
let output = self.output.clone();
let config = self.config.clone();
thread::spawn(move || {
let session = cleanup_session(&output, Duration::from_secs(1));
let accepted = if let Some(session) = &session {
let mut args = vec![
"ssm".into(),
Expand Down Expand Up @@ -672,6 +687,41 @@ mod tests {
..Default::default()
}
}
#[test]
fn cleanup_waits_for_late_stdout_but_not_indefinitely() {
let output = Arc::new(Mutex::new(Output::default()));
let pending = output.clone();
let reader = thread::spawn(move || {
thread::sleep(Duration::from_millis(30));
drain(
std::io::Cursor::new(b"Starting session with SessionId: late-session"),
pending,
true,
);
});
assert_eq!(
cleanup_session(&output, Duration::from_secs(2)).as_deref(),
Some("late-session")
);
reader.join().unwrap();

let unread = Mutex::new(Output::default());
assert_eq!(cleanup_session(&unread, Duration::from_millis(20)), None);
}

#[test]
fn cleanup_returns_on_stdout_eof_before_timeout() {
let output = Arc::new(Mutex::new(Output::default()));
drain(std::io::empty(), output.clone(), true);
let timeout = Duration::from_secs(2);
let started = Instant::now();
assert_eq!(cleanup_session(&output, timeout), None);
assert!(
started.elapsed() < timeout,
"EOF must end the wait, not the deadline"
);
}

#[test]
fn cleanup_completed_before_subscription_is_delivered() {
for accepted in [false, true] {
Expand Down Expand Up @@ -1007,6 +1057,33 @@ mod lifecycle_tests {
thread::sleep(Duration::from_millis(20));
}
}
#[test]
fn cleanup_recovers_session_id_parsed_after_drop() {
let mut fixture = Fixture::new("ready");
let attempt = ConnectAttempt::default();
fixture.config.attempt = Some(attempt.clone());
let mut tunnel = fixture.open();
// Hold the reader's result until after Drop, as when stdout is buffered
// but its drain thread has not yet been scheduled.
let output = Arc::new(Mutex::new(Output::default()));
tunnel.output = output.clone();
fs::write(
fixture.config.aws_cli.as_ref().unwrap(),
"#!/bin/sh\nprintf '{\"SessionId\":\"late-session\"}'\n",
)
.unwrap();
let receiver = attempt.observe_cleanup();
drop(tunnel);
// Reaching here before providing output proves local Drop did not wait.
output
.lock()
.unwrap()
.line("Starting session with SessionId: late-session", true);
let result = cleanup_result(receiver);
assert!(result.accepted);
assert_eq!(result.session.as_deref(), Some("late-session"));
}

#[test]
fn cleanup_result_waits_for_last_tunnel_owner() {
let mut fixture = Fixture::new("ready");
Expand Down
1 change: 0 additions & 1 deletion src/session.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,6 @@ pub(crate) struct Profile {
pub(crate) confirmed_stale: bool,
pub(crate) generation: u64,
pub(crate) state: ProfileState,
pub(crate) cleanup_attempt: Option<crate::db::ConnectAttempt>,
/// Whether the server is known to hold this connection as `mode` says
/// (`Connection::set_read_only`): set when a connect lands, which fails
/// rather than open unheld, and by the latest hold `set_mode` sent, once
Expand Down
Loading