Skip to content

fix(core/txpool): stop delivering special tx events under the pool lock - #2519

Open
gzliudan wants to merge 1 commit into
XinFinOrg:dev-upgradefrom
gzliudan:fix-special-tx-feed-under-lock
Open

fix(core/txpool): stop delivering special tx events under the pool lock#2519
gzliudan wants to merge 1 commit into
XinFinOrg:dev-upgradefrom
gzliudan:fix-special-tx-feed-under-lock

Conversation

@gzliudan

Copy link
Copy Markdown
Collaborator

Proposed changes

Problem

A mainnet node froze for ~2h40m while RPC stayed responsive: no new blocks were imported. debug.stacks() showed the full failure chain:

  1. A peer lost its transaction broadcaster (broadcastTransactions exited while the peer stayed alive).
  2. eth/peer.go AsyncSendTransactions blocked forever on p.txBroadcast, which no longer had a reader.
  3. txBroadcastLoop stopped draining pm.txsCh and became a stalled txFeed subscriber.
  4. LegacyPool.promoteSpecialTx called pool.txFeed.Send while holding pool.mu, so Feed.Send pinned the pool write lock for hours.

Result: 131 goroutines piled up in LegacyPool.Add, 20 in Pending, runReorg never ran, and the node stopped importing blocks.

Fix

Route the special tx event through queueTxEvent, the same path every other transaction in add() uses. The event is delivered by runReorg after pool.mu is released, so a stalled subscriber can no longer pin the pool lock. This also aligns the XDPoS-specific path with the upstream geth design.

Behavior change

Remote adds (sync=false) now deliver the NewTxsEvent for special txs after the next reorg run instead of synchronously inside Add — identical to normal transactions. Local adds (sync=true) still deliver before Add returns. No consensus, RPC, or API surface changes.

Scope / follow-up

This fixes the pool-side amplifier only. The peer-side defect that starts the chain (eth/peer.go: broadcastTransactions can exit while the peer stays alive, leaving AsyncSendTransactions blocked forever) is tracked separately and needs its own fix. Until then, a stalled subscriber plus a full pm.txsCh can block the reorg goroutine, but no longer Add/Pending.

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

@coderabbitai

coderabbitai Bot commented Aug 12, 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: 1557389f-0223-45ad-873d-c5218e6ed96f

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.

@gzliudan
gzliudan force-pushed the fix-special-tx-feed-under-lock branch 5 times, most recently from d7614e7 to 18dd2d4 Compare August 16, 2026 15:35
@gzliudan
gzliudan requested a balanced review from Copilot August 16, 2026 16:14

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Pull request overview

Moves special-transaction event delivery outside the txpool lock to prevent stalled subscribers from freezing pool operations.

Changes:

  • Queues special transaction events through the reorg path.
  • Adds announcement limits and revised shutdown behavior.
  • Adds regression tests for stalled subscribers and reset handling.

Reviewed changes

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

File Description
core/txpool/legacypool/legacypool.go Revises event delivery, reorg scheduling, and shutdown handling.
core/txpool/legacypool/legacypool_test.go Adds regression tests for event delivery and queue behavior.

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment thread core/txpool/legacypool/legacypool.go Outdated
Comment on lines +1268 to +1269
queuedEvents = make(map[common.Address]*SortedMap)
queuedEventCount = 0
Comment thread core/txpool/legacypool/legacypool.go Outdated
Comment on lines +1311 to +1319
if queuedEventCount >= maxQueuedEvents && !tx.IsSpecialTransaction() {
// The set was created above only for this event; remove it
// again to keep the map free of entries holding no events.
if set.Len() == 0 {
delete(queuedEvents, addr)
}
announcementDropMeter.Mark(1)
log.Trace("Transaction event queue full, dropping announcement", "hash", tx.Hash())
continue
Comment thread core/txpool/legacypool/legacypool.go Outdated
Comment on lines +1372 to +1376
select {
case <-pool.reorgShutdownCh:
pool.mu.Unlock()
close(done)
return
Comment thread core/txpool/legacypool/legacypool.go Outdated
Comment on lines +1420 to +1421
for addr, set := range events {
set.Filter(func(tx *types.Transaction) bool { return staleEvent(addr, tx) })
Comment thread core/txpool/legacypool/legacypool.go Outdated
// that stopped draining (e.g. a peer whose transaction broadcaster died),
// and waiters such as Reset or a synchronous Add must not be pinned behind
// such a subscriber.
close(done)
Comment on lines +3970 to +3971
pool.queueTxEvent(specialTx)
<-pool.requestPromoteExecutables(newAccountSet(pool.signer))
@gzliudan
gzliudan force-pushed the fix-special-tx-feed-under-lock branch from 18dd2d4 to ff3fbb0 Compare August 16, 2026 16:44
promoteSpecialTx called txFeed.Send while holding pool.mu, so a subscriber that
stopped draining froze the whole pool. On mainnet a peer lost its transaction
broadcaster, AsyncSendTransactions blocked, txBroadcastLoop stopped draining
pm.txsCh, and the resulting Feed.Send pinned pool.mu for hours: 131 goroutines
piled up in Add, 20 in Pending, runReorg never ran and the node stopped
importing blocks while RPC stayed responsive.

Route the event through queueTxEvent like every other path in add(), so
runReorg delivers it after releasing the lock.

Add TestSpecialTxPromotionDoesNotBlockOnTxFeed, which adds a special tx while a
subscriber refuses to read and asserts both the add and a later pool read
complete.
@gzliudan
gzliudan force-pushed the fix-special-tx-feed-under-lock branch from ff3fbb0 to b70f81c Compare August 16, 2026 16:48
@gzliudan gzliudan added the WIP work in process label Aug 16, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

WIP work in process

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants