Skip to main content

hyprstream_core/services/oauth/
account_worker.rs

1//! Blocking account I/O is contained by a dedicated OAuth process.
2//!
3//! A Rust thread cannot be safely killed while inside filesystem/audit I/O.
4//! Keep its actual handle and join completion; an overdue or abandoned worker
5//! terminates the dedicated process instead of detaching a live listener. The
6//! OAuth entrypoint checks launcher-provided containment before creating one.
7
8use std::{thread::JoinHandle, time::Duration};
9
10use hyprstream_rpc::error::RpcError;
11use tokio::sync::oneshot;
12
13pub(super) const WORKER_FAILURE_EXIT: i32 = 70;
14
15fn terminate(name: &str) -> ! {
16    tracing::error!(
17        worker = name,
18        "account worker did not stop; terminating dedicated OAuth process"
19    );
20    std::process::exit(WORKER_FAILURE_EXIT);
21}
22
23pub(super) struct ContainedWorker<T> {
24    name: &'static str,
25    thread: Option<JoinHandle<()>>,
26    completion: oneshot::Receiver<T>,
27}
28
29impl<T: Send + 'static> ContainedWorker<T> {
30    pub(super) fn spawn(
31        name: &'static str,
32        work: impl FnOnce() -> T + Send + 'static,
33    ) -> Result<Self, RpcError> {
34        let (tx, completion) = oneshot::channel();
35        let thread = std::thread::Builder::new()
36            .name(name.to_owned())
37            .spawn(move || {
38                let result = work();
39                let _ = tx.send(result);
40            })
41            .map_err(|error| RpcError::SpawnFailed(format!("{name} thread spawn: {error}")))?;
42        Ok(Self {
43            name,
44            thread: Some(thread),
45            completion,
46        })
47    }
48
49    pub(super) fn is_joined(&self) -> bool {
50        self.thread.is_none()
51    }
52
53    pub(super) async fn result(&mut self) -> Result<T, RpcError> {
54        let result = (&mut self.completion).await;
55        // The sender runs after work (including runtime destruction) returns.
56        // Its only remaining operation is returning from the thread closure.
57        if let Some(thread) = self.thread.take() {
58            thread
59                .join()
60                .map_err(|_| RpcError::SpawnFailed(format!("{} thread panicked", self.name,)))?;
61        }
62        result.map_err(|_| {
63            RpcError::SpawnFailed(format!("{} thread exited without a result", self.name,))
64        })
65    }
66
67    pub(super) async fn finish(&mut self, timeout: Duration) -> Result<T, RpcError> {
68        match tokio::time::timeout(timeout, self.result()).await {
69            Ok(result) => result,
70            Err(_) => terminate(self.name),
71        }
72    }
73}
74
75impl<T> Drop for ContainedWorker<T> {
76    fn drop(&mut self) {
77        if let Some(thread) = self.thread.take() {
78            if !thread.is_finished() {
79                // Covers cancellation/unwind before common teardown as well.
80                terminate(self.name);
81            }
82            let _ = thread.join();
83        }
84    }
85}
86
87#[cfg(test)]
88pub(super) mod tests {
89    #![allow(clippy::unwrap_used, clippy::expect_used)]
90
91    use super::*;
92    use std::{net::TcpListener, process::Command, time::Instant};
93
94    pub(crate) fn is_child(test: &str) -> bool {
95        std::env::var("HYPRSTREAM_CONTAINED_WORKER_TEST").as_deref() == Ok(test)
96    }
97
98    pub(crate) fn expect_contained_exit(test: &str, env: &[(&str, String)]) {
99        let mut command = Command::new(std::env::current_exe().unwrap());
100        command
101            .args(["--exact", test, "--nocapture"])
102            .env("HYPRSTREAM_CONTAINED_WORKER_TEST", test);
103        for (key, value) in env {
104            command.env(key, value);
105        }
106        let mut child = command.spawn().unwrap();
107        let deadline = Instant::now() + Duration::from_secs(5);
108        loop {
109            if let Some(status) = child.try_wait().unwrap() {
110                assert_eq!(status.code(), Some(WORKER_FAILURE_EXIT));
111                break;
112            }
113            if Instant::now() >= deadline {
114                let _ = child.kill();
115                let _ = child.wait();
116                panic!("contained worker process failed to exit within deadline");
117            }
118            std::thread::sleep(Duration::from_millis(10));
119        }
120    }
121
122    #[tokio::test]
123    async fn completed_worker_is_joined() {
124        let mut worker = ContainedWorker::spawn("test-completes", || 42).unwrap();
125        assert_eq!(worker.finish(Duration::from_secs(1)).await.unwrap(), 42);
126        assert!(worker.is_joined());
127    }
128
129    #[tokio::test(flavor = "current_thread")]
130    async fn early_shutdown_joins_real_listener_and_releases_port() {
131        let listener = TcpListener::bind("127.0.0.1:0").unwrap();
132        listener.set_nonblocking(true).unwrap();
133        let addr = listener.local_addr().unwrap();
134        let shutdown = std::sync::Arc::new(tokio::sync::Notify::new());
135        // Signal before the worker registers: the permit must survive.
136        shutdown.notify_one();
137        let mut worker = ContainedWorker::spawn("test-listener-stop", move || {
138            let runtime = tokio::runtime::Builder::new_current_thread()
139                .enable_all()
140                .build()
141                .unwrap();
142            runtime.block_on(async move {
143                let bound = crate::server::tls::BoundHttpListener::Http(
144                    tokio::net::TcpListener::from_std(listener).unwrap(),
145                );
146                crate::server::tls::serve_bound(
147                    bound,
148                    axum::Router::new(),
149                    shutdown,
150                    "AccountHttpTest",
151                )
152                .await
153            })
154        })
155        .unwrap();
156        worker
157            .finish(Duration::from_secs(2))
158            .await
159            .unwrap()
160            .unwrap();
161        assert!(worker.is_joined());
162        TcpListener::bind(addr).expect("joined listener must release its socket");
163    }
164
165    #[tokio::test(flavor = "current_thread")]
166    async fn listener_timeout_terminates_process_and_releases_port() {
167        const TEST: &str = "services::oauth::account_worker::tests::listener_timeout_terminates_process_and_releases_port";
168        if !is_child(TEST) {
169            let listener = TcpListener::bind("127.0.0.1:0").unwrap();
170            let addr = listener.local_addr().unwrap();
171            drop(listener);
172            expect_contained_exit(
173                TEST,
174                &[("HYPRSTREAM_CONTAINED_WORKER_ADDR", addr.to_string())],
175            );
176            TcpListener::bind(addr).expect("terminated worker must release its listening socket");
177            return;
178        }
179        let addr = std::env::var("HYPRSTREAM_CONTAINED_WORKER_ADDR").unwrap();
180        let (ready_tx, ready_rx) = std::sync::mpsc::channel();
181        let mut worker = ContainedWorker::spawn("test-stuck-listener", move || {
182            let _listener = TcpListener::bind(addr).unwrap();
183            ready_tx.send(()).unwrap();
184            loop {
185                std::thread::park();
186            }
187        })
188        .unwrap();
189        ready_rx.recv_timeout(Duration::from_secs(1)).unwrap();
190        let _ = worker.finish(Duration::from_millis(20)).await;
191        panic!("a live timed-out listener must not be detached");
192    }
193
194    #[test]
195    fn cancellation_cannot_detach_live_worker() {
196        const TEST: &str =
197            "services::oauth::account_worker::tests::cancellation_cannot_detach_live_worker";
198        if !is_child(TEST) {
199            expect_contained_exit(TEST, &[]);
200            return;
201        }
202        let worker = ContainedWorker::spawn("test-cancelled-worker", || loop {
203            std::thread::park();
204        })
205        .unwrap();
206        drop(worker);
207        panic!("dropping a live worker must terminate its process");
208    }
209}