Skip to content

Commit

Permalink
Make tp_watch thread safe
Browse files Browse the repository at this point in the history
  assign_version_tag in PyType_Watch is protected by the lock, but
  tp_watch is also assigned in PyType_Unwatch.  This makes both use
  an atomic operation so watch/unwatch requests won't get lost.
  • Loading branch information
DinoV committed Jan 11, 2024
1 parent dc1bd8f commit 756246f
Showing 1 changed file with 2 additions and 2 deletions.
4 changes: 2 additions & 2 deletions Objects/typeobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -772,8 +772,8 @@ PyType_Watch(int watcher_id, PyObject* obj)
// ensure we will get a callback on the next modification
Py_BEGIN_CRITICAL_SECTION(type);
assign_version_tag(interp, type);
_Py_atomic_or_uint8(&type->tp_watched, 1 << watcher_id);
Py_END_CRITICAL_SECTION();
type->tp_watched |= (1 << watcher_id);
return 0;
}

Expand All @@ -789,7 +789,7 @@ PyType_Unwatch(int watcher_id, PyObject* obj)
if (validate_watcher_id(interp, watcher_id)) {
return -1;
}
type->tp_watched &= ~(1 << watcher_id);
_Py_atomic_and_uint8(&type->tp_watched, ~(1 << watcher_id));
return 0;
}

Expand Down

0 comments on commit 756246f

Please sign in to comment.