Skip to content

gh-162529: Run function watchers under the function's critical section - #162530

Open
alexmalyshev wants to merge 1 commit into
python:mainfrom
alexmalyshev:func-watcher-order
Open

alexmalyshev wants to merge 1 commit into
python:mainfrom
alexmalyshev:func-watcher-order

Conversation

@alexmalyshev

@alexmalyshev alexmalyshev commented Nov 24, 2026 •

Copy link
Copy Markdown
Contributor

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.

…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.
Comment thread Objects/funcobject.c
PyObject *old_value;
Py_BEGIN_CRITICAL_SECTION(func);
handle_func_event(event, func, new_value);
_PyEval_StopTheWorld(interp);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants