Skip to content

Commit 39618b0

Browse files
committed
Add sanity assertions in some import lock code (issue #15599).
1 parent cfe6cf1 commit 39618b0

2 files changed

Lines changed: 3 additions & 0 deletions

File tree

Lib/test/test_threaded_import.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ def find_module(self, name, path=None):
6868
# Simulate some thread-unsafe behaviour. If calls to find_module()
6969
# are properly serialized, `x` will end up the same as `numcalls`.
7070
# Otherwise not.
71+
assert imp.lock_held()
7172
with self.lock:
7273
self.numcalls += 1
7374
x = self.x

Python/import.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,7 @@ _PyImport_AcquireLock(void)
169169
PyThread_acquire_lock(import_lock, 1);
170170
PyEval_RestoreThread(tstate);
171171
}
172+
assert(import_lock_level == 0);
172173
import_lock_thread = me;
173174
import_lock_level = 1;
174175
}
@@ -182,6 +183,7 @@ _PyImport_ReleaseLock(void)
182183
if (import_lock_thread != me)
183184
return -1;
184185
import_lock_level--;
186+
assert(import_lock_level >= 0);
185187
if (import_lock_level == 0) {
186188
import_lock_thread = -1;
187189
PyThread_release_lock(import_lock);

0 commit comments

Comments
 (0)