-
Notifications
You must be signed in to change notification settings - Fork 794
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
F: Ungil
on nightly insufficient for soundness of allow_threads
, even without scoped-tls
or send_wrapper
#3658
Comments
To support things like subinterpreters and "no gil" pyclasses must be |
And now, here’s another argument why use std::cell::Cell;
use std::thread;
use pyo3::prelude::*;
use pyo3::sync::GILProtected;
fn main() -> PyResult<()> {
Python::with_gil(|py| {
let protected = GILProtected::new(Cell::new(0));
let cell: &Cell<u8> = protected.get(py);
py.allow_threads(|| {
thread::scope(|s| {
let t = s.spawn(|| {
Python::with_gil(|py| {
let cell: &Cell<u8> = protected.get(py);
let init = cell.get();
loop {
let gotten = cell.get();
if init != gotten {
println!("concurrently modified! - {init} vs {gotten}");
break;
}
}
});
});
loop {
cell.set(cell.get().wrapping_add(1));
if t.is_finished() {
break;
}
}
})
});
PyResult::Ok(())
})?;
PyResult::Ok(())
} |
Thank you @steffahn, your scrutiny here is invaluable! Given that nogil Python is now seemingly on track for Python 3.13, I would be in favour of moving fast here and requiring If I follow the reasoning on the second example, this implies that (Although a slight weakening of that point is that with Python nogil I assume we need to rework |
To make subinterpreters and no-gil work we'd need to get rid of GilProtected too |
Somewhat off-topic for subinterpreters, I guess As for nogil, I suspect we'll want to provide a sort of "GIL emulation" using a normal mutex so that Rust-build extensions could work the same for gil and nogil builds. Or we drop any GIL-based synchronization outright, but I guess that would force everybody into paying the cost even if they are happy with single-threaded Python. (At the danger of outing myself, I will miss Python as a single-threaded language avoiding the complexities of parallelism. I am not convinced that emphasizing throughput over efficiency is the right thing to do for almost all current Python users, but mainly for large organizations with AI-related investments into Python-based technology stacks.) |
Thinking further, it seems to me the root cause of the problems we're running into here are down to the GIL marker being overloaded, it's currently in use as "safe to call Python APIs" as well as a synchronization primitive. In a nogil-only world in the long run it may simplify things to be able to remove that latter use.
Ooof, maybe, I was sort of hoping that we wouldn't change how they worked for subinterpreters, but I guess if we want interpreter isolation, that's a possible way to do it.
You're not the only one I know who holds this view, even if I myself am quite optimistic for nogil. I think the SC would never have accepted the proposal if the belief wasn't that the single thread efficiency losses are minimal, and that most Python code won't need to have new adjustments to be thread safe. |
I suspect that we will need to keep something around, because I think mutex + GIL has been proven too many times that it's a recipe for deadlocks. That said, we could just offer |
This is going off the rails where do we find the space to discuss this stuff? I think the above is realistic, i.e. the quantitative losses will be bearable. But I also think the real price is something different, namely the reduced audience for people making changes to Python, i.e. to mostly people doing this in a professional capacity and therefore having the context window to learn and manage the new complexities around e.g. reference counting. But I also admit that this change is happening already independently of the nogil changes, e.g. the JIT-like late optimizations are probably a similar barrier to entry, implemented for similar reasons as the nogil changes and similarly well-received in the community. So my main gripe is that I suspect that many Python users will loose the ability to fully understand and shape their own tools. |
(concurrent modification of a
Cell
is UB; I skipped the additional step where you could abuse this data race to cause more obvious UB such as reading memory out of bounds)This is issue is possibly irrelevant in light of existing issues like #3640 (and fixed by #3646 which abandons
Ungil
). Also, it might in principle also be fixable by making.borrow
do a dynamic check that we’re in the same thread as previous.borrow
; or by demandingSync
forpyclass
; or letting the user choose between either of the two options (dynamic check orSync
).I’m raising this issue as another point of argument that nightly
Ungil
isn’t as good as it claims to be, and this would need to be addressed in case the argument would win that we should keepUngil
(with documented unsoundness warnings) for its benefits of not conservatively requiringSend
.The text was updated successfully, but these errors were encountered: