strop-remote 0.19.1

Owned SSH remote-file transport and native path identity for strop
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
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
//! The connection pool: one actor per endpoint, weak manager retention and
//! strong `ConnectionLease` handles. No child exists until a worker
//! acquires a session; the last owner signals stop and the actor itself
//! reaps. The actor owns its codec, runtime and process exclusively — no
//! concurrent writes to one connection are possible — and serializes every
//! job. Cancellation domains are separate: a queued job's cancellation only
//! skips that job, while an active one retires the physical connection and
//! its epoch, fails that request honestly, and never replays it silently.

mod lifecycle;
use crate::address::{RemoteEndpoint, RemoteFile, RemoteLocation};
use crate::client::{ConnectionLease, RemoteDirectorySnapshot, RemoteEntry, RemoteSnapshot};
use crate::selection::ReadSelection;
use crate::transport::{Fault, ReadFailureKind, ReadStage, RemoteReadError};
pub(crate) use lifecycle::{SessionState, StatusCell, StopSignal};
use parking_lot::RwLock;
use std::cell::Cell;
use std::collections::HashMap;
use std::future::Future;
use std::sync::mpsc::{Receiver, SyncSender};
use std::sync::{Arc, Weak};
use std::time::Duration;
use strop_core::worker::CancelToken;

#[cfg(unix)]
use crate::transport::{process::Physical, session};

/// Jobs one endpoint actor will hold before admission blocks a worker.
pub(crate) const QUEUE_CAPACITY: usize = 32;
/// How often an idle actor re-checks its stop signal.
const IDLE_POLL: Duration = Duration::from_millis(100);

/// What one admitted job asks the actor to do.
pub(crate) enum Request {
    Read {
        location: RemoteLocation,
        selection: ReadSelection,
    },
    /// Explicit open: routes by the target's attributes and returns the
    /// typed file or directory resource.
    Open {
        location: RemoteLocation,
        selection: ReadSelection,
    },
    /// Explicit browsing action: may resolve a home query and connect.
    List { location: RemoteLocation },
    /// Completion path: must find a live authenticated session; it never
    /// connects or authenticates on its own.
    ListConnected { directory: RemoteFile },
    /// Explicit user connection: establish and hold a session.
    Connect,
}

impl Request {
    /// The remote identity diagnostics name for this job.
    fn remote(&self) -> String {
        match self {
            Self::Read { location, .. } | Self::Open { location, .. } | Self::List { location } => {
                location.to_string()
            }
            Self::ListConnected { directory } => directory.to_string(),
            Self::Connect => String::new(),
        }
    }
}

pub(crate) enum Reply {
    Snapshot(Box<RemoteSnapshot>),
    Directory(RemoteDirectorySnapshot),
    Entries(Vec<RemoteEntry>),
    Lease(ConnectionLease),
}

/// One admitted job. It owns a lease from admission until it fails or
/// transfers that lease into its reply, so shutdown can never sneak
/// between acquiring the endpoint and delivering the result.
pub(crate) struct Job {
    pub(crate) lease: ConnectionLease,
    pub(crate) request: Request,
    pub(crate) cancel: CancelToken,
    pub(crate) reply: std::sync::mpsc::Sender<Result<Reply, RemoteReadError>>,
}

/// The manager-facing handle: everything a worker needs to talk to one
/// endpoint's actor. The registry holds only weak references; leases hold
/// the strong ones.
pub(crate) struct EndpointHandle {
    endpoint: RemoteEndpoint,
    jobs: SyncSender<Job>,
    stop: Arc<StopSignal>,
    status: Arc<StatusCell>,
}

impl EndpointHandle {
    pub(crate) fn endpoint(&self) -> &RemoteEndpoint {
        &self.endpoint
    }

    pub(crate) fn jobs(&self) -> &SyncSender<Job> {
        &self.jobs
    }

    pub(crate) fn stop_signal(&self) -> &Arc<StopSignal> {
        &self.stop
    }

    pub(crate) fn status(&self) -> SessionState {
        self.status.get()
    }

    pub(crate) fn wait_stopped(&self) -> Result<(), RemoteReadError> {
        self.stop.wait()
    }

    /// Signal the actor to stop. Fire and forget: the actor owns teardown.
    pub(crate) fn signal_stop(&self) {
        self.stop.signal();
    }
}

impl std::fmt::Debug for EndpointHandle {
    fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        formatter
            .debug_struct("EndpointHandle")
            .field("endpoint", &self.endpoint)
            .field("status", &self.status.get())
            .finish()
    }
}

impl Drop for EndpointHandle {
    /// Last lease released: signal stop only. No lock, no I/O, no wait —
    /// the actor reaps its own child asynchronously.
    fn drop(&mut self) {
        self.stop.signal();
    }
}

/// Weak-retention registry of endpoint actors. Looked up and inserted on
/// workers; never on input or render.
#[derive(Default)]
pub(crate) struct Registry {
    slots: RwLock<HashMap<RemoteEndpoint, Weak<EndpointHandle>>>,
}

impl Registry {
    fn upgrade(&self, endpoint: &RemoteEndpoint) -> Option<Arc<EndpointHandle>> {
        self.slots
            .read()
            .get(endpoint)
            .and_then(Weak::upgrade)
            .filter(|handle| !handle.stop.signalled())
    }

    pub(crate) fn connected_handle(
        &self,
        endpoint: &RemoteEndpoint,
    ) -> Option<Arc<EndpointHandle>> {
        self.upgrade(endpoint)
            .filter(|handle| handle.status() == SessionState::Connected)
    }

    /// Look up — or create exactly once — the actor for one endpoint.
    /// The writer mutex covers construction and publication only; no
    /// asynchronous admission or I/O happens under it.
    pub(crate) fn acquire(
        &self,
        endpoint: &RemoteEndpoint,
    ) -> Result<Arc<EndpointHandle>, RemoteReadError> {
        #[cfg(not(unix))]
        {
            let _ = endpoint;
            return Err(RemoteReadError::bare(
                ReadStage::Session,
                ReadFailureKind::Unsupported,
                "remote sessions require Unix process supervision",
            ));
        }
        #[cfg(unix)]
        {
            if let Some(handle) = self.upgrade(endpoint) {
                return Ok(handle);
            }
            let mut slots = self.slots.write();
            if let Some(handle) = slots
                .get(endpoint)
                .and_then(Weak::upgrade)
                .filter(|handle| !handle.stop.signalled())
            {
                return Ok(handle);
            }
            let handle = spawn_actor(endpoint.clone()).map_err(|error| {
                RemoteReadError::bare(
                    ReadStage::Session,
                    ReadFailureKind::Io,
                    format!("starting the session actor: {error}"),
                )
            })?;
            // Prune dead weak entries while the writer lock is held.
            slots.retain(|_, weak| weak.strong_count() > 0);
            slots.insert(endpoint.clone(), Arc::downgrade(&handle));
            Ok(handle)
        }
    }

    /// Remove one endpoint's slot and signal its actor to stop. Later work
    /// re-creates a fresh actor (a new incarnation).
    pub(crate) fn remove(&self, endpoint: &RemoteEndpoint) -> Option<Arc<EndpointHandle>> {
        let handle = self
            .slots
            .write()
            .remove(endpoint)
            .and_then(|weak| weak.upgrade());
        if let Some(handle) = &handle {
            handle.signal_stop();
        }
        handle
    }

    /// Remove every slot and stop every actor.
    pub(crate) fn clear(&self) -> Vec<Arc<EndpointHandle>> {
        let handles: Vec<Arc<EndpointHandle>> = self
            .slots
            .write()
            .drain()
            .filter_map(|(_, weak)| weak.upgrade())
            .collect();
        for handle in &handles {
            handle.signal_stop();
        }
        handles
    }

    /// Endpoints with a live, connected actor. Observational only: the
    /// result can become stale immediately and is not a liveness
    /// guarantee. Returns owned metadata, never leases.
    pub(crate) fn connected(&self) -> Vec<RemoteEndpoint> {
        self.slots
            .read()
            .values()
            .filter_map(|weak| weak.upgrade())
            .filter(|handle| handle.status() == SessionState::Connected)
            .map(|handle| handle.endpoint().clone())
            .collect()
    }
}

