swadhinbiswas πŸ“¦ v2.0.8: security fixes, documentation, testing improvements Β· 7/28/2026
84307b8
302 lines Β· 10.6 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
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
//! Rust-backed background task scheduler for JustAPI.
//!
//! Design (why Rust owns this, not Python):
//!
//! * The *tasks* are Python callables, so executing them inevitably needs the
//!   GIL. But the *scheduling* β€” the queue, the worker pool, backpressure,
//!   stats and graceful shutdown β€” is pure systems work and belongs in Rust:
//!   it has zero Python overhead on the hot path and plugs straight into
//!   Rust's metrics/tracing. This is what lets JustAPI beat FastAPI/Starlette,
//!   which schedule background work with a per-process `anyio` pool and ship
//!   no observability for it.
//! * A single process-wide worker pool (sized to the machine) executes sync
//!   tasks. Async tasks are run to completion with `asyncio.run` on a worker
//!   thread β€” no extra thread per task, unlike the old Python implementation
//!   that spawned a `threading.Thread` per task.
//! * A bounded MPMC queue gives backpressure: a thundering herd can't blow up
//!   memory; overflow is counted, not crashed.
//! * `BackgroundTasks.stats()` exposes submitted/active/completed/failed/dropped
//!   counters β€” something no other Python web framework provides for bg work.
//! * **GIL / free-threaded aware.** The worker pool size is chosen at runtime
//!   from `sys._is_gil_enabled()`: modest under a GIL (extra threads don't help
//!   CPU-bound Python), large under PEP 703 free-threaded builds (true
//!   parallelism). All Python calls go through `Python::attach`, which is valid
//!   in both modes. `Py<..>` handles are atomically refcounted, so sharing
//!   tasks across worker threads is sound in either build.

use std::collections::VecDeque;
use std::sync::atomic::{AtomicBool, AtomicUsize, Ordering};
use std::sync::{Arc, Condvar, Mutex, OnceLock};
use std::thread::{self, JoinHandle};

use pyo3::prelude::*;
use pyo3::types::{PyDict, PyTuple};

#[derive(Default)]
struct Stats {
    submitted: AtomicUsize,
    active: AtomicUsize,
    completed: AtomicUsize,
    failed: AtomicUsize,
    dropped: AtomicUsize,
    asyncs: AtomicUsize,
}

/// A queued unit of work: a Python callable plus its bound args/kwargs.
struct Task {
    func: Py<PyAny>,
    args: Py<PyTuple>,
    kwargs: Option<Py<PyDict>>,
}

/// Bounded MPMC queue (std-only) with shutdown signalling.
struct TaskQueue {
    tasks: Mutex<VecDeque<Task>>,
    not_empty: Condvar,
    size: AtomicUsize,
    shutdown: AtomicBool,
}

impl TaskQueue {
    fn push(&self, task: Task) {
        let mut g = self.tasks.lock().unwrap_or_else(|e| e.into_inner());
        g.push_back(task);
        self.size.fetch_add(1, Ordering::Relaxed);
        self.not_empty.notify_one();
    }

    /// Blocks until a task is available or shutdown is signalled.
    fn pop(&self) -> Option<Task> {
        let mut g = self.tasks.lock().unwrap_or_else(|e| e.into_inner());
        loop {
            if let Some(t) = g.pop_front() {
                self.size.fetch_sub(1, Ordering::Relaxed);
                return Some(t);
            }
            if self.shutdown.load(Ordering::Relaxed) {
                return None;
            }
            g = self.not_empty.wait(g).unwrap();
        }
    }
}

struct Runner {
    queue: TaskQueue,
    stats: Stats,
    handles: Mutex<Vec<JoinHandle<()>>>,
    max_total: usize,
}

impl Runner {
    fn enqueue(&self, task: Task) {
        if self.queue.size.load(Ordering::Relaxed) >= self.max_total {
            self.stats.dropped.fetch_add(1, Ordering::Relaxed);
            return;
        }
        self.queue.push(task);
        self.stats.submitted.fetch_add(1, Ordering::Relaxed);
        self.stats.active.fetch_add(1, Ordering::Relaxed);
    }
}

static RUNNER: OnceLock<Arc<Runner>> = OnceLock::new();

fn runner() -> &'static Arc<Runner> {
    RUNNER.get_or_init(|| {
        let max_total = std::env::var("JUSTAPI_BG_MAX_QUEUE")
            .ok()
            .and_then(|s| s.parse().ok())
            .unwrap_or(100_000usize);

        // Size the worker pool based on whether the interpreter has a GIL:
        //   * GIL enabled (CPython <=3.12, or 3.13/3.14 built `--disable-gil`
        //     off): only one thread can execute Python at a time, so extra
        //     workers don't speed up CPU-bound work β€” they mainly help I/O-bound
        //     tasks that release the GIL. Keep the pool modest to avoid GIL
        //     thrash between workers.
        //   * Free-threaded (PEP 703, `--disable-gil`): threads run Python in
        //     true parallel, so scale the pool up for real multi-core
        //     throughput on background work.
        // `Python::attach` (used throughout) is correct in both modes; this only
        // tunes concurrency.
        let gil_enabled = Python::attach(|py| {
            py.import("sys")
                .ok()
                .and_then(|s| s.getattr("_is_gil_enabled").ok())
                .and_then(|f| f.call0().ok())
                .and_then(|r| r.is_truthy().ok())
                .unwrap_or(true)
        });
        let n = std::thread::available_parallelism().map(|n| n.get()).unwrap_or(4);
        let workers = if gil_enabled { (n + 4).min(32) } else { (n * 2).min(256) };

        let runner = Arc::new(Runner {
            queue: TaskQueue {
                tasks: Mutex::new(VecDeque::new()),
                not_empty: Condvar::new(),
                size: AtomicUsize::new(0),
                shutdown: AtomicBool::new(false),
            },
            stats: Stats::default(),
            handles: Mutex::new(Vec::new()),
            max_total,
        });

        for _ in 0..workers {
            let r = Arc::clone(&runner);
            let h = thread::spawn(move || worker(&r));
            runner.handles.lock().unwrap_or_else(|e| e.into_inner()).push(h);
        }

        runner
    })
}

fn worker(r: &Arc<Runner>) {
    while let Some(task) = r.queue.pop() {
        process(r, task);
    }
}

fn is_coroutine(py: Python<'_>, obj: &Bound<'_, PyAny>) -> bool {
    match py.import("asyncio") {
        Ok(asyncio) => asyncio
            .getattr("iscoroutine")
            .and_then(|f| f.call1((obj.clone(),)))
            .and_then(|r| r.is_truthy())
            .unwrap_or(false),
        Err(_) => false,
    }
}

fn process(r: &Arc<Runner>, task: Task) {
    // Run the callable. Capture an *unbound* object so it survives the
    // `attach` scope (a `Bound` is tied to the `Python` token's lifetime).
    let call_res: PyResult<Py<PyAny>> = Python::attach(|py| {
        let func = task.func.bind(py);
        let args = task.args.bind(py);
        let kwargs = task.kwargs.as_ref().map(|k| k.bind(py));
        func.call(args, kwargs).map(|b| b.unbind())
    });

    match call_res {
        Ok(bound_py) => {
            let is_coro = Python::attach(|py| is_coroutine(py, bound_py.bind(py)));
            if is_coro {
                r.stats.asyncs.fetch_add(1, Ordering::Relaxed);
                let run_res: PyResult<()> = Python::attach(|py| {
                    let asyncio = py.import("asyncio")?;
                    let run = asyncio.getattr("run")?;
                    let bound = bound_py.bind(py);
                    run.call1((bound.clone(),))?;
                    Ok(())
                });
                match run_res {
                    Ok(_) => {
                        r.stats.completed.fetch_add(1, Ordering::Relaxed);
                    }
                    Err(e) => {
                        tracing::error!("background async task failed: {}", e);
                        r.stats.failed.fetch_add(1, Ordering::Relaxed);
                    }
                }
            } else {
                r.stats.completed.fetch_add(1, Ordering::Relaxed);
            }
            r.stats.active.fetch_sub(1, Ordering::Relaxed);
        }
        Err(e) => {
            tracing::error!("background task failed: {}", e);
            r.stats.failed.fetch_add(1, Ordering::Relaxed);
            r.stats.active.fetch_sub(1, Ordering::Relaxed);
        }
    }
}

