Skip to content

Clean up on Ctrl-C in the Go, Rust and Ruby clients - #300

Closed
a-akimov wants to merge 5 commits into
mainfrom
fix/269-ctrl-c-cleanup
Closed

a-akimov wants to merge 5 commits into
mainfrom
fix/269-ctrl-c-cleanup

Conversation

@a-akimov

Copy link
Copy Markdown
Contributor

Motivation and Context

Closes issue #269, remaining task from PR #290.

Ctrl-C at the prompt or during a query now ends the chat loop the way EOF does: the session is closed, the server shut down, and the client exits with status 0. That matches what the Python and TypeScript clients already do.

  • Go: signal.NotifyContext cancels the context. Stdin is read on a goroutine so the loop can watch the context too. The server command is now exec.Command instead of exec.CommandContext, because cancelling the context would kill the server before Cleanup closes the session. After the first Ctrl-C the default handler is restored, so a second one force-quits.
  • Rust: the session future races against tokio::signal::ctrl_c(), then cleanup runs. On that path the client exits explicitly, because tokio reads stdin on a blocking thread that would otherwise stop the runtime from shutting down.
  • Ruby: rescue Interrupt, which isn't a StandardError, so rescue => e didn't catch it. ensure already ran cleanup; this replaces the backtrace with a quiet exit.

How Has This Been Tested?

Locally:

  • Go: gofmt -l, go vet and build with Go 1.25.
  • Rust: cargo fmt --check, cargo clippy --all-targets -D warnings, cargo test.
  • Ruby: ruby -wc (syntax only; no Ruby 3.4 locally).
  • Go and Rust behaviour: started each client against the Go weather server, waited for the Query: prompt, sent SIGINT to the client process only, then checked that the client exited with status 0 and the server process was gone. Before the Rust exit fix, the client cleaned up the server but then hung on the blocking stdin thread.

Breaking Changes

None.

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)
  • Documentation update

Checklist

  • I have read the MCP Documentation
  • My code follows the repository's style guidelines
  • New and existing tests pass locally
  • I have added appropriate error handling
  • I have added or updated documentation as needed

Additional details

Assisted with Claude Code, checked by me.

Copilot AI balanced review requested due to automatic review settings November 15, 2026 12:14

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🟡 Changes recommended

The new Ctrl-C paths in Go, Rust, and Ruby lack required SIGINT regression coverage at the prompt and during pending queries, leaving clean exit behavior, server cleanup, and Rust stdin hangs unchecked.

1 open issue
What changed in this PR

This PR addresses the remaining Ctrl-C cleanup task from #269 after #290, aligning the Go, Rust, and Ruby tutorial clients with Python and TypeScript.

Changes:

  • Go handles interrupts through context cancellation while preserving orderly session cleanup.
  • Rust races the session against Ctrl-C, runs cleanup, then exits directly to avoid a blocking-stdin shutdown hang.
  • Ruby catches Interrupt for a quiet exit while retaining cleanup through ensure.
File Description
mcp-client-rust/​src/​main.rs Adds interrupt-aware session shutdown and explicit exit after cleanup.
mcp-client-ruby/​client.rb Catches Ctrl-C without an interrupt backtrace.
mcp-client-go/​main.go Adds cancellation-aware input and query handling, plus connection-failure cleanup.

Review effort: Balanced

Comment thread mcp-client-rust/src/main.rs Resolved
Copilot AI balanced review requested due to automatic review settings November 15, 2026 13:31

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🟡 Changes recommended

A blocking Python API call prevents timely Ctrl-C cleanup, while an uncanceled readiness timer keeps successful test helpers alive and adds roughly 15 minutes to the smoke suite.

3 open issues

Review effort: Balanced

Comment thread mcp-client-python/client.py Resolved
Comment thread tests/helpers/tool-loop-test.ts Resolved
Copilot AI balanced review requested due to automatic review settings November 15, 2026 13:50

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🔵 Needs a closer look

Closing stdin after submitting a query aborts the signal shared with processQuery, canceling the pending API request or tool call and silently discarding its answer.

1 open issue
1 previously missed issue

In code that hasn't changed since last review

Medium severity Avoid cancelling queries when stdin closes

mcp-client-typescript/​index.ts:260

This signal is also aborted by readline's close event on stdin EOF. Closing stdin after submitting a query now cancels the pending API request or tool call and silently discards its answer, instead of letting the query finish. Use a separate SIGINT-only signal for processQuery, keeping the readline-close signal for rl.question.

2 resolved since last review

Review effort: Balanced

@a-akimov

Copy link
Copy Markdown
Contributor Author

Well, it becomes too complicated for a quickstart tutorial. Let me try in a different branch a simplifeid version, then we can compare and decide which one to use,

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🟡 Changes recommended

Interactive Ctrl-C is not handled by readline, so pending API requests or tool calls are not cancelled and cleanup may be delayed until completion or timeout.

1 open issue
1 resolved since last review

Review effort: Balanced

Comment thread mcp-client-typescript/index.ts Resolved

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🟢 Approval recommended

No active review issues block the cross-client Ctrl-C cleanup changes.

No open issues 🎉

1 resolved since last review

Review effort: Balanced

Copy link
Copy Markdown
Member

We went with Option B from #301 and merged #302. The full clean-shutdown version works, and thank you for building both so they could be compared, but the asyncio, goroutine, tokio::select! and abort-signal machinery lands in code the tutorials quote, and quit and Ctrl-D already give readers a clean exit. This one can be closed unless you want to keep it around for reference.


Generated by Claude Code

@a-akimov a-akimov closed this Nov 16, 2026
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.

3 participants