Skip to content

CommandListener: stop busy-spinning on stdin EOF#230

Merged
Segfaultd merged 1 commit into
MafiaHub:developfrom
Kheartz:fix/command-listener-eof-spin
Jul 11, 2026
Merged

CommandListener: stop busy-spinning on stdin EOF#230
Segfaultd merged 1 commit into
MafiaHub:developfrom
Kheartz:fix/command-listener-eof-spin

Conversation

@Kheartz

@Kheartz Kheartz commented Jul 11, 2026

Copy link
Copy Markdown
Collaborator

The console reader thread looped on std::getline(std::cin, ...) and continued on empty lines. With a non-interactive stdin (a dedicated server in a Docker container with no TTY), std::cin is at EOF and getline returns immediately without blocking, so the loop pegs a full CPU core. Interactive runs block in getline and are unaffected, which is why this only showed up on the Linux/Docker server, not Windows.

Break out of the loop when stdin is permanently closed (EOF); on any other transient stream failure, clear and back off instead of spinning.

Summary by CodeRabbit

  • Bug Fixes
    • Improved command-line input handling in non-interactive environments.
    • Prevented excessive CPU usage when standard input encounters temporary failures.
    • Ensured input processing stops cleanly when the input stream reaches end-of-file.

The console reader thread looped on std::getline(std::cin, ...) and
`continue`d on empty lines. With a non-interactive stdin (a dedicated
server in a Docker container with no TTY), std::cin is at EOF and
getline returns immediately without blocking, so the loop pegs a full
CPU core. Interactive runs block in getline and are unaffected, which
is why this only showed up on the Linux/Docker server, not Windows.

Break out of the loop when stdin is permanently closed (EOF); on any
other transient stream failure, clear and back off instead of spinning.
@Kheartz Kheartz requested a review from Segfaultd July 11, 2026 07:01
@coderabbitai

coderabbitai Bot commented Jul 11, 2026

Copy link
Copy Markdown

Review Change Stack

Walkthrough

The command listener now handles failed stdin reads by exiting on EOF or clearing recoverable errors and retrying after a 100ms delay, preventing busy-spinning in non-interactive environments.

Changes

Command Listener Stdin Handling

Layer / File(s) Summary
Stdin failure and retry handling
code/framework/src/utils/command_listener.cpp
Adds <chrono> and updates the stdin loop to distinguish EOF from recoverable failures, clear stream state, and pause before retrying.

Estimated code review effort: 2 (Simple) | ~10 minutes

Poem

I’m a rabbit with ears tuned to streams,
No more spinning through empty dreams.
EOF says hop, and errors clear,
Then wait a blink before reappear.
A calmer command loop now bounds along.

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title is concise and accurately summarizes the main change: preventing busy-spinning in CommandListener on stdin EOF.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

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

@coderabbitai coderabbitai Bot 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.

🧹 Nitpick comments (1)
code/framework/src/utils/command_listener.cpp (1)

22-31: 📐 Maintainability & Code Quality | 🔵 Trivial | 💤 Low value

LGTM!

The EOF detection and 100ms backoff correctly prevent busy-spinning in non-interactive environments. The interaction with Shutdown() is sound: if _running is set to false during the sleep, the continue loops back to while (_running) and exits cleanly.

One minor optional nit: the 100 ms backoff is a magic number. Consider extracting it to a named constant (e.g., static constexpr auto kRetryDelay = std::chrono::milliseconds(100);) for clarity and easier tuning.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@code/framework/src/utils/command_listener.cpp` around lines 22 - 31,
Optionally replace the magic 100-millisecond delay in the command-listening loop
with a named constant, such as kRetryDelay, defined near the relevant class or
function, and use it in std::this_thread::sleep_for.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Nitpick comments:
In `@code/framework/src/utils/command_listener.cpp`:
- Around line 22-31: Optionally replace the magic 100-millisecond delay in the
command-listening loop with a named constant, such as kRetryDelay, defined near
the relevant class or function, and use it in std::this_thread::sleep_for.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: b2f502bf-0759-4cf4-83df-4991a759fcff

📥 Commits

Reviewing files that changed from the base of the PR and between 3e581f2 and 4a44b76.

📒 Files selected for processing (1)
  • code/framework/src/utils/command_listener.cpp

@Segfaultd Segfaultd merged commit b5af770 into MafiaHub:develop Jul 11, 2026
5 of 6 checks passed
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