oxdock-ssh-plugin 0.49.0-alpha

Ephemeral user-space SSH server and client for OxDock scripts, bridged to stdio pipelines through the Engine facade.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
//! Synchronous byte pumps between handoff queues and DSL pipes.
//!
//! All blocking pump funcs (`SSH_ACCEPT` / `SSH_PUMP_CHANNEL`,
//! `SSH_CONNECT`, `SSH_PUMP`) share one shape: one worker per
//! direction on scoped threads, an event-driven supervisor, and
//! `close_pipe` for prompt downstream EOF. The only async contact is
//! the bounded queue pair; blocking queue ops run exclusively on plain
//! engine-task threads, never on Tokio workers.
//!
//! Nothing here polls. Workers block in pipe reads (woken by data,
//! close, or `oxdock_pipe::PipeInner::cancel`) or in the queue receive (woken by a
//! message, a `Wake`, or sender drop); the supervisor blocks on the
//! task's [`CancelHub`] until a worker completion lands or cancel
//! fires. Flag checks after a wake are not polling: no wakeup is ever
//! spent on an empty check.
//!
//! `&StepCtx` is not `Sync`, so every context call (`pipe_reader`,
//! `pipe_writer`, `pipe_backend`, `close_pipe`, `cancel_hub`) happens
//! on the calling supervisor thread. Workers receive only owned,
//! thread-safe handles.

use std::io::Write;
use std::sync::Arc;
use std::sync::atomic::{AtomicBool, Ordering};

use oxdock_diagnostics::{Diagnostic, DiagnosticResult};

use crate::diagnostics as ssh_diag;
use bytes::Bytes;
use oxdock_core::{StepCtx, Value};
use oxdock_process::{CancelHub, run_guarded as guard_worker};
use oxdock_process::{PUMP_CHUNK, ProcessManager, SharedInput, SharedOutput, read_pump_chunk};
use tokio::sync::mpsc;

use crate::state::{DownMsg, UpMsg};

/// Completion of one `pump_session` direction, by worker index (0 is
/// the pipe-to-wire worker, 1 the wire-to-pipe worker). Delivered
/// through [`guard_worker`], so a panicking worker reports instead of
/// stranding the supervisor past scope end.
enum SessionDone {
    InDone(std::thread::Result<DiagnosticResult<()>>),
    OutDone(std::thread::Result<DiagnosticResult<()>>),
}

/// Pump `up_rx -> writer` until the wire half-closes. The bounded queue
/// is FIFO, so an [`UpMsg::Eof`] always arrives after every byte sent
/// before it: return without waiting for the sender to drop (it stays
/// alive until the channel itself closes, which this return helps cause
/// via the supervisor's force-close : waiting for it would deadlock).
/// The supervisor force-closes the output pipe right after this worker
/// finishes, so downstream observes EOF promptly.
/// A supervisor wake releases a parked wait for cancellation or a
/// finished peer: queued bytes still flush first (every `Data` writes
/// unconditionally); only the wait ends. The wake travels its own
/// queue, so the data queue's sender-drop still means exactly "the
/// wire side is gone", on graceful close and on transport loss alike.
/// When `break_on_peer_done`, also ends once the stdin direction
/// finishes first (`peer_done`): the response producers are gone, so
/// anything still arriving has nowhere to go : this is what releases a
/// server pump whose inner leg (a `RUN` subprocess, an exited remote)
/// died while the outer client idles. Server-side only: a client must
/// linger for the reply and the exit-status, which legitimately arrive
/// after its own stdin EOF, so client pumps always wait for the wire
/// EOF or close.
fn pump_out(
    writer: &SharedOutput,
    mut up_rx: mpsc::Receiver<UpMsg>,
    mut wake_rx: mpsc::Receiver<()>,
    runtime: tokio::runtime::Runtime,
    cancel: &AtomicBool,
    peer_done: &AtomicBool,
    break_on_peer_done: bool,
) -> DiagnosticResult<()> {
    /// One wait outcome: queue traffic or a supervisor wake.
    enum Waited {
        Up(Option<UpMsg>),
        WakeUp,
    }
    loop {
        if cancel.load(Ordering::SeqCst) {
            break;
        }
        // One blocking wait on both queues: no timeout, no spin. The
        // bare current-thread runtime parks the thread (no drivers, no
        // threads of its own); either queue wakes it. `biased` keeps
        // queued bytes ahead of wakes. A closed wake queue ends the
        // worker like a gone wire: the supervisor is tearing down.
        let waited = runtime.block_on(async {
            tokio::select! {
                biased;
                message = up_rx.recv() => Waited::Up(message),
                wake = wake_rx.recv() => match wake {
                    Some(()) => Waited::WakeUp,
                    None => Waited::Up(None),
                },
            }
        });
        match waited {
            Waited::Up(Some(UpMsg::Data(bytes))) => {
                let mut guard = writer
                    .lock()
                    .map_err(|_| ssh_diag::pump_output_lock_poisoned())?;
                guard
                    .write_all(&bytes)
                    .map_err(ssh_diag::pump_output_write_failed)?;
                guard.flush().map_err(ssh_diag::pump_output_flush_failed)?;
            }
            Waited::Up(_) => break,
            Waited::WakeUp => {
                if break_on_peer_done && peer_done.load(Ordering::SeqCst) {
                    break;
                }
            }
        }
    }
    Ok(())
}

/// Pump `reader -> down_tx` until pipe EOF. Cancellation arrives as
/// EOF through [`oxdock_pipe::PipeInner::cancel`]: the supervisor cancels the input
/// backend when the hub fires, so no tick is needed. A cancelled EOF
/// still sends [`DownMsg::Eof`] (the wire half-close is the orderly
/// teardown on every path); only an explicit local cancel skips it,
/// matching the old flag-break wire behavior exactly.
fn pump_in(
    reader: &SharedInput,
    down_tx: &mpsc::Sender<DownMsg>,
    cancel: &AtomicBool,
) -> DiagnosticResult<()> {
    let mut buffer = [0u8; PUMP_CHUNK];
    loop {
        if cancel.load(Ordering::SeqCst) {
            break;
        }
        match read_pump_chunk(reader, &mut buffer) {
            Err(err) => {
                return Err(ssh_diag::pump_input_read_failed(&err));
            }
            Ok(0) => {
                if !cancel.load(Ordering::SeqCst) {
                    let _ = down_tx.blocking_send(DownMsg::Eof);
                }
                break;
            }
            Ok(count) => {
                if down_tx
                    .blocking_send(DownMsg::Data(Bytes::copy_from_slice(&buffer[..count])))
                    .is_err()
                {
                    break;
                }
            }
        }
    }
    Ok(())
}

/// Record the first worker error. Later errors lose: the first failure
/// explains the teardown.
pub(crate) fn note_failure(
    failed: &mut Option<Diagnostic>,
    outcome: std::thread::Result<DiagnosticResult<()>>,
    worker: &str,
) {
    match outcome {
        Ok(Ok(())) => {}
        Ok(Err(err)) => {
            if failed.is_none() {
                *failed = Some(err);
            }
        }
        Err(_) => {
            if failed.is_none() {
                *failed = Some(ssh_diag::pump_worker_panicked(worker));
            }
        }
    }
}

