Skip to content

fix(node): close partial listeners on startRPC failure - #5869

Merged
aljo242 merged 4 commits into
cometbft:mainfrom
songgaoye:song/fix_node_startRPC
May 20, 2026
Merged

aljo242 merged 4 commits into
cometbft:mainfrom
songgaoye:song/fix_node_startRPC

Conversation

@songgaoye

@songgaoye songgaoye commented May 14, 2026 •

Copy link
Copy Markdown
Contributor

If startRPC opened some listeners before a later Listen call failed,
those listeners were leaked. Fix by deferring cleanup behind a success
flag.

PR checklist

  • Tests written/updated
  • Changelog entry added in CHANGELOG.md
  • Updated relevant documentation (docs/ or spec/) and code comments

@songgaoye
songgaoye requested a review from a team as a code owner May 14, 2026 11:19
@aljo242

aljo242 commented May 19, 2026

Copy link
Copy Markdown
Collaborator

@greptile review

@greptile-apps

greptile-apps Bot commented May 19, 2026 •

Copy link
Copy Markdown
Contributor

Greptile Summary

This PR fixes a file descriptor leak in node.startRPC: when multiple listen addresses are configured and a later rpcserver.Listen call fails, any listeners that were successfully opened before the failure were never closed. The fix introduces a success-flag (ok) pattern with a deferred cleanup loop.

  • node/node.go: Adds a deferred cleanup function that iterates over listeners and closes any non-nil entries, guarded by an ok flag that is set to true only on the successful return path. The closure captures the listeners variable by reference, so the gRPC listener appended via append at line 917 is also covered.
  • node/node_test.go: Adds TestStartRPCCleansUpOnFailure, which forces the second listen to fail (by keeping l2 open), then verifies cleanup by re-binding the first port — confirming the deferred close actually ran.

Confidence Score: 5/5

Safe to merge — the fix is a well-scoped addition of a deferred cleanup that does not affect the success path.

The ok-flag pattern correctly gates cleanup on failure. The deferred closure captures listeners by variable reference, so it sees the gRPC listener even if append allocates a new backing array. The test meaningfully validates cleanup by re-binding the freed port, not just asserting an error was returned. No existing behaviour on the success path is altered.

No files require special attention.

Important Files Changed

Filename Overview
node/node.go Adds deferred listener cleanup with an ok-flag; logic is sound and correctly covers both TCP and gRPC listeners via variable capture.
node/node_test.go New test correctly verifies cleanup by probing the first port after a forced failure, serving as a valid regression guard.
CHANGELOG.md Changelog entry added for the bug fix, correctly categorized under the bug-fix section.

Flowchart

%%{init: {'theme': 'neutral'}}%%
flowchart TD
    A[startRPC called] --> B[ConfigureRPC]
    B --> C{Error?}
    C -- yes --> Z[return nil, err]
    C -- no --> D[make listeners slice\nset ok=false\ndefer cleanup]
    D --> E[Loop over listenAddrs]
    E --> F[rpcserver.Listen]
    F --> G{Error?}
    G -- yes --> H[return nil, err\ntrigger defer]
    G -- no --> I[launch Serve goroutine\nlisteners i = listener]
    I --> E
    E -- done --> J{gRPC addr set?}
    J -- no --> K[ok = true\nreturn listeners, nil]
    J -- yes --> L[rpcserver.Listen gRPC]
    L --> M{Error?}
    M -- yes --> N[return nil, err\ntrigger defer]
    M -- no --> O[launch gRPC goroutine\nlisteners = append listener]
    O --> K
    H --> P[defer: ok==false\nclose all non-nil listeners]
    N --> P
    P --> Q[log any close errors]
Loading

Reviews (2): Last reviewed commit: "test(node): strengthen TestStartRPCClean..." | Re-trigger Greptile

Comment thread node/node_test.go Resolved

@aljo242 aljo242 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Thanks for the fix here. I have one merge-blocking concern on the test coverage for the cleanup behavior.

TestStartRPCCleansUpOnFailure currently only asserts that startRPC() returns an error. That outcome happens regardless of whether partial listeners are actually cleaned up, so the test doesn't prove the cleanup path and won't catch a regression that leaks the first listener.

Can we strengthen the test to verify cleanup explicitly (e.g., capture the first bound ephemeral port and assert it can be rebound after startRPC() fails)?

@aljo242

aljo242 commented May 19, 2026

Copy link
Copy Markdown
Collaborator

Non-blocking changelog style nit: the entry currently uses commit-prefix prose (fix(node): ...).

For consistency with surrounding entries, please use plain descriptive wording after the module tag (e.g. [node] close partial listeners on startRPC failure).

@songgaoye
songgaoye requested a review from aljo242 May 20, 2026 09:03

@aljo242 aljo242 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Re-reviewed latest updates and re-ran local stress on the cleanup test:
go test ./node -run TestStartRPCCleansUpOnFailure -count=20

Passes, and the test now explicitly validates that the first listener is reclaimable after failure.

@aljo242
aljo242 added this pull request to the merge queue May 20, 2026
Merged via the queue into cometbft:main with commit 754e62b May 20, 2026
53 of 54 checks passed
@songgaoye
songgaoye deleted the song/fix_node_startRPC branch May 20, 2026 14:51
kevindharmawan pushed a commit to SeeMoReLab/cometbft that referenced this pull request Jul 30, 2026
---
If `startRPC` opened some listeners before a later Listen call failed,
those listeners were leaked. Fix by deferring cleanup behind a success
flag.
#### PR checklist

- [x] Tests written/updated
- [x] Changelog entry added in `CHANGELOG.md`
- [ ] Updated relevant documentation (`docs/` or `spec/`) and code
comments
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.

2 participants