Skip to content

(Mirror) cli : Use acquire/release semantics for stopping logic#101

Open
ngxson wants to merge 2 commits intongxson:masterfrom
matthiasstraka:cli-atomics-cleanup
Open

(Mirror) cli : Use acquire/release semantics for stopping logic#101
ngxson wants to merge 2 commits intongxson:masterfrom
matthiasstraka:cli-atomics-cleanup

Conversation

@ngxson
Copy link
Copy Markdown
Owner

@ngxson ngxson commented Apr 15, 2026

@coderabbitai use mirror preset for review

mirror ggml-org#21822

Summary by CodeRabbit

  • Bug Fixes

    • Improved interrupt (Ctrl+C) responsiveness and more reliable cancellation behavior
    • Double-interrupt now immediately exits with cleaner terminal output
  • Performance

    • Simplified generation loop for more efficient and stable streaming output
  • Other

    • Ensured cancellation state is correctly reset after generation or interactive cancellation

Previously, seq_cst ordering was enforced, which is slower and has no benefits when using only one atomic variable.
@coderabbitai
Copy link
Copy Markdown

coderabbitai bot commented Apr 15, 2026

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: ffbd9262-7943-46e6-abff-832ab95033fc

📥 Commits

Reviewing files that changed from the base of the PR and between 3cdc177 and 07b8b2d.

📒 Files selected for processing (1)
  • tools/cli/cli.cpp
🚧 Files skipped from review as they are similar to previous changes (1)
  • tools/cli/cli.cpp

📝 Walkthrough

Walkthrough

Signal handling and interrupt state were refactored to use explicit atomic memory ordering and exchange semantics; the streaming generation loop condition was simplified; the stop flag is reset via a new reset function; cli_context::loading_show was removed.

Changes

Cohort / File(s) Summary
CLI interrupt & loop
tools/cli/cli.cpp
Reworked interrupt handling to use g_is_interrupted.exchange(true, std::memory_order_acq_rel) in the signal handler, should_stop() uses load(...memory_order_acquire), and a new reset_stop() uses store(...memory_order_release). Simplified streaming loop to while (result && !should_stop()) and replaced manual clears with reset_stop(). Removed cli_context::loading_show member.

Sequence Diagram(s)

sequenceDiagram
  participant User
  participant CLI as CLI Process
  participant Signal as OS Signal
  participant Gen as Generator Loop

  User->>CLI: Start generation
  CLI->>Gen: begin streaming generation
  Signal->>CLI: SIGINT/Ctrl+C
  CLI->>CLI: g_is_interrupted.exchange(true, memory_order_acq_rel)
  CLI->>Gen: should_stop()? (load memory_order_acquire)
  alt stop requested
    Gen->>CLI: stop loop (exit generation)
    CLI->>CLI: reset_stop() (store memory_order_release)
  else continue
    Gen->>Gen: continue streaming
  end
  CLI->>User: exit/cleanup
Loading

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Poem

🐇 I thumped a key, the signal rang,

exchange and orders neatly sang,
The loop now checks and gently stops,
A reset clears the tiny hops,
I nibble code — concurrency snacks! 🥕

🚥 Pre-merge checks | ✅ 1 | ❌ 2

❌ Failed checks (2 warnings)

Check name Status Explanation Resolution
Description check ⚠️ Warning The description is missing critical sections from the template, including Overview (what/why), detailed AI usage disclosure, and the required contributing guidelines acknowledgment. Expand description with Overview section explaining the changes and rationale, explicitly state AI usage (YES/NO), and add the required checklist acknowledging contributing guidelines.
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (1 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly describes the main change: refactoring the interrupt/stop mechanism to use acquire/release memory semantics in the CLI.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Copy Markdown

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@tools/cli/cli.cpp`:
- Around line 46-53: The second-interrupt handler uses non-async-signal-safe
functions (fprintf, fflush, std::exit); change it to only call async-signal-safe
primitives: use g_is_interrupted.exchange as-is, then write(2) to STDOUT_FILENO
(or use a literal escape sequence buffer) to clear colors, and call _exit(130)
instead of std::exit; update the block that currently references
g_is_interrupted and the fprintf/fflush/std::exit sequence to use write(...) and
_exit(...) so the signal handler is POSIX async-signal-safe.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: b0bbdb66-604d-4985-9d54-54b426872436

📥 Commits

Reviewing files that changed from the base of the PR and between 707c0b7 and 3cdc177.

📒 Files selected for processing (1)
  • tools/cli/cli.cpp

Comment thread tools/cli/cli.cpp
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants