Skip to content

Commit 756246f

Browse files
committed
Make tp_watch thread safe
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.
1 parent dc1bd8f commit 756246f

File tree

1 file changed

+2
-2
lines changed

1 file changed

+2
-2
lines changed

Objects/typeobject.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -772,8 +772,8 @@ PyType_Watch(int watcher_id, PyObject* obj)
772772
// ensure we will get a callback on the next modification
773773
Py_BEGIN_CRITICAL_SECTION(type);
774774
assign_version_tag(interp, type);
775+
_Py_atomic_or_uint8(&type->tp_watched, 1 << watcher_id);
775776
Py_END_CRITICAL_SECTION();
776-
type->tp_watched |= (1 << watcher_id);
777777
return 0;
778778
}
779779

@@ -789,7 +789,7 @@ PyType_Unwatch(int watcher_id, PyObject* obj)
789789
if (validate_watcher_id(interp, watcher_id)) {
790790
return -1;
791791
}
792-
type->tp_watched &= ~(1 << watcher_id);
792+
_Py_atomic_and_uint8(&type->tp_watched, ~(1 << watcher_id));
793793
return 0;
794794
}
795795

0 commit comments

Comments
 (0)