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
use std::time::{Duration, Instant};
use super::oneshot::{self, JoinState};
use super::process::{FlowId, FlowOutcome};
use super::worker::is_on_worker;
/// Why a host-side join refused to wait.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum JoinError {
/// [`FlowHandle::join`] (or a bounded variant) was called on a worker
/// thread. That deadlocks the M:N pool if the target needs this worker.
CalledFromWorker,
}
impl std::fmt::Display for JoinError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::CalledFromWorker => {
f.write_str("FlowHandle::join called from a Byteflow worker thread")
}
}
}
}
impl std::error::Error for JoinError {}
/// A reference to a spawned **flow**, returned by
/// [`super::runtime::Runtime::spawn`].
///
/// Holding a `FlowHandle` does not keep the flow alive — it is a way to
/// (a) read its [`FlowId`] for addressing with `Send`, and (b) block the
/// *calling native thread* until it finishes via [`FlowHandle::join`].
/// Dropping without joining is fine (fire-and-forget).
pub struct FlowHandle {
pub(crate) id: FlowId,
pub(crate) receiver: oneshot::Receiver<FlowOutcome>,
}
impl FlowHandle {
pub fn id(&self) -> FlowId {
self.id
}
/// Block the current (native) thread until the flow terminates.
///
/// **Never call from inside a worker / from a native invoked by bytecode.**
/// That case is machine-checked: this returns
/// [`FlowOutcome::Failed`] with [`JoinError::CalledFromWorker`] rather
/// than deadlocking the pool.
///
/// Returns [`FlowOutcome::Failed`] rather than blocking forever if the
/// flow was destroyed without producing an outcome.
/// [`super::runtime::Runtime::shutdown`] finalizes remaining flows
/// (`runtime shutdown`); a dropped sender without finalize is
/// [`super::error::RuntimeError::Abandoned`].
pub fn join(self) -> FlowOutcome {
if is_on_worker() {
return FlowOutcome::Failed(JoinError::CalledFromWorker.to_string());
}
match self.receiver.join() {
Ok(outcome) => outcome,
Err(e) => FlowOutcome::Failed(e.to_string()),
}
}
/// Same as [`Self::join`], but surfaces [`JoinError`] instead of folding
/// it into [`FlowOutcome::Failed`].
pub fn join_checked(self) -> Result<FlowOutcome, JoinError> {
if is_on_worker() {
return Err(JoinError::CalledFromWorker);
}
Ok(match self.receiver.join() {
Ok(outcome) => outcome,
Err(e) => FlowOutcome::Failed(e.to_string()),
})
}
/// The outcome if the flow has already terminated, `None` if it is still
/// running. Never waits.
///
/// For embedders that drive their own loop and cannot surrender a thread
/// to [`FlowHandle::join`]. Takes `&self`, so it can be polled until it
/// answers; note that the outcome is handed out exactly once, and a
/// further poll after that reports
/// [`super::error::RuntimeError::AlreadyCollected`] as
/// [`FlowOutcome::Failed`] rather than repeating it.
pub fn try_join(&self) -> Option<FlowOutcome> {
if is_on_worker() {
return Some(FlowOutcome::Failed(JoinError::CalledFromWorker.to_string()));
}
Self::settle(self.receiver.try_join())
}
/// Wait up to `timeout` for the flow to terminate; `None` if it is still
/// running when the bound elapses.
///
/// This is the variant to reach for in anything with a deadline — a
/// control loop, a watchdog, a test harness — since it is the only join
/// whose worst-case duration the caller chooses.
pub fn join_timeout(&self, timeout: Duration) -> Option<FlowOutcome> {
if is_on_worker() {
return Some(FlowOutcome::Failed(JoinError::CalledFromWorker.to_string()));
}
Self::settle(self.receiver.join_timeout(timeout))
}
/// [`FlowHandle::join_timeout`] against an absolute deadline, for callers
/// that already track one and must not have it drift across repeated
/// waits.
pub fn join_deadline(&self, deadline: Instant) -> Option<FlowOutcome> {
if is_on_worker() {
return Some(FlowOutcome::Failed(JoinError::CalledFromWorker.to_string()));
}
Self::settle(self.receiver.join_deadline(deadline))
}
/// Collapse a bounded-wait result into the handle's public vocabulary:
/// `None` for "still running", `Some(Failed)` for an infrastructure
/// error, since an embedder polling a handle has no separate error
/// channel to report one on.
fn settle(
state: Result<JoinState<FlowOutcome>, super::error::RuntimeError>,
) -> Option<FlowOutcome> {
match state {
Ok(JoinState::Ready(outcome)) => Some(outcome),
Ok(JoinState::Pending) => None,
Err(e) => Some(FlowOutcome::Failed(e.to_string())),
}
}
}
#[cfg(test)]
mod tests {
use super::super::worker::WorkerGuard;
use super::*;
#[test]
fn join_from_worker_is_rejected() {
let _guard = WorkerGuard::enter();
// Synthetic handle: receiver will never complete; we must not block.
let (_tx, rx) = oneshot::channel::<FlowOutcome>();
let handle = FlowHandle {
id: FlowId(1),
receiver: rx,
};
match handle.join_checked() {
Err(JoinError::CalledFromWorker) => {}
other => panic!("expected CalledFromWorker, got {other:?}"),
}
}
}