/// Everything the actor thread owns. It never holds a strong handle to
/// itself, or it would keep its own session alive forever.
struct Actor {
    receiver: Receiver<Job>,
    stop: Arc<StopSignal>,
    status: Arc<StatusCell>,
    /// Builds one fresh SSH command per physical connection from the
    /// single crate SSH policy.
    spawner: Box<dyn Fn() -> std::process::Command + Send>,
}

/// Spawn the dedicated actor thread for one endpoint incarnation. The
/// thread is detached: its lifetime is governed by the stop signal and the
/// job channel, never by a joiner.
#[cfg(unix)]
fn spawn_actor(endpoint: RemoteEndpoint) -> Result<Arc<EndpointHandle>, String> {
    let (jobs, receiver) = std::sync::mpsc::sync_channel(QUEUE_CAPACITY);
    let stop = Arc::new(StopSignal::new());
    let status = Arc::new(StatusCell::default());
    let policy_endpoint = endpoint.clone();
    let spawner: Box<dyn Fn() -> std::process::Command + Send> =
        Box::new(move || crate::ssh::sftp_subsystem(&policy_endpoint));
    let handle = Arc::new(EndpointHandle {
        endpoint,
        jobs,
        stop: stop.clone(),
        status: status.clone(),
    });
    let actor = Actor {
        receiver,
        stop,
        status,
        spawner,
    };
    std::thread::Builder::new()
        .name("strop-remote-session".to_owned())
        .spawn(move || actor_loop(actor))
        .map(drop)
        .map_err(|error| error.to_string())?;
    Ok(handle)
}

/// The actor state machine:
///
/// ```text
/// Disconnected --dequeue live job--> Connecting(epoch) --> Working
/// Working --success--> ConnectedIdle --stop--> teardown --> exit
/// Working --transport/protocol error or cancel--> teardown --> Disconnected
/// ```
#[cfg(unix)]
fn actor_loop(actor: Actor) {
    let Actor {
        receiver,
        stop,
        status,
        spawner,
    } = actor;
    let _exit = lifecycle::ActorExit(stop.clone());
    let mut next_epoch: u64 = 0;
    let mut physical: Option<Physical> = None;
    loop {
        if stop.signalled() {
            break;
        }
        match receiver.recv_timeout(IDLE_POLL) {
            Ok(job) => {
                // Cancellation observed before dispatch skips the job; the
                // physical connection is never touched.
                if job.cancel.is_cancelled() {
                    refuse(job, cancelled_before_dispatch());
                    continue;
                }
                dispatch(
                    &stop,
                    &status,
                    &*spawner,
                    &mut next_epoch,
                    &mut physical,
                    job,
                );
            }
            Err(std::sync::mpsc::RecvTimeoutError::Timeout) => continue,
            Err(std::sync::mpsc::RecvTimeoutError::Disconnected) => break,
        }
    }
    let outcome = physical.take().map_or(Ok(()), Physical::retire);
    status.set(SessionState::Disconnected);
    stop.complete(outcome);
}

#[cfg(unix)]
fn cancelled_before_dispatch() -> RemoteReadError {
    RemoteReadError::bare(
        ReadStage::Session,
        ReadFailureKind::Cancelled,
        "the job was cancelled before the session picked it up",
    )
}

#[cfg(unix)]
fn not_connected() -> RemoteReadError {
    RemoteReadError::bare(
        ReadStage::Session,
        ReadFailureKind::NotConnected,
        "no authenticated session exists for this endpoint, and this \
         operation never starts one",
    )
}

fn refuse(job: Job, error: RemoteReadError) {
    // A gone receiver simply drops the job (and its lease).
    let _ = job.reply.send(Err(error));
}

