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

Exception when printing from thread in interactive mode

Open
#1,040 1 comment 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

I get this exception when I have asynchronous logging on the terminal while in interactive mode as soon as I hit enter with a command. The following code can reproduce the problem:

#!/usr/bin/env python3
import threading
import time

msg = b'This is the message.  It will be repeated.'

def UDP_receive():
    while True:
        print(msg)
        time.sleep(0.01)

listen_UDP = threading.Thread(target=UDP_receive)
listen_UDP.start()

which gives

...
b'This is the message.  It will be repeated.'
b'This is the message.  It will be repeated.'
In [1]: sdcs # just type something and hit enter
Exception in default exception handler
Traceback (most recent call last):
  File "/usr/local/Cellar/ipython/7.8.0/libexec/vendor/lib/python3.7/site-packages/prompt_toolkit/eventloop/posix.py", line 154, in _run_task
    t()
  File "/usr/local/Cellar/ipython/7.8.0/libexec/vendor/lib/python3.7/site-packages/prompt_toolkit/eventloop/context.py", line 115, in new_func
    return func(*a, **kw)
  File "/usr/local/Cellar/ipython/7.8.0/libexec/vendor/lib/python3.7/site-packages/prompt_toolkit/patch_stdout.py", line 102, in write_and_flush_in_loop
    run_in_terminal(write_and_flush, in_executor=False)
  File "/usr/local/Cellar/ipython/7.8.0/libexec/vendor/lib/python3.7/site-packages/prompt_toolkit/application/run_in_terminal.py", line 41, in run_in_terminal
    return run_coroutine_in_terminal(async_func, render_cli_done=render_cli_done)
  File "/usr/local/Cellar/ipython/7.8.0/libexec/vendor/lib/python3.7/site-packages/prompt_toolkit/application/run_in_terminal.py", line 62, in run_coroutine_in_terminal
    assert app._is_running
AssertionError

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/usr/local/Cellar/ipython/7.8.0/libexec/vendor/lib/python3.7/site-packages/prompt_toolkit/eventloop/base.py", line 129, in call_exception_handler
    self._exception_handler(context)
  File "/usr/local/Cellar/ipython/7.8.0/libexec/vendor/lib/python3.7/site-packages/prompt_toolkit/application/application.py", line 702, in handle_exception
    run_coroutine_in_terminal(print_exception)
  File "/usr/local/Cellar/ipython/7.8.0/libexec/vendor/lib/python3.7/site-packages/prompt_toolkit/application/run_in_terminal.py", line 62, in run_coroutine_in_terminal
    assert app._is_running
AssertionError
b'This is the message.  It will be repeated.'
b'This is the message.  It will be repeated.'
...

test.py.txt

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

Run the attached test.py.txt reproduction in interactive mode, then trace the failing path through prompt_toolkit/patch_stdout.py, application/run_in_terminal.py, and application/application.py. Compare the event-loop behavior around the AssertionError and confirm that asynchronous terminal output no longer raises an exception after entering a command.

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
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.