Repository navigation
gh-162529: Run function watchers under the function's critical section - #162530
alexmalyshev wants to merge 1 commit into
Conversation
…section This ensures that function events arrive in the same order as their corresponding stores, and a watcher can read the function's fields without racing other setters (provided the watcher callback doesn't block and suspend the critical section). An alternative would be to call the callback with the world stopped, but that's dangerous. The callback could inadvertently re-enter Python code and deadlock trying to stop the world again (e.g. triggering GC). This is a behavior difference for users of function watchers on free-threaded builds of CPython. It has no effect on users of the default build.
| PyObject *old_value; | ||
| Py_BEGIN_CRITICAL_SECTION(func); | ||
| handle_func_event(event, func, new_value); | ||
| _PyEval_StopTheWorld(interp); |
There was a problem hiding this comment.
I think this opens up the opportunity for us to release the critical section, have the function be further modified before we stop the world, and then when we resume w/ the world stopped the function is modified from before we do the update.
I think in that case watchers should be in a sane state - we've fired the events in order, but here we're going to update to the 1st value which is going to lead to an inconsistent state.
I think we probably need to capture (and inc ref) the value and then validate that it's the same. I think it's similar to https://github.com/python/cpython/pull/161586/changes - @nascheme may have this paged into his head enough where he has a good idea if this is the same pattern.
There was a problem hiding this comment.
Yeah, I think this design has problems. It can't promise the ordering if the STW request is contended (stw->mutex, or a global STW like GC). The CS can be suspended and other threads run holding that CS before you enter STW. Also, if the callback suspends (by allocating and triggering GC, taking another critical section, or running bytecode), another setter can get in.
Two possible ideas: document that MODIFY events can arrive in different order from the stores, when threads race. I'm guessing you don't want that, seems the point of this PR to avoid it.
Another idea, record something (per-func modification count) under the CS before firing the event, in STW check and if changed the restart and retry. I think you could still get "strange" events, e.g. if T1 sets A and T2 sets B, watcher could see A B A. That approach seems similar to the counter based approach I was trying to use to fix the typeobject STW deadlock issue. Tricky to get right.
This ensures that function events arrive in the same order as their corresponding stores, and a watcher can read the function's fields without racing other setters (provided the watcher callback doesn't block and suspend the critical section).
An alternative would be to call the callback with the world stopped, but that's dangerous. The callback could inadvertently re-enter Python code and deadlock trying to stop the world again (e.g. triggering GC).
This is a behavior difference for users of function watchers on free-threaded builds of CPython. It has no effect on users of the default build.