diff --git a/Lib/test/test_threaded_import.py b/Lib/test/test_threaded_import.py index 0528b139f7b..93bfb2a7581 100644 --- a/Lib/test/test_threaded_import.py +++ b/Lib/test/test_threaded_import.py @@ -68,6 +68,7 @@ class Finder: # Simulate some thread-unsafe behaviour. If calls to find_module() # are properly serialized, `x` will end up the same as `numcalls`. # Otherwise not. + assert imp.lock_held() with self.lock: self.numcalls += 1 x = self.x diff --git a/Python/import.c b/Python/import.c index 2f71b97bc8b..5fc2523b268 100644 --- a/Python/import.c +++ b/Python/import.c @@ -169,6 +169,7 @@ _PyImport_AcquireLock(void) PyThread_acquire_lock(import_lock, 1); PyEval_RestoreThread(tstate); } + assert(import_lock_level == 0); import_lock_thread = me; import_lock_level = 1; } @@ -182,6 +183,7 @@ _PyImport_ReleaseLock(void) if (import_lock_thread != me) return -1; import_lock_level--; + assert(import_lock_level >= 0); if (import_lock_level == 0) { import_lock_thread = -1; PyThread_release_lock(import_lock);