-
Notifications
You must be signed in to change notification settings - Fork 4
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix(cli): add ctrlc listener to kill log loop in --no-tui
mode
#36
Conversation
WalkthroughThe change integrates signal handling into the Changes
Sequence Diagram(s)sequenceDiagram
participant User
participant SignalHandler
participant LogEvents
User->>SignalHandler: Press Ctrl-C
SignalHandler-->>LogEvents: Set stop_loop flag (AtomicBool true)
LogEvents->>LogEvents: Check stop_loop flag in loop
LogEvents->>LogEvents: Exit loop when flag is true
Possibly Related PRs
Poem
Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media? 🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments. CodeRabbit Commands (Invoked using PR comments)
Other keywords and placeholders
CodeRabbit Configuration File (
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 0
🧹 Nitpick comments (1)
crates/cli/src/cli/simnet/mod.rs (1)
197-202
: LGTM with suggestions for improvement.The implementation correctly uses thread-safe primitives for handling Ctrl-C signals. However, consider these improvements:
- The handler might be called multiple times if Ctrl-C is pressed repeatedly. Consider adding a guard to prevent multiple calls.
- The handler is not cleaned up when the function exits. Consider implementing cleanup to prevent potential issues in subsequent runs.
Here's a more robust implementation:
- let stop_loop = Arc::new(AtomicBool::new(false)); - let do_stop_loop = stop_loop.clone(); - ctrlc::set_handler(move || { - stop_loop.store(true, Ordering::Relaxed); - }) - .expect("Error setting Ctrl-C handler"); + let stop_loop = Arc::new(AtomicBool::new(false)); + let do_stop_loop = stop_loop.clone(); + let prev_handler = ctrlc::set_handler(move || { + if !stop_loop.load(Ordering::Relaxed) { + stop_loop.store(true, Ordering::Relaxed); + } + }) + .expect("Error setting Ctrl-C handler");Then at the end of the function (before the
Ok(())
), add:// Restore the previous Ctrl-C handler drop(prev_handler);
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (1)
crates/cli/src/cli/simnet/mod.rs
(2 hunks)
⏰ Context from checks skipped due to timeout of 90000ms (2)
- GitHub Check: build
- GitHub Check: run_cargo_checks
🔇 Additional comments (2)
crates/cli/src/cli/simnet/mod.rs (2)
4-7
: LGTM!The new imports for
AtomicBool
andArc
are correctly placed and necessary for implementing thread-safe signal handling.
205-207
: LGTM!The loop break condition is correctly implemented using atomic operations.
Summary by CodeRabbit