diff --git a/Modules/_io/bufferedio.c b/Modules/_io/bufferedio.c --- a/Modules/_io/bufferedio.c +++ b/Modules/_io/bufferedio.c @@ -306,7 +306,14 @@ static int return 0; } Py_BEGIN_ALLOW_THREADS - PyThread_acquire_lock(self->lock, 1); + if (!_Py_Finalizing) + PyThread_acquire_lock(self->lock, 1); + else + /* When finalizing, we don't want a deadlock to happen with daemon + * threads abruptly shut down while they owned the lock. Therefore, + * only wait for a grace period (1 s.). + */ + PyThread_acquire_lock_timed(self->lock, 1e6, 0); Py_END_ALLOW_THREADS return 1; }