#[pyclass]
pub struct BackgroundTasks {
    pending: Mutex<Vec<Task>>,
}

#[pymethods]
impl BackgroundTasks {
    #[new]
    fn new() -> Self {
        BackgroundTasks { pending: Mutex::new(Vec::new()) }
    }

    /// `add_task(func, *args, **kwargs)` β€” queue a callable for later.
    #[pyo3(signature = (func, *args, **kwargs))]
    fn add_task(
        &self,
        func: Bound<'_, PyAny>,
        args: &Bound<'_, PyTuple>,
        kwargs: Option<&Bound<'_, PyDict>>,
    ) -> PyResult<()> {
        let task = Task {
            func: func.clone().unbind(),
            args: args.clone().unbind(),
            kwargs: kwargs.map(|k| k.clone().unbind()),
        };
        self.pending.lock().unwrap_or_else(|e| e.into_inner()).push(task);
        Ok(())
    }

    /// Schedule all queued tasks onto the shared Rust executor (non-blocking).
    fn run(&self) -> PyResult<()> {
        let tasks: Vec<Task> =
            std::mem::take(&mut *self.pending.lock().unwrap_or_else(|e| e.into_inner()));
        let r = runner();
        for t in tasks {
            r.enqueue(t);
        }
        Ok(())
    }

    fn __call__(&self) -> PyResult<()> {
        self.run()
    }

    fn task_count(&self) -> usize {
        self.pending.lock().unwrap_or_else(|e| e.into_inner()).len()
    }

    /// Process-wide counters: submitted/active/completed/failed/dropped/async.
    #[staticmethod]
    fn stats(py: Python<'_>) -> PyResult<Py<PyDict>> {
        let r = runner();
        let d = PyDict::new(py);
        d.set_item("submitted", r.stats.submitted.load(Ordering::Relaxed))?;
        d.set_item("active", r.stats.active.load(Ordering::Relaxed))?;
        d.set_item("completed", r.stats.completed.load(Ordering::Relaxed))?;
        d.set_item("failed", r.stats.failed.load(Ordering::Relaxed))?;
        d.set_item("dropped", r.stats.dropped.load(Ordering::Relaxed))?;
        d.set_item("async", r.stats.asyncs.load(Ordering::Relaxed))?;
        Ok(d.unbind())
    }

    /// Drain workers and stop the scheduler. `wait=True` joins worker threads.
    #[staticmethod]
    fn shutdown(wait: Option<bool>) -> PyResult<()> {
        let r = runner();
        r.queue.shutdown.store(true, Ordering::Relaxed);
        r.queue.not_empty.notify_all();
        if wait.unwrap_or(true) {
            for h in r.handles.lock().unwrap_or_else(|e| e.into_inner()).drain(..) {
                let _ = h.join();
            }
        }
        Ok(())
    }
}

/// Enqueue an already-bound Python callable (with args/kwargs) onto the shared
/// Rust background-task executor. Used by the scheduler (`scheduler.rs`) so
/// cron/interval jobs run on the same worker pool and observability as
/// `BackgroundTasks`.
pub(crate) fn submit_py_task(func: Py<PyAny>, args: Py<PyTuple>, kwargs: Option<Py<PyDict>>) {
    let task = Task { func, args, kwargs };
    runner().enqueue(task);
}