Skip to content

fix(approval): route fallback input() through prompt_toolkit run_in_terminal to fix deadlock - #16477

Closed
Feranmi10 wants to merge 0 commit into
NousResearch:mainfrom
Feranmi10:main
Closed

fix(approval): route fallback input() through prompt_toolkit run_in_terminal to fix deadlock#16477
Feranmi10 wants to merge 0 commit into
NousResearch:mainfrom
Feranmi10:main

Conversation

@Feranmi10

Copy link
Copy Markdown
Contributor

What does this PR do?

Fixes the deadlock in tools/approval.py where get_input was called from a background thread while prompt_toolkit's Application.run() was already running on MainThread. Both competed for stdin, causing the approval prompt to freeze with no way to type o/s/d.

Root cause: In subagent/delegate threads, _callback_tls.approval is None because TLS doesn't propagate to child threads. This caused prompt_dangerous_approval to fall through to its raw input() fallback — which deadlocked with prompt_toolkit's own stdin reader.

Fix — two changes in tools/approval.py:

  1. New _try_pt_run_in_terminal(func, timeout) helper that detects a live prompt_toolkit Application via _current_app ContextVar, schedules an async _schedule() coroutine via asyncio.run_coroutine_threadsafe, then inside _schedule, calls app.run_in_terminal(func) — the non-executor path that detaches prompt_toolkit's stdin reader and restores cooked mode before calling func, eliminating the stdin race.
  2. Refactored prompt_dangerous_approval fallback to call _try_pt_run_in_terminal first; falls back to the original daemon-thread + input() approach only when None is returned (no live Application — non-TUI paths, tests, cron).

Related Issue

Fixes #15216

Type of Change

  • Bug fix

@alt-glitch alt-glitch added type/bug Something isn't working P1 High — major feature broken, no workaround comp/agent Core agent runtime: loop, agent_init, prompt builder, context-compression, responses endpoint comp/cli CLI entry point, hermes_cli/, setup wizard comp/tui Terminal UI (ui-tui/ + tui_gateway/) labels Apr 27, 2026
@Feranmi10 Feranmi10 closed this Apr 29, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

comp/agent Core agent runtime: loop, agent_init, prompt builder, context-compression, responses endpoint comp/cli CLI entry point, hermes_cli/, setup wizard comp/tui Terminal UI (ui-tui/ + tui_gateway/) P1 High — major feature broken, no workaround type/bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: approval prompt get_input deadlocks against prompt_toolkit MainThread (regression of #13617)

2 participants