Skip to content

Fix memory-safety and correctness bugs surfaced by Coverity audit (part 8) - #22293

Merged
stelfrag merged 1 commit into
netdata:masterfrom
ktsaou:cov_fix_part8
Apr 27, 2026
Merged

Fix memory-safety and correctness bugs surfaced by Coverity audit (part 8)#22293
stelfrag merged 1 commit into
netdata:masterfrom
ktsaou:cov_fix_part8

Conversation

@ktsaou

@ktsaou ktsaou commented Apr 27, 2026

Copy link
Copy Markdown
Member

Summary

Single follow-up commit not yet covered by parts 1-7 (#22266, #22267, #22268, #22270, #22279, #22280, #22281).

@stelfrag — could you pick this one up and review when you get a chance? Thanks for splitting the original PR into reviewable parts.

CID 442107 (SLEEP) — daemon: free removed stale nodes outside rrd lock

remove_ephemeral_host() in src/daemon/commands.c held the global RRD write lock while calling rrdhost_free___while_having_rrd_wrlock(), which performs blocking teardown work (logging, stream-shutdown waits, data collection cleanup, health cleanup). Every other RRD writer was stalled meanwhile.

The fix splits host removal into:

  • rrdhost_unlink___while_having_rrd_wrlock() — fast index removal, runs under the lock
  • rrdhost_free_unlinked() — slow teardown (cleanup_data_collection_and_health, free), runs after the lock is released

A new wrapper rrdhost_free___without_having_rrd_wrlock() encapsulates the lock-then-unlink-then-unlock-then-free pattern for callers like remove_ephemeral_host().

Files changed:

  • src/daemon/commands.c
  • src/database/rrdhost.c
  • src/database/rrdhost.h

3 files changed, +23 -15.

Test plan

  • CI build passes
  • Coverity scan no longer reports CID 442107

Summary by cubic

Fixes Coverity CID 442107 by moving slow host teardown outside the global RRD write lock to prevent stalls. Host unlinking now happens under the lock, with teardown after unlock for better responsiveness.

  • Bug Fixes
    • Unlink under lock with rrdhost_unlink___while_having_rrd_wrlock(), then teardown via rrdhost_free_unlinked() after releasing the lock.
    • Added rrdhost_free___without_having_rrd_wrlock() and updated remove_ephemeral_host() to use it.
    • Keeps cleanup order intact while minimizing lock hold time.

Written for commit 509be03. Summary will update on new commits. Review in cubic

Coverity CID 442107 (SLEEP): remove-stale-node held the global RRD write lock while freeing a host. Unlink the host under the lock, then run teardown/freeing outside the lock so logging and stream shutdown waits do not block global RRD progress.

@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 3 files

Confidence score: 5/5

  • Automated review surfaced no issues in the provided summaries.
  • No files require special attention.
Architecture diagram
sequenceDiagram
    participant D as Daemon (Commands)
    participant L as RRD Global Lock
    participant H as RRDHost Logic
    participant I as RRD Index & List
    participant S as Health & Collection Subsystems

    Note over D,S: Ephemeral Host Removal Flow

    D->>H: NEW: rrdhost_free___without_having_rrd_wrlock(host)
    
    H->>L: rrd_wrlock()
    Note right of L: Blocks other writers
    
    H->>I: NEW: rrdhost_unlink___while_having_rrd_wrlock()
    I-->>H: Removed from GUID index & Linked List
    
    H->>L: rrd_wrunlock()
    Note right of L: Lock released early
    
    Note over H,S: CHANGED: Slow teardown occurs outside global lock
    
    H->>S: rrdhost_cleanup_data_collection_and_health()
    activate S
    Note right of S: Blocking I/O, stream shutdown, health cleanup
    S-->>H: Teardown complete
    deactivate S
    
    H->>H: freez(host)
    H-->>D: Return success status
Loading

@sonarqubecloud

Copy link
Copy Markdown

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.

Copilot encountered an error and was unable to review this pull request. You can try again by re-requesting a review.

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.

Copilot encountered an error and was unable to review this pull request. You can try again by re-requesting a review.

@stelfrag
stelfrag requested a review from Copilot April 27, 2026 14:38

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.

Copilot encountered an error and was unable to review this pull request. You can try again by re-requesting a review.

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

Copilot reviewed 3 out of 3 changed files in this pull request and generated 2 comments.


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

Comment thread src/database/rrdhost.h
Comment thread src/database/rrdhost.c
@stelfrag
stelfrag merged commit 8e49531 into netdata:master Apr 27, 2026
173 checks passed
@stelfrag stelfrag mentioned this pull request Jun 22, 2026
Ferroin pushed a commit that referenced this pull request Jul 15, 2026
…rt 8) (#22293)

daemon: free removed stale nodes outside rrd lock

Coverity CID 442107 (SLEEP): remove-stale-node held the global RRD write lock while freeing a host. Unlink the host under the lock, then run teardown/freeing outside the lock so logging and stream shutdown waits do not block global RRD progress.

(cherry picked from commit 8e49531)
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