swadhinbiswas WIP save-point: production hardening, H3 removal, fmt gate, and accumulated phase work · 7/15/2026
a293d8c
223 lines · 7.5 KB
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
use pyo3::prelude::*;
use pyo3::types::{PyDict, PyTuple};
use std::collections::{HashMap, HashSet};
use std::sync::Arc;
use tokio::sync::Notify;
use tokio_util::sync::CancellationToken;

#[pyclass(name = "DagNode", from_py_object)]
pub struct DagNode {
    pub name: String,
    pub handler: Py<PyAny>,
    pub dependencies: Vec<String>,
}

impl Clone for DagNode {
    fn clone(&self) -> Self {
        Python::<'_>::try_attach(|py| Self {
            name: self.name.clone(),
            handler: self.handler.clone_ref(py),
            dependencies: self.dependencies.clone(),
        })
        .expect("GIL required for Clone")
    }
}

#[pymethods]
impl DagNode {
    #[new]
    #[pyo3(signature = (name, handler, dependencies=None))]
    fn new(name: String, handler: Py<PyAny>, dependencies: Option<Vec<String>>) -> Self {
        Self { name, handler, dependencies: dependencies.unwrap_or_default() }
    }
}

#[pyclass(name = "Dag", from_py_object)]
#[derive(Clone)]
pub struct Dag {
    nodes: HashMap<String, DagNode>,
}

#[pymethods]
impl Dag {
    #[new]
    fn new(nodes: Vec<DagNode>) -> PyResult<Self> {
        let mut map = HashMap::new();
        for node in nodes {
            map.insert(node.name.clone(), node);
        }
        Ok(Self { nodes: map })
    }

    /// This function returns an `asyncio.Future` in Python.
    fn execute<'py>(
        &self,
        py: Python<'py>,
        inputs: &Bound<'py, PyDict>,
    ) -> PyResult<Bound<'py, PyAny>> {
        let nodes = self.nodes.clone();

        let mut state: HashMap<String, Py<PyAny>> = HashMap::new();
        for (k, v) in inputs.iter() {
            if let Ok(key_str) = k.extract::<String>() {
                state.insert(key_str, v.into());
            }
        }

        let state = Arc::new(tokio::sync::RwLock::new(state));
        let notify = Arc::new(Notify::new());
        let cancel = CancellationToken::new();

        // 1. Get asyncio loop and create a future
        let asyncio = py.import("asyncio")?;
        let loop_obj = asyncio.call_method0("get_event_loop")?;
        let future_obj = loop_obj.call_method0("create_future")?;

        let loop_py = loop_obj.into_any().unbind();
        let fut_py = future_obj.clone().unbind();

        // 2. Decide how to run it based on whether we are in a tokio context
        let handle = tokio::runtime::Handle::try_current();
        match handle {
            Ok(rt) => {
                rt.spawn(run_dag_internal(nodes, state, notify, cancel, loop_py, fut_py));
            }
            Err(_) => {
                std::thread::spawn(move || {
                    let rt = tokio::runtime::Runtime::new().unwrap();
                    rt.block_on(run_dag_internal(nodes, state, notify, cancel, loop_py, fut_py));
                });
            }
        }

        Ok(future_obj)
    }
}

async fn run_dag_internal(
    nodes: HashMap<String, DagNode>,
    state: Arc<tokio::sync::RwLock<HashMap<String, Py<PyAny>>>>,
    notify: Arc<Notify>,
    cancel: CancellationToken,
    loop_py: Py<PyAny>,
    fut_py: Py<PyAny>,
) {
    let mut pending: HashSet<String> = nodes.keys().cloned().collect();
    let mut completed: HashSet<String> = HashSet::new();
    let mut running: HashSet<String> = HashSet::new();
    let mut tasks = tokio::task::JoinSet::new();

    let result = loop {
        if pending.is_empty() && running.is_empty() {
            break Ok(());
        }

        let state_guard = state.read().await;
        let mut ready = Vec::new();
        for node_name in &pending {
            let node = &nodes[node_name];
            let can_run = node.dependencies.iter().all(|d| state_guard.contains_key(d));
            if can_run {
                ready.push(node_name.clone());
            }
        }
        drop(state_guard);

        for r in ready {
            pending.remove(&r);
            running.insert(r.clone());

            let node = nodes[&r].clone();
            let state_clone = state.clone();
            let notify_clone = notify.clone();

            tasks.spawn_blocking(move || {
                let res = Python::attach(|py| -> Result<Py<PyAny>, String> {
                    let sg = state_clone.blocking_read();
                    let mut args = Vec::new();
                    for dep in &node.dependencies {
                        if let Some(val) = sg.get(dep) {
                            args.push(val.clone_ref(py));
                        }
                    }
                    drop(sg);

                    let py_tuple = PyTuple::new(py, args).expect("tuple");
                    match node.handler.call1(py, py_tuple) {
                        Ok(val) => Ok(val),
                        Err(e) => Err(e.to_string()),
                    }
                });
                notify_clone.notify_waiters();
                (r, res)
            });
        }

        if !running.is_empty() {
            tokio::select! {
                Some(res) = tasks.join_next() => {
                    match res {
                        Ok((name, result)) => {
                            running.remove(&name);
                            completed.insert(name.clone());
                            match result {
                                Ok(py_obj) => {
                                    state.write().await.insert(name, py_obj);
                                    notify.notify_waiters();
                                }
                                Err(e) => {
                                    tracing::error!("DAG node {} failed: {}", name, e);
                                    cancel.cancel();
                                    break Err(format!("Node {} failed: {}", name, e));
                                }
                            }
                        }
                        Err(e) => {
                            tracing::error!("DAG task join error: {}", e);
                            cancel.cancel();
                            break Err(format!("Join error: {}", e));
                        }
                    }
                }
                _ = cancel.cancelled() => {
                    break Err("DAG cancelled".to_string());
                }
            }
        } else if !pending.is_empty() {
            break Err(format!(
                "DAG deadlock: nodes {:?} are waiting for dependencies that never arrive",
                pending
            ));
        }
    };

    let final_state = state.read().await;
    // 3. Resolve the asyncio future in Python
    Python::attach(|py| {
        let call_soon_ts = loop_py.getattr(py, "call_soon_threadsafe").unwrap();
        match result {
            Ok(_) => {
                let dict = PyDict::new(py);
                for (k, v) in final_state.iter() {
                    dict.set_item(k, v).unwrap();
                }
                let set_result = fut_py.getattr(py, "set_result").unwrap();
                if let Err(e) = call_soon_ts.call1(py, (set_result, dict)) {
                    tracing::error!("Failed to call set_result: {:?}", e);
                    e.print(py);
                }
            }
            Err(e) => {
                let set_exception = fut_py.getattr(py, "set_exception").unwrap();
                let exceptions = py.import("builtins").unwrap();
                let runtime_error = exceptions.getattr("RuntimeError").unwrap();
                let err_obj = runtime_error.call1((e,)).unwrap();
                if let Err(e) = call_soon_ts.call1(py, (set_exception, err_obj)) {
                    tracing::error!("Failed to call set_exception: {:?}", e);
                    e.print(py);
                }
            }
        }
    });
}