Skip to content

Commit 65de80b

Browse files
Handle InvalidStateError during termination when using run_in_terminal/patch_stdout.
In some edge cases, during cancellation, probably when using anyio, we can get this `InvalidStateError` during termination. This patch fixes that bug. ``` Traceback (most recent call last): File "/home/jonathan/git/python-prompt-toolkit/src/prompt_toolkit/application/run_in_terminal.py", line 49, in run async with in_terminal(render_cli_done=render_cli_done): File "/home/jonathan/.local/share/uv/python/cpython-3.11.10-linux-aarch64-gnu/lib/python3.11/contextlib.py", line 217, in __aexit__ await anext(self.gen) File "/home/jonathan/git/python-prompt-toolkit/src/prompt_toolkit/application/run_in_terminal.py", line 114, in in_terminal new_run_in_terminal_f.set_result(None) asyncio.exceptions.InvalidStateError: invalid state ```
1 parent 435bd99 commit 65de80b

File tree

1 file changed

+4
-1
lines changed

1 file changed

+4
-1
lines changed

src/prompt_toolkit/application/run_in_terminal.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,4 +111,7 @@ async def f():
111111
app._request_absolute_cursor_position()
112112
app._redraw()
113113
finally:
114-
new_run_in_terminal_f.set_result(None)
114+
# (Check for `.done()`, because it can be that this future was
115+
# cancelled.)
116+
if not new_run_in_terminal_f.done():
117+
new_run_in_terminal_f.set_result(None)

0 commit comments

Comments
 (0)