/// Pump a live session between explicit DSL pipes until both directions
/// end or the task cancels. `up_rx` carries wire bytes into `out_pipe`,
/// `in_pipe` bytes flow into `down_tx`. The output pipe is
/// force-closed as soon as the wire direction ends so downstream
/// observes EOF promptly instead of waiting out unrelated writer and
/// keeper lifetimes. The first worker error wins.
///
/// Workers run detached: the supervisor never joins them, so a worker
/// parked in an uninterruptible read (an orphaned grandchild holding
/// its pipe past every kill) cannot strand the task thread. Every
/// worker owns its captures outright (`Arc` clones, moved queue ends)
/// and reports through [`guard_worker`]; the frame keeps its own pipe
/// writers so backends detach at frame end. A stuck worker parks
/// quietly in kernel space holding its references until its pipe
/// severs; it never spins, and it never blocks teardown.
///
/// The wire worker waits on the data queue and a supervisor wake queue
/// together (one blocking wait, no timeout): wakes release it for
/// cancellation or a finished peer, while the data queue's sender-drop
/// still means exactly "the wire side is gone".
///
/// `break_on_peer_done` releases the output wait once the input side
/// ends: pass true for server pumps (a dead inner leg with an idling
/// outer client would strand the close), false for client pumps (the
/// reply and the exit-status arrive after the client's own stdin EOF).
pub fn pump_session<P: ProcessManager>(
    cx: &StepCtx<P>,
    in_pipe: &Value,
    out_pipe: &Value,
    up_rx: mpsc::Receiver<UpMsg>,
    down_tx: mpsc::Sender<DownMsg>,
    break_on_peer_done: bool,
) -> DiagnosticResult<()> {
    let reader = cx
        .pipe_reader(in_pipe)
        .map_err(ssh_diag::pump_borrow_input_failed)?;
    let writer = cx
        .pipe_writer(out_pipe)
        .map_err(ssh_diag::pump_borrow_output_failed)?;
    let in_backend = cx.pipe_backend(in_pipe);
    let hub = cx.cancel_hub();
    // Task-local cancellation shared with the detached workers. The
    // caller's token stays the source of truth (`cx.is_cancelled`);
    // this flag is the workers' copy of it.
    let cancel = Arc::new(AtomicBool::new(false));
    // Bare current-thread wait runtime for the wire worker's joint
    // queue wait: no drivers, no threads of its own, just a parking
    // spot. One per session; the client legs already pay more than
    // this for their multi-thread runtime.
    let wait_runtime = tokio::runtime::Builder::new_current_thread()
        .build()
        .map_err(ssh_diag::pump_wait_runtime_failed)?;
    // Supervisor wake queue for the wire worker. Bounded and
    // best-effort: a full queue means queued wakes already guarantee
    // the worker releases.
    let (wake_tx, wake_rx) = mpsc::channel::<()>(8);
    let peer_done = Arc::new(AtomicBool::new(false));
    let (done_tx, done_rx) = std::sync::mpsc::channel::<SessionDone>();
    let mut failed: Option<Diagnostic> = None;
    let mut in_done = false;
    let mut out_done = false;
    let mut out_closed = false;
    // Detached workers: the supervisor never joins them, so a worker
    // parked in an uninterruptible read cannot strand the task thread.
    // Every worker owns its captures outright (`Arc` clones, moved
    // queue ends); the frame keeps its own pipe writers so backends
    // detach at frame end.
    let in_tx = done_tx.clone();
    let out_tx = done_tx.clone();
    let peer_done_in = Arc::clone(&peer_done);
    let peer_done_out = Arc::clone(&peer_done);
    let reader_in = Arc::clone(&reader);
    let down_tx_in = down_tx.clone();
    let writer_out = Arc::clone(&writer);
    let cancel_in = Arc::clone(&cancel);
    let cancel_out = Arc::clone(&cancel);
    let in_hub = Arc::clone(&hub);
    let out_hub = Arc::clone(&hub);
    std::thread::spawn(move || {
        guard_worker(&in_hub, &in_tx, SessionDone::InDone, || {
            let result = pump_in(&reader_in, &down_tx_in, &cancel_in);
            if result.is_ok() {
                peer_done_in.store(true, Ordering::SeqCst);
            }
            result
        });
    });
    std::thread::spawn(move || {
        guard_worker(&out_hub, &out_tx, SessionDone::OutDone, || {
            pump_out(
                &writer_out,
                up_rx,
                wake_rx,
                wait_runtime,
                &cancel_out,
                &peer_done_out,
                break_on_peer_done,
            )
        });
    });
    // Supervisor: drain completions, translate hub events into worker
    // wakes, block on the hub otherwise. Every wake path is idempotent
    // so repeated wakes while cancelled stay harmless. Cancellation
    // breaks at once (after teardown): with detached workers there is
    // no join to wait out, so the task thread always returns.
    let mut seen = hub.sequence();
    loop {
        for event in done_rx.try_iter() {
            match event {
                SessionDone::InDone(outcome) => {
                    in_done = true;
                    note_failure(&mut failed, outcome, "input worker");
                    if break_on_peer_done {
                        let _ = wake_tx.try_send(());
                    }
                }
                SessionDone::OutDone(outcome) => {
                    out_done = true;
                    note_failure(&mut failed, outcome, "output worker");
                    if !out_closed {
                        out_closed = true;
                        // Best-effort EOF propagation: unbound/OS pipes cannot
                        // force-close, and their kernel halves close by drop.
                        let _ = cx.close_pipe(out_pipe);
                    }
                }
            }
        }
        if in_done && out_done {
            break;
        }
        if hub.is_cancelled() || cx.is_cancelled() {
            cancel.store(true, Ordering::SeqCst);
            if let Some(inner) = in_backend.as_ref() {
                inner.cancel();
            }
            let _ = wake_tx.try_send(());
            // Prompt downstream EOF on the way out: readers parked
            // past this see the close instead of hanging, and writers
            // still pushing fail out of their loops.
            let _ = cx.close_pipe(in_pipe);
            let _ = cx.close_pipe(out_pipe);
            break;
        }
        (_, seen) = hub.wait(seen);
    }
    match failed {
        Some(err) => Err(err),
        None => {
            if cancel.load(Ordering::SeqCst) {
                return Err(ssh_diag::pump_interrupted_by_cancellation());
            }
            Ok(())
        }
    }
}

