← Back to Pull requests

Don't leak/taint `errno` outside `rcutils_log()` #615

Closed
unknown closed this pull request without merging on Nov 30, 2026 01:57 UTC
View on GitHub
unknown unknown commented on Nov 27, 2026 16:25 UTC
Author

Description

rcutils_log() can change errno even when logging succeeds. With RCUTILS_COLORIZED_OUTPUT unset, rcutils_logging_console_output_handler() calls isatty(fileno(stream)) for every message to decide on colours. When the output is not a terminal, isatty() returns 0 and sets errno to ENOTTY. That is always the case for processes started by ros2 launch, which gives them pipes unless emulate_tty is set, and for output redirected to a file or run under systemd.

The C standard allows that, but it causes problems in two common situations:

  • Logging in an error path. For example, after a failed open(), a call to RCUTILS_LOG_ERROR(...) (or RCLCPP_ERROR) followed by return errno; hands back ENOTTY instead of the real error, but only when the output isn't a terminal.

  • Libraries that read errno after running callbacks. Some libraries read errno after running user callbacks and treat any non-zero value as a failure. We hit this with the Lely CANopen stack: its ev::Loop::run() clears errno, runs the loop, and throws std::system_error if errno is set afterwards. A driver callback that logged through RCLCPP_INFO made the process abort at shutdown with run: Inappropriate ioctl for device, but only when started via ros2 launch.
  • This PR saves errno before the output handler is called and restores it afterwards. It's done in vrcutils_log_internal(), so it covers both rcutils_log() and rcutils_log_internal(), and therefore every RCUTILS_LOG_ / RCLCPP_ macro, whatever output handler is installed (console, rosout, external file loggers). The header documents the guarantee.

    Two tests are added in test/test_logging.cpp:

  • test_log_preserves_errno: a custom output handler sets errno = EBADF. The test checks that rcutils_log() and rcutils_log_internal() leave both a pending errno (EINTR) and a cleared one (0) unchanged.

  • test_console_log_to_pipe_preserves_errno (POSIX only): this is the real-world case. It uses the default console handler with colours on auto and stderr redirected to a pipe, and checks that the message reached the pipe and that errno is still 0.
  • Both fail without the change; the pipe test sees errno == ENOTTY. Both pass with it.

    Is this user-facing behavior change?

    Logging no longer changes errno. Nothing else changes: the output is the same, and colours are chosen the same way.

    Did you use Generative AI?

    Claude Code (Claude Opus 5.5) was used to investigate the original crash and to draft the change, the tests and this description. All of it was reviewed by the author.

    Additional Information

    Tested on Ubuntu 24.04 (glibc) against a ROS 2 Lyrical underlay. The existing logging tests still pass: test_logging, test_logging_console_output_handler, test_logging_custom_env, test_logging_custom_env2, test_logging_enable_for, test_logging_bad_env, test_logging_long_messages. ament_uncrustify and ament_cpplint are clean on the changed files. The gmock-based tests and test_logging_output_format were not run locally, so CI will cover them.

    Diff: 391 lines (+293 / -98) across 4 files Reviewer: fujitatomoya Review duration: 3451m
    All Vouch review confidence checks passed
    Residual risk 0.17 = Change Risk (0.43) × (1 − Review Confidence (0.60)). Review depth and reviewer attention state are adequate.
    Model 1 · Change Risk (XGBoost)
    Score: 0.43 · 391 lines diff across 4 files
    0.43
    Model 2 · Review Depth (DistilBERT)
    Depth Score: 0.17 · 1 substantive comment(s)
    0.17
    Model 3 · Review Timing & Attention
    Session adequacy: 100% · Reviewer familiarity: 35%
    60%
    Bedrock Claude 3 · Risk Narration
    (disconnected)
    Groq
    Groq Risk Narration
    AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
    Model 1 · Change Risk Attribution
    SHAP Weights
    Diff Size diff_size
    0.357
    Shallow Review Depth shallow_review_depth
    0.289
    Real PR Conversation Timeline
    1 comments
    "Review active on #615"
    general weight: 0.35
    Reviewers
    Assignees
    Unassigned
    Labels
    risk: low confidence: 60.5% reviewed
    Vouch Risk Breakdown
    AI-powered review risk analysis
    Change Risk
    Change Risk · Model 1 (XGBoost)
    Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
    Score: 43.1% (MEDIUM) · Higher = larger blast radius
    43.1%
    Review Confidence
    Review Confidence · Models 2 & 3
    Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
    Score: 60.5% · Lower = rushed or shallow review
    60.5%
    Residual Risk
    Residual Risk · Core Equation
    Residual = Change × (1 − Confidence)
    Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
    Score: 17% · Safe to merge
    17%
    Repository