Not returning on connection close for chainsync, block-fetch and tx-submission protocol
- Added changes to not return error while closing the connection for chainsync, block-fetch and tx-submission protocol- made changes in client,server and respective protocol files.
- Added unit tests to test the change.
Closes #1112
Summary by CodeRabbit
-
New Features
- Centralized connection error handling for more consistent closure semantics.
-
Improvements
- Per-protocol concurrency and explicit state tracking with safe state queries.
- Protocol contexts propagated for clearer cancellation and lifecycle control.
- Avoid redundant "done" messages during client shutdown to reduce noise.
-
Tests
- Expanded test coverage across connection handling, blockfetch, chainsync, txsubmission, and protocol lifecycle/configuration behaviors.
- Added changes to not return error while closing the connection for chainsync, block-fetch and tx-submission protocol- made changes in client,server and respective protocol files.
- Added unit tests to test the change.
Closes #1112
@coderabbitai review
β 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.
[!NOTE]
Other AI code review bot(s) detected
CodeRabbit has detected other AI code review bot(s) in this pull request and will avoid duplicating their findings in the review comments. This may lead to a less comprehensive review.
π Walkthrough
Walkthrough
This PR implements protocol state tracking and conditional error handling to address closing connections when protocols are shut down. It adds IsDone() and GetDoneState() methods to track protocol terminal states, introduces a currentState field to the Protocol struct for state management, and modifies Stop() implementations in blockfetch and chainsync clients to conditionally send final messages only when protocols are active. New test suites validate protocol initialization, configuration, and message-sending behavior across blockfetch, chainsync, and txsubmission protocols. Connection tests are refactored to verify error handling differs when protocols are active versus stopped.
Estimated code review effort
π― 3 (Moderate) | β±οΈ ~20 minutes
- Core logic changes across multiple protocol implementations require careful verification
- New API methods (
IsDone(),GetDoneState()) andcurrentStatefield tracking need validation for correctness with existing state machine - Multiple similar test files (blockfetch, chainsync, txsubmission) reduce individual review effort but cumulative coverage is significant
- Conditional logic in Stop() methods affects error propagation semantics and warrants close inspection
Areas requiring extra attention:
- Protocol.IsDone() state detection logic and its correctness against various protocol states
- Interaction between currentState tracking and existing state machine in stateLoop
- Conditional Stop() implementations in blockfetch/client.go and chainsync/client.go β verify state checks prevent message sends appropriately
- Connection error handling behavior with active vs. stopped protocols in connection_test.go refactoring
- Test cleanup delays (100ms sleeps in blockfetch/client_test.go and chainsync/client_test.go) β assess if timing is reliable
Possibly related PRs
#1233: Modifies protocol/protocol.go with currentState field and state-access methods, directly related to the state tracking infrastructure introduced here.
Pre-merge checks and finishing touches
β Failed checks (1 inconclusive)
| Check name | Status | Explanation | Resolution |
|---|---|---|---|
| Out of Scope Changes check | β Inconclusive | While most changes are in-scope, minor formatting changes (blank lines in txsubmission/client.go and keepalive/client_test.go) and a 100ms sleep delay in test cleanup appear tangential to the core objective. | Clarify if the sleep delays in blockfetch/client_test.go and chainsync/client_test.go are necessary for the feature or if they should be separated into a distinct refactoring PR. |
β Passed checks (3 passed)
| Check name | Status | Explanation |
|---|---|---|
| Description Check | β Passed | Check skipped - CodeRabbitβs high-level summary is enabled. |
| Title check | β Passed | The title clearly and specifically describes the main change: preventing error returns on connection close for three specific protocols (chainsync, block-fetch, tx-submission) after proper shutdown. |
| Linked Issues check | β Passed | The PR directly addresses issue #1112 by implementing the required functionality: preventing error returns when protocols are properly shut down before connection close across chainsync, block-fetch, and tx-submission. |
β¨ Finishing touches
- [ ] π Generate docstrings
π§ͺ Generate unit tests (beta)
- [ ] Create PR with unit tests
- [ ] Post copyable unit tests in a comment
- [ ] Commit unit tests in branch
feat/no-error-return
π Recent review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
π₯ Commits
Reviewing files that changed from the base of the PR and between 90450f286811a969c8f020f1f062f934a3b2ca78 and 26c42c7c9586b0da462e18d44fc730f850ec487b.
π Files selected for processing (5)
connection_test.go(2 hunks)protocol/blockfetch/client_test.go(1 hunks)protocol/chainsync/client.go(1 hunks)protocol/chainsync/client_test.go(1 hunks)protocol/protocol.go(3 hunks)
π§ Files skipped from review as they are similar to previous changes (1)
- protocol/protocol.go
π§° Additional context used
𧬠Code graph analysis (1)
protocol/chainsync/client.go (7)
protocol/keepalive/messages.go (1)
NewMsgDone(94-101)protocol/localtxmonitor/messages.go (1)
NewMsgDone(79-86)protocol/chainsync/messages.go (1)
NewMsgDone(269-276)protocol/localstatequery/messages.go (1)
NewMsgDone(245-252)protocol/peersharing/messages.go (1)
NewMsgDone(88-95)protocol/localtxsubmission/messages.go (1)
NewMsgDone(118-125)protocol/txsubmission/messages.go (1)
NewMsgDone(169-176)
πͺ GitHub Actions: go-test
protocol/blockfetch/client_test.go
[error] 103-103: TestGetBlock: found unexpected goroutines after test execution
[error] 103-103: TestGetBlockNoBlocks: found unexpected goroutines after test execution
[error] 104-104: TestGetCurrentTip: found unexpected goroutines after test execution
[error] 104-104: TestGetAvailableBlockRange: found unexpected goroutines after test execution
β° Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (2)
- GitHub Check: cubic Β· AI code reviewer
- GitHub Check: Analyze (go)
π Additional comments (2)
protocol/chainsync/client.go (1)
150-153: Idempotent Stop via IsDone guard looks correctConditioning the Done send on
!c.IsDone()underbusyMutexkeepsStopidempotent and avoids spurious Done messages after a terminal state, while still propagating anySendMessageerror via the outererr. This aligns with the intended shutdown semantics.connection_test.go (1)
315-353: Basic error-handling tests (DialFailure/DoubleClose) look goodThe new
TestBasicErrorHandlingsubtests correctly exercise dial failure and Close idempotency without overreaching into protocol behavior. Theyβre small, focused, and compatible with goleak whenClose()behaves as intended.
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.
Comment @coderabbitai help to get the list of available commands and usage tips.
I'm not sure if it's helpful, but I have some (abandoned) code on a similar path here: https://github.com/blinklabs-io/gouroboros/pull/1244
@cubic-dev-ai review this PR
@cubic-dev-ai review this PR
@wolf31o2 I've started the AI code review. It'll take a few minutes to complete.