Skip to content

Make Ctrl-C behave the same way in all five clients #301

Closed

Description

@a-akimov

Problem

The five clients each react to Ctrl-C in a different way. This was the last open task in #269, which asked for cleanup on Ctrl-C in Go, Rust and Ruby and assumed Python and TypeScript already handled it. They don't: Python hangs at the prompt, and TypeScript waits out a pending query.

Behavior on main (d084a4b), checked by starting each client against a stdio server, waiting for the Query: prompt, and sending SIGINT with stdin left open. A second case sent SIGINT while the API request for a query was still pending. Ruby is described from the code; it wasn't run.

Client Ctrl-C at the prompt Ctrl-C during a query Cleanup runs?
Python Prints a KeyboardInterrupt traceback, then hangs until Enter or Ctrl-D, because Python waits at exit for the thread blocked in input() Exits with status 130 and a KeyboardInterrupt traceback Not reliably: on Python 3.11+, the cancellation also cancels cleanup's own awaits
TypeScript Exits cleanly with status 0 Waits for the pending reply, then exits 0; a second Ctrl-C kills it at once Yes
Go Exits at once with status 130 Same No
Rust Exits at once with status 130 Same No
Ruby ensure runs cleanup, then Ruby prints the Interrupt backtrace and exits non-zero Same Yes

Where this comes from in the code:

  • mcp-client-python/client.py:184-185 reads input with asyncio.to_thread(input, ...) and catches KeyboardInterrupt, but under asyncio.run (line 233) Ctrl-C doesn't arrive there, and the worker thread stays blocked in input().
  • mcp-client-typescript/index.ts:230-235 closes readline on SIGINT, which ends the loop at the prompt but doesn't abort a request in flight.
  • mcp-client-go/main.go and mcp-client-rust/src/main.rs install no handler, so the default SIGINT action ends the process before Cleanup() / cleanup() runs.
  • mcp-client-ruby/client.rb:231-249 cleans up in ensure, but rescue => e doesn't catch Interrupt, which isn't a StandardError.

In practice no server is left running in any client: in a terminal Ctrl-C reaches the server too, and a stdio server exits when its stdin closes. So the visible differences are hangs, backtraces and exit status, not leaked processes.

Decision needed

How much Ctrl-C handling belongs in a quickstart. AGENTS.md asks to leave out code that only makes an example more robust, and quit and Ctrl-D already exit cleanly in all five clients.

  • Option A, clean shutdown on Ctrl-C everywhere: every client catches Ctrl-C at the prompt and during a query, closes the session and exits 0. Draft PR Clean up on Ctrl-C in the Go, Rust and Ruby clients #300 implements this and extends the tool-loop test to check it. It works, but it costs about 230 lines across the clients and tests, including asyncio internals in Python (Task.uncancel(), os._exit), a stdin goroutine and signal.NotifyContext in Go, tokio::select! plus process::exit in Rust, and an abort signal passed through TypeScript's query code. Much of that sits in code the tutorials quote.
  • Option B, quit and Ctrl-D are the clean exit, Ctrl-C just stops the client: fix only what's broken, the Python hang and the Ruby backtrace, leave the default behavior elsewhere, and say in each README that quit or Ctrl-D exits cleanly and Ctrl-C stops the client at once. This is a few lines per client, plus a test that Ctrl-C at the prompt ends the client promptly.

Option A

Option B

  • Python: restore the default SIGINT handler before asyncio.run(main()), so Ctrl-C exits at once instead of hanging, and drop KeyboardInterrupt from the except around input().
  • Ruby: rescue Interrupt next to rescue => e, so Ctrl-C exits without a backtrace (ensure still cleans up).
  • TypeScript: decide whether Ctrl-C during a query should stop the client at once (drop the SIGINT handler, or abort the request) or keep waiting for the reply, and document whichever it is.
  • Say in every client README that quit or Ctrl-D exits and shuts the server down cleanly, and what Ctrl-C does.
  • Extend tests/helpers/tool-loop-test.ts to send SIGINT to the client's process group at the prompt, with stdin left open, and fail if the client doesn't exit within a few seconds.

Acceptance criteria

  • All five clients do the same thing on Ctrl-C, at the prompt and during a query, and their READMEs say what that is.
  • No client hangs after Ctrl-C or prints a backtrace.
  • The tool-loop test fails if a client stops behaving that way.

Activity

  1. added
    bugSomething isn't working
    javascriptPull requests that update javascript code
    rustPull requests that update rust code
    python:uvPull requests that update python:uv code
    goPull requests that update go code
    rubyPull requests that update ruby code
    on Nov 15, 2026
  2. olaservo commented on Nov 15, 2026

    @olaservo
    Member

    Decision: Option B. #302 is merged, so quit and Ctrl-D are the clean exits in all five clients, Ctrl-C stops the client at once, every README says so, and the tool-loop test checks that Ctrl-C at the prompt ends the client without a hang or a backtrace. #300 can be closed.

    One small follow-up if you want the five fully alike: Ruby exits 0 after Ctrl-C while the other four exit 130.


    Generated by Claude Code

  3. a-akimov commented on Nov 16, 2026

    @a-akimov
    ContributorAuthor

    Thank you, @olaservo. Created #303 as a follow up on Ruby.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workinggoPull requests that update go codejavascriptPull requests that update javascript codepython:uvPull requests that update python:uv coderubyPull requests that update ruby coderustPull requests that update rust code

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions