Skip to content

fix(glances): InfluxDB export broken in Glances v4 - #639

Open
ChrisHaPunkt wants to merge 6 commits into
hassio-addons:mainfrom
ChrisHaPunkt:fix/influxdb-export-v4-bugfix
Open

fix(glances): InfluxDB export broken in Glances v4#639
ChrisHaPunkt wants to merge 6 commits into
hassio-addons:mainfrom
ChrisHaPunkt:fix/influxdb-export-v4-bugfix

Conversation

@ChrisHaPunkt

@ChrisHaPunkt ChrisHaPunkt commented Jun 1, 2026

Copy link
Copy Markdown

Bug

Glances v4 changed the export mechanism: exporters must now be enabled via the --export CLI flag. However, --export is incompatible with -w (webserver mode) — Glances enforces this restriction since nicolargo/glances#614. Running Glances with both flags results in:

Export is only available in standalone or client mode

This causes the addon to crash on startup whenever any exporter (InfluxDB) is configured. Reported in issue #621 (9 👍).

Fix

Run two separate Glances processes when an exporter is configured:

  1. Webserver processglances -w … (serves the UI, same as before)
  2. Exporter processglances --quiet --export <name> … (no webserver, data export only)

Both run as background processes. A trap on SIGTERM/SIGINT forwards the signal to both children for clean shutdown when the addon stops. wait -n monitors both; if either exits unexpectedly, the other is killed and the container exits with code 1 (S6-Overlay will restart it).

When no exporter is configured, the script falls back to the original exec glances code path — no change in behaviour.

Testing

  • Start addon without InfluxDB enabled → Glances webserver starts normally
  • Start addon with InfluxDB v1 enabled → both processes start, data is exported, addon stops cleanly
  • Start addon with InfluxDB v2 enabled → same as above

Fixes #621

Summary by CodeRabbit

  • Bug Fixes
    • Web server now properly supports configured exporters by running the exporter as a separate background process in webserver mode.
    • Improved shutdown behavior so both the web server and exporter stop cleanly and containers exit reliably.
    • Increased robustness by monitoring that both processes keep running; unexpected exits now fail fast.
  • New Features
    • Exporter runs independently when configured and honors quiet/debug behavior.
  • Chores
    • Updated the bundled nginx package to a newer Alpine revision.

Glances v4 requires --export on the CLI but cannot combine it with -w
(webserver mode). Run a dedicated quiet/standalone instance for exports
while the webserver remains the supervised foreground process.

Fixes: hassio-addons#621
@coderabbitai

coderabbitai Bot commented Jun 1, 2026

Copy link
Copy Markdown

Review Change Stack

Note

Reviews paused

It looks like this branch is under active development. To avoid overwhelming you with review comments due to an influx of new commits, CodeRabbit has automatically paused this review. You can configure this behavior by changing the reviews.auto_review.auto_pause_after_reviewed_commits setting.

Use the following commands to manage reviews:

  • @coderabbitai resume to resume automatic reviews.
  • @coderabbitai review to trigger a single review.

Use the checkboxes below for quick actions:

  • ▶️ Resume reviews
  • 🔍 Trigger review

Walkthrough

The startup script now launches Glances exporter and webserver processes separately when exporters are configured, with coordinated signal handling and failure monitoring. The Dockerfile also updates the pinned nginx package revision.

Changes

Multi-process Glances startup with exporter isolation

Layer / File(s) Summary
Exporter instance initialization
glances/rootfs/etc/services.d/glances/run
Builds exporter options, starts Glances in the background for exporter targets, and records EXPORTER_PID.
Webserver launch and process coordination
glances/rootfs/etc/services.d/glances/run
Adds debug mode handling, starts the webserver separately, traps TERM/INT, forwards termination to both processes, escalates after 30 seconds, and falls back to exec when no exporter is configured.

Nginx package pin update

Layer / File(s) Summary
Nginx package revision
glances/Dockerfile
Updates the pinned nginx Alpine package revision used in the image build.

Estimated code review effort: 4 (Complex) | ~45 minutes

Possibly related PRs

  • hassio-addons/app-glances#617: Also changes how Glances --export is handled in the startup script, affecting exporter invocation and command construction.

Suggested labels: maintenance

Poem

🐰 I split the watchers, one to export,
One to serve the web by torchlit port.
If signals hop in, I heed them with care,
And keep the nginx pin tidy in there.

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Out of Scope Changes check ⚠️ Warning The nginx package pin in the Dockerfile appears unrelated to #621's Glances export startup fix. Remove the nginx version pin unless it is required for the Glances export fix and can be justified by the issue scope.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title matches the main fix: restoring Glances InfluxDB export on v4.
Linked Issues check ✅ Passed The new dual-process startup and shutdown logic addresses the Glances v4 export/webserver conflict and startup loop in #621.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
✨ 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.

@ChrisHaPunkt

Copy link
Copy Markdown
Author

@frenck — this is a focused bug-fix PR split off from #628. It contains a single change to the run script and nothing else.

Context: Glances v4 broke InfluxDB export for all users who have it enabled (issue #621, 9 👍). The --export flag is mutually exclusive with -w (webserver mode). The fix spawns a dedicated quiet exporter process alongside the webserver process.

The best-practices improvements from the original PR are tracked separately in #628.

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

Actionable comments posted: 2

🧹 Nitpick comments (1)
glances/rootfs/etc/services.d/glances/run (1)

61-73: ⚖️ Poor tradeoff

Consider adding shutdown timeout and SIGKILL escalation.

If either process hangs and doesn't respond to SIGTERM, the shutdown function will wait indefinitely. Best practice is to wait with a timeout and escalate to SIGKILL if needed.

♻️ Example with timeout and SIGKILL escalation
shutdown() {
    bashio::log.info "Stopping Glances..."
    bashio::log.debug "Sending SIGTERM to web server (PID: ${WEBSERVER_PID})..."
    kill "${WEBSERVER_PID}" 2>/dev/null
    bashio::log.debug "Sending SIGTERM to exporter (PID: ${EXPORTER_PID})..."
    kill "${EXPORTER_PID}" 2>/dev/null
    
    # Wait up to 10 seconds for graceful shutdown
    local timeout=10
    while (( timeout > 0 )); do
        if ! kill -0 "${WEBSERVER_PID}" 2>/dev/null && ! kill -0 "${EXPORTER_PID}" 2>/dev/null; then
            break
        fi
        sleep 1
        (( timeout-- ))
    done
    
    # Force kill if still running
    kill -9 "${WEBSERVER_PID}" 2>/dev/null
    kill -9 "${EXPORTER_PID}" 2>/dev/null
    wait "${WEBSERVER_PID}" 2>/dev/null
    wait "${EXPORTER_PID}" 2>/dev/null
    
    bashio::log.info "Glances stopped"
    exit 0
}
🤖 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 `@glances/rootfs/etc/services.d/glances/run` around lines 61 - 73, The
shutdown() function currently sends SIGTERM to WEBSERVER_PID and EXPORTER_PID
and then waits indefinitely; modify shutdown() to implement a bounded graceful
shutdown: after sending SIGTERM to WEBSERVER_PID and EXPORTER_PID, poll (e.g.,
with kill -0) in a short loop up to a configurable timeout (seconds) to detect
process exit, logging progress, and if either PID is still alive after the
timeout send SIGKILL (kill -9) to that PID, then perform wait on both PIDs and
log the final stopped messages; keep references to the existing WEBSERVER_PID
and EXPORTER_PID variables and preserve existing logging (bashio::log.*).
🤖 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.

Inline comments:
In `@glances/rootfs/etc/services.d/glances/run`:
- Around line 43-45: After backgrounding Glances with glances
"${exporter_options[@]}" & and storing EXPORTER_PID, verify the process is
actually running by waiting a short moment (e.g., sleep 1) then testing the pid
(use kill -0 or check /proc/$EXPORTER_PID); if the pid is not alive, call wait
$EXPORTER_PID to capture the exit status and log an error via bashio::log.error
including the exit code or reason, then exit non‑zero; if the pid is alive, keep
the existing bashio::log.info "Glances exporter started (PID: ${EXPORTER_PID})".
- Around line 57-59: The web server is backgrounded with glances and
WEBSERVER_PID is set but there's no verification it actually started; after
launching glances (glances "${options[@]}" & and WEBSERVER_PID=$!) add a quick
liveness check (e.g., short sleep then kill -0 $WEBSERVER_PID or check
/proc/$WEBSERVER_PID) and if the check fails, log an error via bashio::log.error
including the PID and exit or handle failure the same way the exporter does so
wait -n won't be triggered unexpectedly; keep the existing bashio::log.info
"Glances web server started (PID: ${WEBSERVER_PID})" only after the liveness
check succeeds.

---

Nitpick comments:
In `@glances/rootfs/etc/services.d/glances/run`:
- Around line 61-73: The shutdown() function currently sends SIGTERM to
WEBSERVER_PID and EXPORTER_PID and then waits indefinitely; modify shutdown() to
implement a bounded graceful shutdown: after sending SIGTERM to WEBSERVER_PID
and EXPORTER_PID, poll (e.g., with kill -0) in a short loop up to a configurable
timeout (seconds) to detect process exit, logging progress, and if either PID is
still alive after the timeout send SIGKILL (kill -9) to that PID, then perform
wait on both PIDs and log the final stopped messages; keep references to the
existing WEBSERVER_PID and EXPORTER_PID variables and preserve existing logging
(bashio::log.*).
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 06616c29-ac94-4d8a-b85f-c745b0aae9e3

📥 Commits

Reviewing files that changed from the base of the PR and between e8411c9 and 3ebf513.

📒 Files selected for processing (1)
  • glances/rootfs/etc/services.d/glances/run

Comment thread glances/rootfs/etc/services.d/glances/run
Comment thread glances/rootfs/etc/services.d/glances/run
If either child process ignores SIGTERM, the previous shutdown() would
wait indefinitely. Poll with kill -0 up to STOP_TIMEOUT seconds; force
SIGKILL on any PID still alive after the deadline.
@ChrisHaPunkt

Copy link
Copy Markdown
Author

Thanks for the review @coderabbitai!

Shutdown — bounded graceful shutdown with SIGKILL fallback ✅ Fixed in 434c9b8

Added a 30-second polling loop (kill -0) after SIGTERM. If either PID is still alive after the timeout, it gets SIGKILL before the final wait calls.


Startup verification (both findings) — Skipped intentionally

The wait -n call immediately below the glances … & launches acts as the crash detector: if either process fails at startup and exits, wait -n returns, bashio::log.fatal fires, and the container exits with code 1 for S6-Overlay to restart. Adding an extra sleep+kill -0 at startup would introduce artificial latency and duplicate logic already covered by wait -n.

@coderabbitai

coderabbitai Bot commented Jun 1, 2026

Copy link
Copy Markdown

Tip

For best results, initiate chat on the files or code changes.

🧩 Analysis chain

🏁 Script executed:

#!/bin/bash
cat glances/rootfs/etc/services.d/glances/run

Length of output: 131



@ChrisHaPunkt Thanks for the detailed follow-up!

Shutdown (bounded SIGKILL fallback): The 30-second kill -0 polling loop followed by a SIGKILL fallback before the final wait calls is the standard and correct pattern for bounded graceful shutdown. That's solid. ✅

Startup verification: Your reasoning holds — wait -n already covers the crash-at-startup case for both child processes. If either process exits immediately after launch, wait -n returns, bashio::log.fatal fires, and S6-Overlay gets a clean exit code 1 to trigger a restart. An additional sleep+kill -0 check would be redundant and introduce unnecessary latency. Acknowledged. ✅

The implementation looks well-reasoned overall. Good luck getting the maintainer review!

🐇 ✨

@ChrisHaPunkt

Copy link
Copy Markdown
Author

@coderabbitai review

@coderabbitai

coderabbitai Bot commented Jun 1, 2026

Copy link
Copy Markdown
✅ 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.

@ChrisHaPunkt

Copy link
Copy Markdown
Author

@coderabbitai review

@coderabbitai

coderabbitai Bot commented Jun 1, 2026

Copy link
Copy Markdown
✅ 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.

@ChrisHaPunkt
ChrisHaPunkt force-pushed the fix/influxdb-export-v4-bugfix branch from 2ae94b1 to 434c9b8 Compare June 1, 2026 13:09
@github-actions

github-actions Bot commented Jul 2, 2026

Copy link
Copy Markdown

There hasn't been any activity on this pull request recently. This pull request has been automatically marked as stale because of that and will be closed if no further activity occurs within 7 days. Thank you for your contributions.

@github-actions github-actions Bot added the stale There has not been activity on this issue or PR for quite some time. label Jul 2, 2026
@ChrisHaPunkt

Copy link
Copy Markdown
Author

Please @frenck, have a look into this PR to get #621 closed.

@McFcologne

Copy link
Copy Markdown

There hasn't been any activity on this pull request recently. This pull request has been automatically marked as stale because of that and will be closed if no further activity occurs within 7 days. Thank you for your contributions.

Ah yes, the stale bot. Nothing says "thank you for your contributions" like threatening to close the PR that fixes a bug still breaking everyone's startup on 0.22.0. The bug isn't stale, it's very much alive and looping. Maybe point the 7-day timer at #621 instead? 😄

@github-actions github-actions Bot removed the stale There has not been activity on this issue or PR for quite some time. label Jul 3, 2026
@tmonck

tmonck commented Jul 13, 2026

Copy link
Copy Markdown

@frenck what would it take to get this reviewed and merged?

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.

startup looping with new 0.22.0

3 participants