prompt-toolkit / prompt-toolkit/python-prompt-toolkit

Getting Unhandled exception in event loop: Press Enter to continue

Open
#1,139 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Python
Stars
10.6k
Forks
815
PR merge metrics
No merged PRs in 30d

Description

This appears to be due to cpr events getting lost along the way. This little bit of code reproduces the issue under linux (python 3.7.3, prompt_toolkit 3.0.5):

import os
from asyncio import sleep, get_running_loop, run
from prompt_toolkit.patch_stdout import patch_stdout
from prompt_toolkit.shortcuts import PromptSession

from prompt_toolkit import __version__
print("Version:", __version__)


def _exception_handler(loop, context):
    # Should never get here - exit quickly with error
    os._exit(4)


async def t1():
    'Print stuff to screen quickly so prompt_toolkit needs cpr'
    counter = 0 
    while True:
        print("Counter:", counter)
        counter += 1 
        await sleep(.01)


async def main():
    loop = get_running_loop()
    loop.set_exception_handler(_exception_handler)
    loop.create_task(t1())
    session = PromptSession('% ')
    with patch_stdout():
        await session.prompt_async(set_exception_handler=False)

run(main())

This appears to be due to a cpr event getting lost along the way and then the wait_for_responses task eventually gets reclaimed and we get the error. I'll submit a pr for discussion.

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by running the provided reproducer under Linux with Python 3.7.3, prompt_toolkit 3.0.5, and the asyncio exception handler. Trace prompt_async, CPR event handling, and the wait_for_responses task to determine how a response is lost. Done means the reproducer no longer reaches the unhandled-exception handler while output is being printed.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
cli
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.