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 — The asyncio REPL interactive daemon thread doesn't handle `PYTHONSTARTUP` exceptions · Issue #140287 · python/cpython

The asyncio REPL interactive daemon thread doesn't handle PYTHONSTARTUP exceptions #140287

New issue
Copy link
New issue
Copy link
Closed
Closed
The asyncio REPL interactive daemon thread doesn't handle PYTHONSTARTUP exceptions#140287
Copy link
Labels
3.13only security fixesonly security fixes3.14bugs and security fixesbugs and security fixes3.15bugs and security fixesbugs and security fixesstdlibStandard Library Python modules in the Lib/ directoryStandard Library Python modules in the Lib/ directorytopic-asynciotopic-replRelated to the interactive shellRelated to the interactive shelltype-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error

Description

@johnslavik
johnslavik
opened on Oct 18, 2025
Issue body actions

Bug report

Bug description:

That leads to flaky behaviors.

This is my PYTHONSTARTUP file:

raise ModuleNotFoundError("oops!")

ModuleNotFoundError is just an example exception that can occur in PYTHONSTARTUP scripts.

This is how the regular REPLs handle it (both the basic REPL and the new REPL):

❯ PYTHONSTARTUP=t.py ./python
Python 3.15.0a1+ (heads/main:fbf0843e39e, Oct 18 2025, 11:24:43) [GCC 15.2.1 20250808 (Red Hat 15.2.1-1)] on linux
Type "help", "copyright", "credits" or "license" for more information.
Traceback (most recent call last):
  File "t.py", line 1, in <module>
    raise ModuleNotFoundError("oops!")
ModuleNotFoundError: oops!
>>> 

The traceback is shown and we proceed to the loop.

For the asyncio REPL, startup file exception always causes it to exit immediately -- we'd never want the REPL to exit because of a startup file error. On top of that, it doesn't report the exception properly, because it's racing with the interpreter shutdown at that point.

I've found 3 variants of the incorrect behavior:

  1. The interactive daemon thread doesn't write the full traceback:

    ❯ PYTHONSTARTUP=t.py ./python -m asyncio
    asyncio REPL 3.15.0a1+ (heads/main:fbf0843e39e, Oct 18 2025, 11:24:43) [GCC 15.2.1 20250808 (Red Hat 15.2.1-1)] on linux
    Use "await" directly instead of "asyncio.run()".
    Type "help", "copyright", "credits" or "license" for more information.
    Exception in thread Interactive thread:
    exiting asyncio REPL...
    Traceback (most recent call last):
    
  2. The interactive daemon thread does flush, but after exiting is reported:

    ❯ PYTHONSTARTUP=t.py ./python -m asyncio
    asyncio REPL 3.15.0a1+ (heads/main:fbf0843e39e, Oct 18 2025, 11:24:43) [GCC 15.2.1 20250808 (Red Hat 15.2.1-1)] on linux
    Use "await" directly instead of "asyncio.run()".
    Type "help", "copyright", "credits" or "license" for more information.
    Exception in thread Interactive thread:
    exiting asyncio REPL...
    Traceback (most recent call last):
      File "/home/bswck/Python/cpython/Lib/threading.py", line 1075, in _bootstrap_inner
        self._context.run(self.run)
        ~~~~~~~~~~~~~~~~~^^^^^^^^^^
      File "/home/bswck/Python/cpython/Lib/asyncio/__main__.py", line 104, in run
        exec(startup_code, console.locals)
        ~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
      File "t.py", line 1, in <module>
        raise ModuleNotFoundError("oops!")
    ModuleNotFoundError: oops!
    
  3. The interactive daemon tries to acquire stderr at interpreter shutdown, and fails:

    ❯ PYTHONSTARTUP=t.py ./python -m asyncio
    asyncio REPL 3.15.0a1+ (heads/main:fbf0843e39e, Oct 18 2025, 11:24:43) [GCC 15.2.1 20250808 (Red Hat 15.2.1-1)] on linux
    Use "await" directly instead of "asyncio.run()".
    Type "help", "copyright", "credits" or "license" for more information.
    Exception in thread Interactive thread:
    exiting asyncio REPL...
    Traceback (most recent call last):
      File "/home/bswck/Python/cpython/Lib/threading.py", line 1075, in _bootstrap_inner
        self._context.run(self.run)
        ~~~~~~~~~~~~~~~~~^^^^^^^^^^
    Fatal Python error: _enter_buffered_busy: could not acquire lock for <_io.BufferedWriter name='<stderr>'> at interpreter shutdown, possibly due to daemon threads
    Python runtime state: finalizing (tstate=0x00000000009c3818)
    
    Current thread 0x00007f6c4781cbc0 [python] (most recent call first):
      <no Python frame>
    Aborted (core dumped)
    

