Skip to content

fix(eth): move initial transaction sync off the peer handshake path - #2540

Open
gzliudan wants to merge 2 commits into
XinFinOrg:dev-upgradefrom
gzliudan:fix-peer-sync-tx-blocking
Open

fix(eth): move initial transaction sync off the peer handshake path#2540
gzliudan wants to merge 2 commits into
XinFinOrg:dev-upgradefrom
gzliudan:fix-peer-sync-tx-blocking

Conversation

@gzliudan

@gzliudan gzliudan commented Aug 24, 2026

Copy link
Copy Markdown
Collaborator

Proposed changes

Summary

ProtocolManager.handle() ran syncTransactions synchronously between registering a peer and entering its message loop, so a peer could not read a single message until txpool.Pending returned. While the pool lock was held, the peer answered nothing and the downloader eventually dropped it with err=timeout. A stack dump from a frozen mainnet node showed 20 peer registrations parked in makeProtocol -> handle -> syncTransactions -> Pending.

The freeze itself is fixed elsewhere (the pool lock was pinned by promoteSpecialTx delivering an event while holding it, and by a peer broadcaster that stopped draining its queue after a send error). This PR removes the coupling so peer registration no longer depends on the transaction pool being responsive, and adds a watchdog so a wedged pool stays visible in the logs instead of going silent.

Changes

Commit 1 — refactor(eth): move initial transaction sync off the peer handshake path

  • Run the initial transaction sync in a goroutine tracked by pm.wg, so the peer enters its message loop immediately and a full Pending scan stays off the handshake path even when the pool is healthy.
  • Since the sync now runs concurrently with the peer message loop:
    • Filter the pending set against the peer's known transactions, so transactions received from the peer after registration are not echoed back to it.
    • Bail out before the scan, and abandon the sync after the scan, if the peer was dropped or the node began shutting down.
    • Also select on p.term when handing the batch to the txsync loop, so a dropped peer never parks the sync goroutine.
  • Stop() waits for the sync goroutine via pm.wg, so a pinned pool lock can still delay shutdown — same as before, when handle() itself was parked in Pending.

Commit 2 — feat(eth): report stalled initial transaction syncs

  • Moving the sync off the handshake path keeps peers usable when the pool is contended, but it also makes a wedged pool silent: peers look healthy while their tx sync never completes, and nothing in the logs points at the pool anymore.
  • Add txSyncStallWatch, a per-peer watchdog goroutine that reports at warn level once the sync is still pending after txSyncStatusLogCycle (1 minute, much shorter than syncStatusLogCycle), then re-reports every txSyncStallRepeatCycle (10 minutes) for as long as it remains pending — so a permanently wedged pool keeps warning for already connected peers instead of going silent after the first report.
  • A sync that finished in time, a peer that was dropped, or a node that began shutting down is not reported. The warning logs the actual elapsed duration, plus the lock-free txpool/pending and txpool/queued gauges (read atomically, so the diagnostic itself can never be blocked by the contended pool it reports on).

Notes

  • Concurrency: p.knownTxs is a mapset.NewSet from golang-set/v2 v2.7.0, whose operations are individually locked, so the concurrent reader added here is safe. Compound sequences such as Cardinality-check-then-Pop-then-Add remain non-atomic across calls, which can only cause benign over/under-marking, not a crash.
  • The stall watchdog is an XDC-specific enhancement: upstream go-ethereum has no equivalent, so there is no upstream pattern to follow here.

Types of changes

What types of changes does your code introduce to XDC network?
Put an in the boxes that apply

  • build: Changes that affect the build system or external dependencies
  • ci: Changes to CI configuration files and scripts
  • chore: Changes that don't change source code or tests
  • docs: Documentation only changes
  • feat: A new feature
  • fix: A bug fix
  • perf: A code change that improves performance
  • refactor: A code change that neither fixes a bug nor adds a feature
  • revert: Revert something
  • style: Changes that do not affect the meaning of the code
  • test: Adding missing tests or correcting existing tests

Impacted Components

Which parts of the codebase does this PR touch?
Put an in the boxes that apply

  • Consensus
  • Account
  • Network
  • Geth
  • Smart Contract
  • External components
  • Not sure (Please specify below)

Checklist

Put an in the boxes once you have confirmed below actions (or provide reasons on not doing so) that

  • This PR has sufficient test coverage (unit/integration test) OR I have provided reason in the PR description for not having test coverage
  • Tested on a private network from the genesis block and monitored the chain operating correctly for multiple epochs.
  • Provide an end-to-end test plan in the PR description on how to manually test it on the devnet/testnet.
  • Tested the backwards compatibility.
  • Tested with XDC nodes running this version co-exist with those running the previous version.
  • Relevant documentation has been updated as part of this PR
  • N/A

handle() called syncTransactions between registering a peer and entering its
message loop, so a peer could not read a single message until txpool.Pending
returned. While the pool lock was held the peer answered nothing and the
downloader eventually dropped it with err=timeout. A stack dump from a frozen
mainnet node showed 20 peer registrations parked in
makeProtocol -> handle -> syncTransactions -> Pending.

The freeze itself is fixed elsewhere: the pool lock was pinned by
promoteSpecialTx delivering an event while holding it, and by a peer
broadcaster that stopped draining its queue after a send error. This change
only removes the coupling, so peer registration no longer depends on the
transaction pool being responsive, and a full Pending scan stays off the
handshake path even when the pool is healthy.

Move the call into a goroutine tracked by pm.wg.

Since the sync now runs concurrently with the peer message loop, filter the
pending set against the peer's known transactions so transactions received
from the peer after registration are not echoed back to it, and abandon the
sync if shutdown started while the pool was being scanned.

Stop() waits for the sync goroutine via pm.wg, so a pinned pool lock can
still delay shutdown, same as before when handle() itself was parked in
Pending.

Concurrency note: p.knownTxs is a mapset.NewSet from golang-set/v2 v2.7.0,
whose operations are individually locked, so the concurrent reader added
here is safe. Compound sequences such as Cardinality check then Pop then Add
remain non-atomic across calls, which can only cause benign over/under-
marking, not a crash.

Add TestPeerServesRequestsWhileTxPoolIsBlocked, which serves a header request
while Pending is blocked, and split newTestProtocolManagerWithTxPool out of
newTestProtocolManager so the test can inject a blocking pool.
Running the initial transaction sync off the handshake path keeps peers usable
when the transaction pool is contended, but it also makes a wedged pool silent:
peers look healthy while their tx sync never completes. Nothing in the logs
would point at the pool anymore.

Watch the sync from its own goroutine and warn on the peer's logger if it is
still pending after txSyncStatusLogCycle, which is much shorter than
syncStatusLogCycle so a wedged pool shows up within a minute instead of tens of
minutes. A sync normally finishes in milliseconds, so only one that is still
pending a full cycle later is reported, which keeps healthy churn from
producing warnings. A sync that finished in time, a peer that was dropped, or a
node that began shutting down is not reported. The warning logs the actual
elapsed duration rather than the configured cycle.

Add TestTxSyncStallWatch covering the stall detection, including the finished,
still-running, dropped-peer, and shutting-down paths.

This watchdog is an XDC-specific enhancement: upstream go-ethereum has no
equivalent, so there is no upstream pattern to follow here.
@coderabbitai

coderabbitai Bot commented Aug 24, 2026

Copy link
Copy Markdown

Important

Review skipped

Auto reviews are disabled on base/target branches other than the default branch.

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro Plus

Run ID: 24ed095f-a5bf-46ac-9084-d5356a2fc459

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • 🔍 Trigger review

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.

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.

1 participant