fix(daemon): send log_message to stderr to avoid polluting command substitution - #19
Open
dmitrytkachuk wants to merge 1 commit into
Conversation
…bstitution
calculate_sleep_duration() emits a "Time remaining: N minutes" log via
log_message and returns the sleep interval on stdout. The caller captures
that stdout with $(calculate_sleep_duration), so before this change the
captured value was the multi-line string:
[2026-04-19 01:00:53] Time remaining: 59 minutes
600
Then `$((sleep_duration / 60))` crashed with:
line 471: [2026-04-19 ...] Time remaining: 59 minutes
600: syntax error: operand expected (error token is "...")
which killed the daemon every loop iteration. Under systemd with
Restart=always, the daemon crash-looped ~500+ times overnight and renewal
checks never ran.
The bug was dormant previously because the only log_message call inside
calculate_sleep_duration sits on the ccusage branch; when ccusage
returned nothing, the fallback branch (no log_message) was taken.
Fix: redirect log_message's echo|tee to stderr. journal/terminal output
is unchanged (systemd captures both streams), and stdout capture by
callers is no longer polluted. Verified that no callsite captures
log_message output via command substitution, so this is safe globally.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
log_messagewrites to stdout viatee. Insidecalculate_sleep_duration()it logsTime remaining: N minutes, but the caller captures that function's stdout withsleep_duration=$(calculate_sleep_duration). Sosleep_durationbecomes a two-line string (log text + number), and$((sleep_duration / 60))crashes with a bash syntax error every loop iteration.Under
systemdwithRestart=always, the daemon crash-loops and renewals never fire. My instance restarted 500+ times overnight.Repro
Observed in journal:
The bug was latent until
ccusagestarted returning a value — the "ccusage returned nothing" fallback branch does not calllog_message, so earlier installs worked by accident.Fix
Redirect
log_messageoutput to stderr (tee -a "$LOG_FILE" >&2). systemd/journal capture both streams, so user-visible log output is unchanged. Verified no callsite uses$(log_message ...), so this is safe globally.Test plan
systemctl --user; observed 7+ successful 10-minute check cycles with no restarts, no syntax errors, andTime remainingcorrectly decreasing asccusagedata ticks down.