Skip to content

Studio: stop tool calls from getting stuck in Running forever - #12234

Merged
Lyxot merged 2 commits into
unslothai:mainfrom
NilayYadav:tool-calls-completely-halt-randomly
Sep 29, 2026
Merged

Lyxot merged 2 commits into
unslothai:mainfrom
NilayYadav:tool-calls-completely-halt-randomly

Conversation

@NilayYadav

Copy link
Copy Markdown
Collaborator

Since the chat database started reusing connections, a terminal or Python tool call now and then never starts. Its card stays on Running far past Max Tool Call Duration and Stop cannot clear it. It is more likely when several chats are busy at once.

Starting a tool forks Studio, and the copy tried to close database connections owned by other threads. If one of them was mid-query at that moment, the copy waited forever on its lock. Now the copy leaves them alone and the tool starts normally.

With eight busy project chats on main, 29 of about 1,260 terminal calls got stuck this way. On this branch none of 1,306 did. #12087 fixed a different hang (the credential check on commands like echo "A=$A") and closed the issue, but this one still happens with it merged. Two branch calls still stalled on the Hugging Face client's own cleanup in the forked copy, which is a separate issue.

Refs #12048

@NilayYadav

Copy link
Copy Markdown
Collaborator Author

Codex review has converged on this PR.

@NilayYadav NilayYadav left a comment

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review + reproduction evidence

Pinned review: 8d7fbc6ef4bbe746b77599fae6dd4877d061db84. No blocking findings, nothing pushed.

I reproduced the stuck tool call on fresh upstream main on Modal (L4, install.sh --local, logged in through the password form, Qwen3-4B-Instruct-2507-GGUF Q4_K_M picked in the model picker, project chat with Code on and Run automatically, 8 tabs each asking for 15 terminal commands one call at a time, Chromium 1400x1000). Same flow, same inputs on main and on main + this PR:

  • main (fix reverted run): 27 of 959 terminal calls never returned (log has execute_tool: name=terminal with no tool result). The composer showed Running: echo done 401s past the 300s Max Tool Call Duration. py-spy on the Studio children that never exec'd: all 11 left at the end sit in _close_quietly (chat_generation_runs_db.py:126) <- weakref.finalize <- subprocess._execute_child <- spawn_prepared_launch <- _bash_exec. In CI the new test fails with "the forked child hung on a SQLite lock another thread held at the fork" on Linux and macOS.
  • PR head (run): 1,065 of 1,065 terminal calls returned, 0 stuck turns, 0 stuck children. The new test passes on Linux and macOS; on Windows it is skipped and the other 19 pass on both arms.

PR 12234 before/after

  • Adjacent flows identical on main and the PR: plain reply, Python tool (2**100 correct), terminal echo, and Stop on a running sleep 120 (card goes to Cancelled in under 1s).
  • Performance, 7 runs each: warm connect+close 0.82us vs 0.81us, first connect 1072us vs 1010us. Tool launch with 32 pooled threads gets faster: Popen(preexec_fn) 5.2ms vs 2.6ms, because the child no longer closes 32 connections before exec. Nothing on the streaming path changes.
  • Differential tests: the new test fails on main (1 failed, 19 passed) and passes on the PR (20 passed).

One wording nit on the description: in my runs Stop did end the turn in the UI. What it cannot clear is the stuck launch left in the background. Maybe say "Stop ends the turn but the stuck launch stays behind".

Conclusion: real regression from #11525, fixed at the right place, no regressions found. Ready once upstream CI finishes green and someone other than the author has looked at it.

huggingface_hub closes its shared httpx client in every forked child through an at-fork hook. A tool or sidecar spawned with preexec_fn runs that hook before exec, and it blocks forever on an httpx lock another thread held at the fork, leaving the tool call stuck in Running. A before-fork hook now makes the live client's close a no-op outside the process that owns it, so the child keeps it referenced and execs.
@Lyxot

Lyxot commented Sep 29, 2026

Copy link
Copy Markdown
Collaborator

Pushed a73163a on top of this branch. It closes the other instance of the same hang, the one the description mentions as the Hugging Face client's cleanup in the forked copy.

huggingface_hub registers os.register_at_fork(after_in_child=close_session). A Popen(preexec_fn=...) child runs that hook before exec, and closing the shared httpx client waits on an httpcore lock. If another thread held that lock at the fork, the child never execs and the tool stays on Running. A before-fork hook in utils/hub_settings.py now makes the live shared client's close() a no-op outside the process that owns it. The child keeps the client referenced, and the owner still closes it normally. The hook runs in the parent because child hooks run in import order, so ours cannot be guaranteed to run before huggingface_hub's.

A deterministic test holds the pool lock in another thread while a preexec_fn child is forked. It hangs without the fix on macOS and Linux and passes with it. The sqlite change here is unchanged.

@Lyxot

Lyxot commented Sep 29, 2026

Copy link
Copy Markdown
Collaborator

Codex review has converged on this PR.

@Lyxot Lyxot left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. Verified both hangs with deterministic repros on macOS and Linux; Codex converged and the remaining CI failures are main-side and already fixed on main.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants