bpo-33097: Fix submit accepting callable after executor shutdown by interpreter exit by mrknmc · Pull Request #6144 · python/cpython (original) (raw)

Thanks for spotting this! Here are a few comments on your patch.

This behavior is caused by the fact that when the interpreter is shutting down, the executor is not flagged as shutdown. For ProcessPoolExecutor, in L428, we do not set the _shutdown_thread flag as we consider that no job can be submitted after we detect that the executor is shutting down. We can thus set _shutdown_thread=True when the executor exists at this point to have a proper state in the executor.

The same inconsistency in the flag exists for ThreadPoolExecutor and fixing both would be a nice addition to this PR.

On the other hand, if the executor has not been started before the interpreter exit, a submit in atexit would launch processes and the cleaning process tends to be messy. So adding a check on global interpreter shutdown at submit avoids this messy state.