/// Copy one pipe into another until EOF, then force-close the target so
/// downstream observes EOF. Returns the byte count. Single-threaded:
/// the caller decides task placement. Cancellation arrives through a
/// [`oxdock_process::CancelGuard`] that cancels the source backend
/// when the hub fires, so the blocking read ends without a tick.
pub fn pump_pipe_to_pipe<P: ProcessManager>(
    cx: &StepCtx<P>,
    from_pipe: &Value,
    to_pipe: &Value,
    cancel: &AtomicBool,
) -> DiagnosticResult<i64> {
    let reader = cx
        .pipe_reader(from_pipe)
        .map_err(ssh_diag::pump_borrow_source_failed)?;
    let writer = cx
        .pipe_writer(to_pipe)
        .map_err(ssh_diag::pump_borrow_target_failed)?;
    let hub: Arc<CancelHub> = cx.cancel_hub();
    let _guard = cx
        .pipe_backend(from_pipe)
        .map(|inner| oxdock_process::CancelGuard::arm(&hub, vec![inner]));
    let mut buffer = [0u8; PUMP_CHUNK];
    let mut total: i64 = 0;
    loop {
        if cancel.load(Ordering::SeqCst) || hub.is_cancelled() || cx.is_cancelled() {
            break;
        }
        match read_pump_chunk(&reader, &mut buffer) {
            Err(err) => {
                return Err(ssh_diag::pump_source_read_failed(&err));
            }
            Ok(0) => break,
            Ok(count) => {
                {
                    let mut guard = writer
                        .lock()
                        .map_err(|_| ssh_diag::pump_target_lock_poisoned())?;
                    guard
                        .write_all(&buffer[..count])
                        .map_err(ssh_diag::pump_target_write_failed)?;
                    guard.flush().map_err(ssh_diag::pump_target_flush_failed)?;
                }
                total += count as i64;
            }
        }
    }
    let _ = cx.close_pipe(to_pipe);
    Ok(total)
}

// Shared child-pump core for the `SSH_PTY_RUN` and `SSH_RUN` backends.
// A terminal child and a piped child differ only in spawn, teardown
// extras, and input-sink ownership; exit-first reporting, deferred
// closes, and cancel-kill must never diverge between them, so they
// share this supervisor. The wire pumps above stay separate: channel
// half-close has no child to reap.

/// Completion of one child-pump worker, by slot index into the
/// supervisor's closes (0 is always the input worker). Delivered
/// through [`guard_worker`]: panics report instead of stranding the
/// supervisor.
pub(crate) struct ChildDone(pub usize, pub std::thread::Result<DiagnosticResult<()>>);

/// The reaper observed child exit, carrying the code. Produced by the
/// backend's blocking wait on its own thread (ConPTY holds the master
/// open past child death, so no pipe EOF can stand in for it), which
/// is also where the wire exit-status report fires: the report always
/// precedes the EOF closes the supervisor applies after, so a fast
/// child never loses its status to the close.
pub(crate) struct ChildExited(pub i64);

/// One DSL pipe closed after its worker drains, gated on an observed
/// exit so the wire status always precedes the EOF.
pub(crate) struct DeferredClose<'a> {
    pub pipe: &'a Value,
    pub closed: bool,
}

/// Pump DSL-pipe bytes into a child stdin sink until pipe EOF,
/// cancellation, or `peer_done` (child gone: bytes would have nowhere
/// to go). Cancellation arrives as EOF through [`oxdock_pipe::PipeInner::cancel`].
pub(crate) fn pump_pipe_to_sink(
    reader: &SharedInput,
    sink: &mut Box<dyn Write + Send>,
    cancel: &AtomicBool,
    peer_done: &AtomicBool,
    func: &str,
) -> DiagnosticResult<()> {
    let mut buffer = [0u8; PUMP_CHUNK];
    loop {
        if cancel.load(Ordering::SeqCst) || peer_done.load(Ordering::SeqCst) {
            break;
        }
        match read_pump_chunk(reader, &mut buffer) {
            Err(err) => {
                return Err(ssh_diag::pump_func_input_read_failed(func, &err));
            }
            Ok(0) => break,
            Ok(count) => {
                if sink.write_all(&buffer[..count]).is_err() {
                    break;
                }
                if sink.flush().is_err() {
                    break;
                }
            }
        }
    }
    Ok(())
}

/// Apply deferred closes for finished output workers once the exit is
/// observed, so the wire status always precedes the EOF. Each entry is
/// (worker finished, deferred close). Returns true when every output
/// worker finished.
pub(crate) fn close_drained_outputs<P: ProcessManager>(
    cx: &StepCtx<P>,
    outputs: &mut [(bool, DeferredClose<'_>)],
    exit_observed: bool,
) -> bool {
    let mut all_done = true;
    for (finished, close) in outputs.iter_mut() {
        if !*finished {
            all_done = false;
            continue;
        }
        if !close.closed && exit_observed {
            close.closed = true;
            let _ = cx.close_pipe(close.pipe);
        }
    }
    all_done
}