/// Process one job to completion. Connection policy: clean SFTP refusals
/// fail only this job; transport/protocol failures, cancellation, stop and
/// failed cleanup retire the physical connection. Nothing is retried
/// silently — a later job may establish a new epoch on its own behalf.
#[cfg(unix)]
fn dispatch(
    stop: &Arc<StopSignal>,
    status: &Arc<StatusCell>,
    spawner: &dyn Fn() -> std::process::Command,
    next_epoch: &mut u64,
    physical: &mut Option<Physical>,
    job: Job,
) {
    let remote = job.request.remote();
    // Completion-path jobs must find a live session: never connect here.
    if matches!(job.request, Request::ListConnected { .. }) && physical.is_none() {
        refuse(job, not_connected());
        return;
    }
    let mut spawned_here = false;
    if physical.is_none() {
        let Some(epoch) = next_epoch.checked_add(1) else {
            refuse(
                job,
                RemoteReadError::bare(
                    ReadStage::Session,
                    ReadFailureKind::Protocol,
                    "connection epoch exhausted",
                ),
            );
            return;
        };
        *next_epoch = epoch;
        status.set(SessionState::Connecting);
        let mut command = spawner();
        match Physical::connect(&mut command, &job.cancel, stop, *next_epoch, &remote) {
            Ok(connection) => {
                physical.replace(connection);
                spawned_here = true;
                status.set(SessionState::Connected);
            }
            Err(error) => {
                status.set(SessionState::Disconnected);
                // `Physical::connect` already consumed, terminated and
                // reaped the child; the spawning job's hook is spent.
                job.cancel.clear_cancel_resource();
                refuse(job, error);
                return;
            }
        }
    }
    let Some(connection) = physical.as_mut() else {
        refuse(
            job,
            RemoteReadError::bare(
                ReadStage::Session,
                ReadFailureKind::Protocol,
                "connected actor has no physical session",
            ),
        );
        return;
    };
    debug_assert_eq!(connection.epoch, *next_epoch);
    let outcome = execute(connection, &job, stop);
    if spawned_here {
        // The connection outlives this job: detach its kill hook so a
        // later cancellation of this job's owner cannot kill a connection
        // now serving others.
        job.cancel.clear_cancel_resource();
    }
    match outcome {
        Ok(reply) => {
            // Cancellation has priority over an uncommitted success; a
            // clean success leaves the connection sound.
            if job.cancel.is_cancelled() || stop.signalled() {
                let fault = if stop.signalled() && !job.cancel.is_cancelled() {
                    Fault::stopped(ReadStage::Teardown)
                } else {
                    Fault::cancelled(ReadStage::Teardown)
                };
                let fault = if let Some(connection) = physical.take() {
                    match connection.retire() {
                        Ok(()) => fault,
                        Err(error) => fault.with_cleanup(Fault::new(
                            ReadStage::Teardown,
                            error.kind(),
                            error.to_string(),
                        )),
                    }
                } else {
                    fault
                };
                status.set(SessionState::Disconnected);
                refuse(job, RemoteReadError::fault(&remote, fault));
            } else {
                let _ = job.reply.send(Ok(reply));
            }
        }
        Err(mut fault) => {
            let diagnostics = physical.as_ref().and_then(Physical::diagnostics);
            let (kind, poison) = fault.disposition();
            if poison || !connection_survives(kind) {
                if let Some(connection) = physical.take() {
                    if let Err(error) = connection.retire() {
                        fault = fault.with_cleanup(Fault::new(
                            ReadStage::Teardown,
                            error.kind(),
                            error.to_string(),
                        ));
                    }
                }
                status.set(SessionState::Disconnected);
            }
            refuse(
                job,
                RemoteReadError::fault(&remote, fault).stderr(diagnostics),
            );
        }
    }
}

/// Clean SFTP refusals that leave the framing sound: the connection stays.
#[cfg(unix)]
fn connection_survives(kind: ReadFailureKind) -> bool {
    matches!(
        kind,
        ReadFailureKind::NotFound
            | ReadFailureKind::Permission
            | ReadFailureKind::NotRegularFile
            | ReadFailureKind::NotDirectory
            | ReadFailureKind::UnknownLength
            | ReadFailureKind::TooLarge
            | ReadFailureKind::InvalidUtf8
            | ReadFailureKind::HomeUnsupported
            | ReadFailureKind::TooManyEntries
    )
}

/// Run one job's protocol script on the actor's connection.
#[cfg(unix)]
fn execute(physical: &mut Physical, job: &Job, stop: &Arc<StopSignal>) -> Result<Reply, Fault> {
    use crate::client::RemoteEntryKind;

    let stage = Cell::new(ReadStage::Connect);
    let Physical {
        runtime,
        codec,
        advertised,
        ..
    } = physical;
    let cancel = &job.cancel;
    let lease = job.lease.clone();
    match &job.request {
        Request::Read {
            location,
            selection,
        } => {
            let file = block_on_guarded(
                runtime,
                session::resolve_location(codec, location, advertised),
                cancel,
                stop,
                &stage,
            )?;
            let (text, window) = block_on_guarded(
                runtime,
                session::read_selection(codec, &file, selection, &stage),
                cancel,
                stop,
                &stage,
            )?;
            let mut buffer = strop_core::Buffer::from_text(&text);
            // A remote snapshot is never writable; provenance beyond this
            // flag is the caller's document identity.
            buffer.readonly = true;
            Ok(Reply::Snapshot(Box::new(RemoteSnapshot {
                file,
                buffer,
                window,
                connection: lease,
            })))
        }
        Request::Open {
            location,
            selection,
        } => {
            let file = block_on_guarded(
                runtime,
                session::resolve_location(codec, location, advertised),
                cancel,
                stop,
                &stage,
            )?;
            let kind = block_on_guarded(
                runtime,
                session::path_kind(codec, &file, &stage),
                cancel,
                stop,
                &stage,
            )?;
            match kind {
                RemoteEntryKind::Directory => {
                    let entries = block_on_guarded(
                        runtime,
                        session::list_entries(codec, &file, &stage),
                        cancel,
                        stop,
                        &stage,
                    )?;
                    Ok(Reply::Directory(RemoteDirectorySnapshot {
                        directory: file,
                        entries,
                        connection: lease,
                    }))
                }
                RemoteEntryKind::File => {
                    let (text, window) = block_on_guarded(
                        runtime,
                        session::read_selection(codec, &file, selection, &stage),
                        cancel,
                        stop,
                        &stage,
                    )?;
                    let mut buffer = strop_core::Buffer::from_text(&text);
                    buffer.readonly = true;
                    Ok(Reply::Snapshot(Box::new(RemoteSnapshot {
                        file,
                        buffer,
                        window,
                        connection: lease,
                    })))
                }
                _ => Err(Fault::new(
                    ReadStage::Inspect,
                    ReadFailureKind::NotRegularFile,
                    "the target is neither a regular file nor a directory",
                )),
            }
        }
        Request::List { location } => {
            let directory = block_on_guarded(
                runtime,
                session::resolve_location(codec, location, advertised),
                cancel,
                stop,
                &stage,
            )?;
            let kind = block_on_guarded(
                runtime,
                session::path_kind(codec, &directory, &stage),
                cancel,
                stop,
                &stage,
            )?;
            if kind != RemoteEntryKind::Directory {
                return Err(Fault::new(
                    ReadStage::Open,
                    ReadFailureKind::NotDirectory,
                    "listing requires a directory",
                ));
            }
            let entries = block_on_guarded(
                runtime,
                session::list_entries(codec, &directory, &stage),
                cancel,
                stop,
                &stage,
            )?;
            Ok(Reply::Directory(RemoteDirectorySnapshot {
                directory,
                entries,
                connection: lease,
            }))
        }
        Request::ListConnected { directory } => {
            let entries = block_on_guarded(
                runtime,
                session::list_entries(codec, directory, &stage),
                cancel,
                stop,
                &stage,
            )?;
            Ok(Reply::Entries(entries))
        }
        Request::Connect => Ok(Reply::Lease(lease)),
    }
}

/// Drive one script under the cancellation/stop/deadline race.
#[cfg(unix)]
fn block_on_guarded<T, F>(
    runtime: &tokio::runtime::Runtime,
    work: F,
    cancel: &CancelToken,
    stop: &Arc<StopSignal>,
    stage: &Cell<ReadStage>,
) -> Result<T, Fault>
where
    F: Future<Output = Result<T, Fault>>,
{
    runtime.block_on(session::guarded(work, cancel, stop, stage))
}

#[cfg(test)]
#[path = "pool_tests.rs"]
mod tests;