CPython versions tested on:

CPython main branch

Operating systems tested on:

Linux

Linked PRs

  • gh-140287: Handle PYTHONSTARTUP script exceptions in the asyncio REPL #140288
  • [3.14] gh-140287: Handle PYTHONSTARTUP script exceptions in the asyncio REPL (GH-140288) #148987
  • [3.13] gh-140287: Handle PYTHONSTARTUP script exceptions in the asyncio REPL (GH-140288) #148988
Reacted by msmojtabafar

Activity

  1. johnslavik
    added
    type-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error
    on Oct 18, 2025
  2. johnslavik commented on Oct 18, 2025

    @johnslavik
    johnslavik
    on Oct 18, 2025
    MemberAuthor
    More actions

    CC @ZeroIntensity (mentorship) I'd add these labels: stdlib, type-repl, type-asyncio. Perhaps also version labels once I reproduce for all versions.

    Reacted by Peter Bierma
  3. johnslavik commented on Oct 18, 2025

    @johnslavik
    johnslavik
    on Oct 18, 2025
    MemberAuthor
    More actions

    The simplest fix is to just handle these exceptions with sys.excepthook (via console.showtraceback).

  4. johnslavik commented on Oct 18, 2025

    @johnslavik
    johnslavik
    on Oct 18, 2025
    MemberAuthor
    More actions

    Interestingly, _pyrepl doesn't seem to properly handle this as well when pythonstartup=True is passed to _pyrepl.main.interactive_console. PYTHONSTARTUP=t.py ./python -c 'import _pyrepl.main; _pyrepl.main.interactive_console(pythonstartup=True)' exits immediately. However, it doesn't suffer from problems that daemonic threads do. I need to check that later.

    Reacted by msmojtabafar
  5. ZeroIntensity
    added
    topic-asyncio
    stdlibStandard Library Python modules in the Lib/ directoryStandard Library Python modules in the Lib/ directory
    topic-replRelated to the interactive shellRelated to the interactive shell
    on Oct 18, 2025
  6. github-project-automation
    added this to asyncioon Oct 18, 2025
  7. github-project-automation
    moved this to Todo in asyncioon Oct 18, 2025
  8. johnslavik commented on Nov 8, 2025

    @johnslavik
    johnslavik
    on Nov 8, 2025
    MemberAuthor
    More actions

    This is a bugfix -- let's add 3.13, 3.14 and 3.15 here cc @ZeroIntensity mentorship

    Reacted by Peter Bierma
  9. ZeroIntensity
    added
    3.13only security fixesonly security fixes
    3.14bugs and security fixesbugs and security fixes
    3.15bugs and security fixesbugs and security fixes
    on Nov 8, 2025
  10. pablogsal
    added a commit that references this issue on Apr 25, 2026

    gh-140287: Handle `PYTHONSTARTUP` script exceptions in the asyncio RE…

    5ea3ae7
  11. ljfp
    added a commit that references this issue on Apr 25, 2026

    pythongh-140287: Handle `PYTHONSTARTUP` script exceptions in the asyn…

    955bef7
  12. hugovk
    added 2 commits that reference this issue on Apr 29, 2026

    [3.14] gh-140287: Handle `PYTHONSTARTUP` script exceptions in the asy…

    adfde79

    [3.13] gh-140287: Handle `PYTHONSTARTUP` script exceptions in the asy…

    0c83e50
  13. hugovk
    closed this as completedon Apr 29, 2026
  14. github-project-automation
    moved this from Todo to Done in asyncioon Apr 29, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    3.13only security fixesonly security fixes3.14bugs and security fixesbugs and security fixes3.15bugs and security fixesbugs and security fixesstdlibStandard Library Python modules in the Lib/ directoryStandard Library Python modules in the Lib/ directorytopic-asynciotopic-replRelated to the interactive shellRelated to the interactive shelltype-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error

    Projects

    • asyncio
      Status
      Done
      Show more project fields

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

    You are visitor 01402520 — 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.