GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — gh-109047: Add PythonFinalizationError exception by vstinner · Pull Request #109809 · python/cpython

Conversation

@vstinner

@vstinner vstinner commented Sep 24, 2023 •
edited by github-actions Bot
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Member

Add PythonFinalizationError. This exception derived from RuntimeError is raised when an operation is blocked during the Python finalization.

The following functions now raise PythonFinalizationError, instead of RuntimeError:

  • _thread.start_new_thread()
  • os.fork()
  • os.fork1()
  • os.forkpty()

Morever, _winapi.Overlapped finalizer now logs an unraisable PythonFinalizationError, instead of an unraisable RuntimeError.

  • Issue: test_concurrent_futures.test_shutdown: test_interpreter_shutdown() fails randomly (race condition) #109047

📚 Documentation preview 📚: https://cpython-previews--109809.org.readthedocs.build/

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@bedevere-app bedevere-app Bot mentioned this pull request Sep 24, 2023
test_concurrent_futures.test_shutdown: test_interpreter_shutdown() fails randomly (race condition) #109047
Closed
@vstinner

vstinner commented Sep 24, 2023

Copy link
Copy Markdown
Member Author

Ubuntu failed (logs reformatted):

FAIL: test_interpreter_shutdown (test.test_concurrent_futures.test_shutdown.ProcessPoolSpawnProcessPoolShutdownTest.test_interpreter_shutdown)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/runner/work/cpython/cpython-ro-srcdir/Lib/test/test_concurrent_futures/test_shutdown.py", line 49, in test_interpreter_shutdown
    self.assertFalse(err)
AssertionError:

b'Exception in thread Thread-1:\n
Traceback (most recent call last):\n
  File "/home/runner/work/cpython/cpython-ro-srcdir/Lib/threading.py", line 1059, in _bootstrap_inner\n
    self.run()\n
  File "/home/runner/work/cpython/cpython-ro-srcdir/Lib/concurrent/futures/process.py", line 344, in run\n
    self.add_call_item_to_queue()\n
  File "/home/runner/work/cpython/cpython-ro-srcdir/Lib/concurrent/futures/process.py", line 399, in add_call_item_to_queue\n
    self.call_queue.put(_CallItem(work_id,\n
  File "/home/runner/work/cpython/cpython-ro-srcdir/Lib/multiprocessing/queues.py", line 94, in put\n
    self._start_thread()\n
  File "/home/runner/work/cpython/cpython-ro-srcdir/Lib/multiprocessing/queues.py", line 177, in _start_thread\n
    self._thread.start()\n
  File "/home/runner/work/cpython/cpython-ro-srcdir/Lib/threading.py", line 978, in start\n
    _start_new_thread(self._bootstrap, ())\n
PythonFinalizationError: can\'t create new thread at interpreter shutdown\n
Traceback (most recent call last):\n
  File "<string>", line 1, in <module>\n
  File "/home/runner/work/cpython/cpython-ro-srcdir/Lib/multiprocessing/spawn.py", line 122, in spawn_main\n
    exitcode = _main(fd, parent_sentinel)\n
               ^^^^^^^^^^^^^^^^^^^^^^^^^^\n
  File "/home/runner/work/cpython/cpython-ro-srcdir/Lib/multiprocessing/spawn.py", line 132, in _main\n
    self = reduction.pickle.load(from_parent)\n
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n
  File "/home/runner/work/cpython/cpython-ro-srcdir/Lib/multiprocessing/synchronize.py", line 115, in __setstate__\n
    self._semlock = _multiprocessing.SemLock._rebuild(*state)\n
                    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n
FileNotFoundError: [Errno 2] No such file or directory\n'

is not false
All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@vstinner
pythongh-109047: Add PythonFinalizationError exception
6d969f3
Add PythonFinalizationError. This exception derived from RuntimeError
is raised when an operation is blocked during the Python
finalization.

The following functions now raise PythonFinalizationError, instead of
RuntimeError:

* _thread.start_new_thread()
* os.fork()
* os.fork1()
* os.forkpty()

Morever, _winapi.Overlapped finalizer now logs an unraisable
PythonFinalizationError, instead of an unraisable RuntimeError.
@vstinner
vstinner force-pushed the python_finalization_error branch from 261082a to 6d969f3 Compare September 25, 2023 00:00
This was referenced Sep 25, 2023
gh-109047: concurrent.futures catches RuntimeError #109810
Merged
_Py_IsFinalizing has been removed, meaning C/C++ threads cannot avoid termination during shutdown #108014
Closed
@AA-Turner

AA-Turner commented Sep 25, 2023

Copy link
Copy Markdown
Member

Perhaps drop the 'Python' prefix (or perhaps change to IntepreterFinalizationError etc) -- as a reader, I'd be confused as to what Python is trying to signify in the name.

A

AlexWaygood and erlend-aasland reacted with thumbs up emoji
All reactions
  • 👍 2 reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@vstinner

vstinner commented Sep 25, 2023

Copy link
Copy Markdown
Member Author

IntepreterFinalizationError

Python can have multiple interpreters. This exception is when the main interpreter is being finalized. I prefer to document it as "Python finalization".

If it's called FinalizationError, you may ask: which finalization? All Python objects have finalizers.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@AA-Turner

AA-Turner commented Sep 25, 2023 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Member

PrimaryIntepreterFinalizationError? It's perhaps getting a bit silly, though. I still think that 'PythonFinalizationError' is unclear--for example as you noted I would naively think that it would apply to e.g. subinterpreter finalisation, as that's still part of Python.

Whilst FinalizationError is generic (as you say), that might not be a bad thing, in case custom object finalisers need to raise errors of a similar nature as here?

A

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@vstinner

vstinner commented Sep 25, 2023

Copy link
Copy Markdown
Member Author

I prefer the term "Python finalization error".

PrimaryIntepreterFinalizationError?

What is being finalized is not a sub-interpreter or the main interpeter, but in fact, it's the world "Python world": anything related to Python. It's wider than the main interpreter. There are many "global variables" cross-interprereters. Like path configuration (_PyPathConfig_ClearGlobal()), runtime (_PyRuntime_Finalize()), etc.

For example, before, Py_Initialize(), it's not possible to create a Python object. Py_Finalize() destroys ("finalizes") all Python objects. When Py_Finalize() completes, it's no longer possible to create new Python objects.

That's why https://docs.python.org/dev/c-api/init.html and https://docs.python.org/dev/c-api/init_config.html#init-config are about "Python Initialization". Sadly, https://docs.python.org/dev/c-api/init.html#c.Py_FinalizeEx is not well documented.

https://docs.python.org/dev/library/sys.html#sys.is_finalizing mentions "the main Python interpreter".

I'm not happy about https://docs.python.org/dev/glossary.html#term-interpreter-shutdown glossary entry. It isn't clear if we are talking about the main interpreter, a sub-interpreter, or "Python".

Moreover, there is a competition between "shutting down" and "finalization" terms in the documentation. It should be unified.

"Finalization" term is part of the public API: sys.is_finalizing() and Py_Finalize().

"Shutdown" is used in one private API: threading._shutdown().

My notes of Python finalization: https://pythondev.readthedocs.io/finalization.html

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@vstinner

vstinner commented Sep 29, 2023

Copy link
Copy Markdown
Member Author

My fix PR gh-109810 does not strictly need a new exception. So close this PR for now, until someone comes with a more concrete use case which requires adding a new exception.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@vstinner vstinner closed this Sep 29, 2023
@vstinner
vstinner deleted the python_finalization_error branch September 29, 2023 19:48
@vstinner vstinner mentioned this pull request Oct 7, 2023
The Word "Finalizing" in C-API Function Names is Sometimes Misleading #110490
Open
@vstinner vstinner mentioned this pull request Jan 30, 2024
Checking for sys.is_finalizing before thread.start() still yields "can't create new thread at interpreter shutdown" #114570
Closed
@vstinner

vstinner commented Feb 12, 2024

Copy link
Copy Markdown
Member Author

I created a new PR for this exception: #115352

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@gpshead gpshead Awaiting requested review from gpshead gpshead will be requested when the pull request is marked ready for review gpshead is a code owner
@iritkatriel iritkatriel Awaiting requested review from iritkatriel iritkatriel will be requested when the pull request is marked ready for review iritkatriel is a code owner
@ericsnowcurrently ericsnowcurrently Awaiting requested review from ericsnowcurrently ericsnowcurrently will be requested when the pull request is marked ready for review ericsnowcurrently is a code owner
@AA-Turner AA-Turner Awaiting requested review from AA-Turner AA-Turner will be requested when the pull request is marked ready for review AA-Turner is a code owner

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

None yet

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

Uh oh!

There was an error while loading. Please reload this page.

2 participants

@vstinner @AA-Turner
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.