Skip to content

Conversation

@lmagyar
Copy link
Contributor

@lmagyar lmagyar commented Nov 8, 2025

Proposed Changes

Based on my comment in #174 (#174 (comment))

This is useful in scripts that are not called by S6 and have their STDOUT and STDERR not initialized (ie. redirected to /dev/null) and they have to redirect their STDOUT and STDERR to eg. /proc/1/fd/1. This is valid for eg. Docker healthcheck scripts, some daemons started by S6 services (eg. in case of NetworkManager dispatcher only STDERR has valid value, but crond scripts are started with valid STDOUT and STDERR).

Possible enhancements:

  1. Maybe we can extend it with adding exec &>/proc/1/fd/1 before/outside the if? Add-on scripts can log/write only here. In this case this would be a general reinitialization for scripts not started by S6. This would be a useful addition.

  2. Or we can hide this function completely: if the shebang references bashio, bashio checks that STDOUT and STDERR has valid, non /dev/null values, and if any of them is /dev/null, redirects them to /proc/1/fd/1 automatically? This can be a breaking change for some corner cases.

Related Issues

Summary by CodeRabbit

  • New Features
    • Added the ability to reinitialize logging output so log messages can be redirected to the current standard output. This ensures logs remain visible and correctly routed after external changes to output streams, improving reliability of logging across scripts and runtime adjustments.

@coderabbitai
Copy link

coderabbitai bot commented Nov 8, 2025

Walkthrough

A new public function bashio::log.reinitialize_output() was added to lib/log.sh. It validates that LOG_FD is numeric and, if so, rebinds that file descriptor to the current STDOUT using an exec redirect (errors are suppressed).

Changes

Cohort / File(s) Change Summary
Log Output Reinitialization
lib/log.sh
Added public function bashio::log.reinitialize_output() that checks whether LOG_FD is a numeric file descriptor and rebinds it to the current STDOUT using an eval-based exec redirect, with error suppression.

Sequence Diagram(s)

sequenceDiagram
    autonumber
    participant Caller
    participant LogLib as bashio::log.reinitialize_output()
    participant Shell as Shell FD Table

    Caller->>LogLib: call reinitialize_output()
    LogLib->>LogLib: is LOG_FD numeric?
    alt LOG_FD is numeric
        LogLib->>Shell: exec {LOG_FD}>&1  (rebind to current STDOUT)
        Shell-->>LogLib: result (success / failure suppressed)
        LogLib-->>Caller: return (no error output)
    else not numeric
        LogLib-->>Caller: no-op (returns)
    end
Loading

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

  • Single new function in one file; straightforward conditional and exec usage.
  • Review focus:
    • Correctness of numeric validation for LOG_FD.
    • Safety of eval-based exec and proper error suppression.

Possibly related PRs

Suggested reviewers

  • frenck

Poem

🐰 I nudged a FD back to the sunlit stream,
Where stdout dances and loglines gleam.
A quiet exec, a gentle rebound,
Now echoes of output are easily found. ✨

Pre-merge checks and finishing touches

✅ Passed checks (3 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title directly describes the main change: a new function that redirects the LOG_FD file descriptor to a changed STDOUT, which aligns with the added public function bashio::log.reinitialize_output().
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.
✨ Finishing touches
  • 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment

📜 Recent review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 6f4a682 and b3804d8.

📒 Files selected for processing (1)
  • lib/log.sh (1 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
  • lib/log.sh

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 and usage tips.

Copy link

@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: 0

🧹 Nitpick comments (1)
lib/log.sh (1)

27-31: Consider adding fd writeability validation for consistency and robustness.

The function safely validates that LOG_FD is numeric before using it in eval, which is correct. However, the validation is less strict than the LOG_FD initialization at line 11, which also checks that the fd is actually writable using { : >&"${LOG_FD-2}"; }. If LOG_FD is set to a numeric value for a closed or invalid file descriptor, the exec will silently fail without indication.

While this may be acceptable if callers are expected to verify state, consider matching the validation rigor of line 11 for robustness:

function bashio::log.reinitialize_output() {
    if [[ "${LOG_FD-}" =~ ^[0-9]+$ ]] && { : >&"${LOG_FD}"; } 2>/dev/null; then
        eval "exec ${LOG_FD}>&1"
    fi
}

This ensures the fd exists and is writable before attempting redirection.

📜 Review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between f788a0b and 6f4a682.

📒 Files selected for processing (1)
  • lib/log.sh (1 hunks)

@lmagyar

This comment was marked as resolved.

@coderabbitai
Copy link

coderabbitai bot commented Nov 8, 2025

✅ Actions performed

Review triggered.

Note: CodeRabbit is an incremental review system and does not re-review already reviewed commits. This command is applicable only when automatic reviews are paused.

@lmagyar
Copy link
Contributor Author

lmagyar commented Nov 8, 2025

@coderabbitai review

@coderabbitai
Copy link

coderabbitai bot commented Nov 8, 2025

✅ Actions performed

Review triggered.

Note: CodeRabbit is an incremental review system and does not re-review already reviewed commits. This command is applicable only when automatic reviews are paused.

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.

1 participant