Skip to content

Fix pluginsd cleanup race with active collector - #22207

Merged
stelfrag merged 2 commits into
netdata:masterfrom
stelfrag:pluginsd-cleanup-live-collector-race
Apr 14, 2026
Merged

Fix pluginsd cleanup race with active collector#22207
stelfrag merged 2 commits into
netdata:masterfrom
stelfrag:pluginsd-cleanup-live-collector-race

Conversation

@stelfrag

@stelfrag stelfrag commented Apr 14, 2026

Copy link
Copy Markdown
Collaborator
Summary
  • Prevent lifecycle violations by improving thread ownership checks.
  • Address race condition when releasing PRD dimension references under active collector threads.
  • Refactor stale collector_tid handling and clarify comments.

Summary by cubic

Fixes a race where pluginsd cleanup could free PRD dimension refs while a collector thread still used them. Enforces strict ownership: cleanup only proceeds in the collector thread; otherwise it is skipped.

  • Bug Fixes
    • Skip cleanup when collector_tid is set and not the current thread; stop relying on RRDSET_FLAG_COLLECTION_FINISHED.
    • If called by the collector thread, assert/log and set collector_tid=0 before proceeding; clarify that teardown clears it in rrdhost_pluginsd_receive_chart_slots_free().
    • Remove stale collector_tid handoff path and tighten teardown comments/logging to prevent lifecycle violations.

Written for commit 4a2f22a. Summary will update on new commits.

- Prevent lifecycle violations by improving thread ownership checks.
- Address race condition when releasing PRD dimension references under active collector threads.
- Refactor stale `collector_tid` handling and clarify comments.

@cubic-dev-ai cubic-dev-ai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No issues found across 1 file

Confidence score: 5/5

  • Automated review surfaced no issues in the provided summaries.
  • No files require special attention.
Architecture diagram
sequenceDiagram
    participant Svc as Service/Cleanup Thread
    participant Coll as Collector Thread
    participant RRD as RRDSET (Chart Metadata)
    participant Mem as PRD Dimension Memory

    Note over Svc,Mem: Cleanup Request Flow (rrdset_pluginsd_receive_unslot_and_cleanup)

    Svc->>RRD: atomic_load(collector_tid)
    RRD-->>Svc: returns active_tid

    alt NEW: collector_tid != current_tid (Race Prevention)
        Note right of Svc: Another thread owns the chart.
        Svc->>Svc: Log warning (Skipping cleanup)
        Note over Svc,RRD: CHANGED: We no longer rely on<br/>RRDSET_FLAG_COLLECTION_FINISHED<br/>as it was prone to races.
    else CHANGED: collector_tid == current_tid (Collector teardown)
        Svc->>Svc: internal_fatal() (if NETDATA_INTERNAL_CHECKS)
        Svc->>RRD: NEW: atomic_store(collector_tid, 0)
        Svc->>Mem: Free PRD dimension references
    else collector_tid == 0 (Safe State)
        Svc->>Mem: Free PRD dimension references
    end

    Note over Coll,Mem: Active Collection Flow (concurrent)
    Coll->>RRD: Access chart dimensions
    opt collector_tid != 0
        Coll->>Mem: Dereference pointers
        Note left of Mem: PRD references remain valid because<br/>Service Thread skipped cleanup.
    end
Loading

@stelfrag
stelfrag marked this pull request as ready for review April 14, 2026 09:41
Copilot AI review requested due to automatic review settings April 14, 2026 09:41
@stelfrag
stelfrag marked this pull request as draft April 14, 2026 09:41

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Fixes a race in pluginsd slot cleanup where PRD dimension references could be released while an active collector thread may still be dereferencing cached pointers, by tightening collector thread ownership checks during cleanup.

Changes:

  • Skip rrdset_pluginsd_receive_unslot_and_cleanup() when collector_tid is set and owned by a different thread (no longer relying on RRDSET_FLAG_COLLECTION_FINISHED as a gate).
  • When cleanup is invoked from the collector thread, explicitly log/assert and force-clear collector_tid before proceeding.
  • Remove the prior “stale collector_tid handoff” path and clarify inline comments to avoid lifecycle violations.
Comments suppressed due to low confidence (1)

src/database/rrdset-slots.c:229

  • This warning is emitted when cleanup is skipped due to an active collector, but it doesn't include chart/host identification, which can make it hard to action (especially if this can happen during shutdown/finalization across many charts). Consider including rrdhost_hostname(st->rrdhost) and rrdset_id(st) (or similar identifiers) in the log fields/message.
            nd_log_limit(&erl, NDLS_DAEMON, NDLP_WARNING,
                         "PLUGINSD: attempted cleanup while collector (tid %d) is still active on chart, skipping",
                         collector_tid);

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread src/database/rrdset-slots.c Outdated
@stelfrag
stelfrag marked this pull request as ready for review April 14, 2026 10:04

@thiagoftsm thiagoftsm left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PR is running as expected during few hours. LGTM!

@stelfrag
stelfrag merged commit 5419eec into netdata:master Apr 14, 2026
150 checks passed
@stelfrag
stelfrag deleted the pluginsd-cleanup-live-collector-race branch April 14, 2026 18:33
nedi-app Bot pushed a commit that referenced this pull request Apr 24, 2026
* Fix cleanup logic for collector thread in RRDSET

- Prevent lifecycle violations by improving thread ownership checks.
- Address race condition when releasing PRD dimension references under active collector threads.
- Refactor stale `collector_tid` handling and clarify comments.

* Clarify collector teardown comment in `rrdset-slots.c` for improved readability
@stelfrag stelfrag mentioned this pull request Jun 22, 2026
Ferroin pushed a commit that referenced this pull request Jul 15, 2026
* Fix cleanup logic for collector thread in RRDSET

- Prevent lifecycle violations by improving thread ownership checks.
- Address race condition when releasing PRD dimension references under active collector threads.
- Refactor stale `collector_tid` handling and clarify comments.

* Clarify collector teardown comment in `rrdset-slots.c` for improved readability

(cherry picked from commit 5419eec)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants