-
-
Notifications
You must be signed in to change notification settings - Fork 31.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fatal Python error: _enter_buffered_busy: could not acquire lock for <_io.BufferedWriter name='<stderr>'> at interpreter shutdown, possibly due to daemon threads #129536
Comments
There's not much we can do, the error tells you exactly what's wrong: daemon threads cause a deadlock if they hold the |
We could perhaps make SSL reporting error more friendly for those cases? or is this something that can't be done on our side? or is this something we don't do for other modules? Or is this something that has nothing to do with SSL in general and that's the only reproducer we have? I mean, it's pretty common to use daemon threads as dirty hacks instead of two separate applications (in this case, I would recommend two different scripts, one for the server, one for the client, or a script that runs both scripts using separate processes) |
As far as I can tell, it's unrelated to SSL. A daemon thread, which will probably be hung at this point of finalization, holds the lock to stderr. Python can't do anything at that point, so it just bails out with a fatal error. |
AFAICT, the server is trying to perform handshake but because of an exception, the program exits and the daemon thread then tries to finalize something (maybe it tries to report the error on stderr, but since it's daemon and the program is about to exit, it cannot do it properly). So maybe we should check that we don't report SSL failures if we are finalizing as otherwise we'll need to acquire a lock on stderr (remember that the SSL path for creating exceptions is slow and that may also be the reason why the thread dies before we can create and report that exception). Now, I don't think we need to dig more as a simple workaround would be to use two different processes (one for the client and one for the server), which is what should be done in this situation IMO. |
I think some of these cases can be made to not need a lock (esp. on shutdown), stderr is line buffered and usually w/ print or logging, lines are being written at a time, at which point kernel ordering the |
That seems like a reasonable temporary fix. IMO, avoiding locks isn't a great permanent solution. Really, we need a better way to shut down daemon threads, rather than just hanging the re-acquisition of thread states. The issue here isn't specific to IO, but to any lock. For example: Py_BEGIN_ALLOW_THREADS;
// tstate is detached
PyMutex_Lock(&whatever_lock); // or PyThread_acquire_lock
Py_END_ALLOW_THREADS; // Daemon thread gets hung with the lock held! If the main thread tries to acquire that lock, deadlock ensues. Something akin to how stop-the-world works on free-threading would be better. Probably something like routine |
Crash report
What happened?
Python crashes at interpreter shutdown when running this script which starts a misconfigured SSL server:
Here's some of the apple crash report:
I also see the same crash on Python 3.9. Is this expected behavior?
CPython versions tested on:
3.13
Operating systems tested on:
macOS
Output from running 'python -VV' on the command line:
Python 3.13.0 (v3.13.0:60403a5409f, Oct 7 2024, 00:37:40) [Clang 15.0.0 (clang-1500.3.9.4)]
The text was updated successfully, but these errors were encountered: