Commit 618af12
fix(txpool): fix data race that broadcasts a null transaction (#12162)
* fix(txpool): fix data race that broadcasts a null transaction
TxBroadcaster.BroadcastOnce locked on the _accumulatedTemporaryTxs
instance while TimerOnElapsed swapped that field by reference via
Interlocked.Exchange without taking the same lock. A monitor only
serialises sections that lock the same stable object, so the swap let
two threads hold monitors on two different ResettableList instances
while both Add()-ing to the same underlying List<T>. A concurrent Add
during a resize leaves a null hole in the list, which is later read
lazily through txs.Where(_gossipFilter) and dereferenced by
SpecDrivenTxGossipPolicy, throwing NullReferenceException in
CompositeTxGossipPolicy.ShouldGossipTransaction while gossiping to peers.
Use a dedicated, never-reassigned lock for both the append and the swap.
After the swap, BroadcastOnce only touches the new (empty) accumulator
while the timer exclusively owns the buffer being sent, so the two lists
are never mutated concurrently. The ResettableList reuse/swap design is
kept to avoid per-broadcast allocations during sync.
The pre-existing race surfaces as a fatal crash now only because
SpecDrivenTxGossipPolicy is the first gossip policy to dereference the
transaction; it was observed on gnosis+Flat sync where finalization-driven
background work shifts scheduling enough to hit the window.
Adds a concurrency regression test that drives BroadcastOnce against
repeated timer swaps and asserts no null reaches the peer (and that every
transaction is sent exactly once). The test fails reliably on the old
code and passes on the fix.
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
* refactor(txpool): address review feedback
- Remove the two comments @asdacap flagged as unnecessary (the XML doc on
_accumulatedTxsLock and the inline comment in NotifyPeers).
- Yield in the regression test's ticker loop so it no longer busy-spins a
core; the swap window is still hit reliably (test still fails 5/5 on the
pre-fix code, passes on the fix).
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
---------
Co-authored-by: Claude Opus 4.8 (1M context) <noreply@anthropic.com>1 parent 760ccb2 commit 618af12
2 files changed
Lines changed: 73 additions & 2 deletions
Lines changed: 67 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
2 | 2 | | |
3 | 3 | | |
4 | 4 | | |
| 5 | + | |
5 | 6 | | |
6 | 7 | | |
7 | 8 | | |
| |||
723 | 724 | | |
724 | 725 | | |
725 | 726 | | |
| 727 | + | |
| 728 | + | |
| 729 | + | |
| 730 | + | |
| 731 | + | |
| 732 | + | |
| 733 | + | |
| 734 | + | |
| 735 | + | |
| 736 | + | |
| 737 | + | |
| 738 | + | |
| 739 | + | |
| 740 | + | |
| 741 | + | |
| 742 | + | |
| 743 | + | |
| 744 | + | |
| 745 | + | |
| 746 | + | |
| 747 | + | |
| 748 | + | |
| 749 | + | |
| 750 | + | |
| 751 | + | |
| 752 | + | |
| 753 | + | |
| 754 | + | |
| 755 | + | |
| 756 | + | |
| 757 | + | |
| 758 | + | |
| 759 | + | |
| 760 | + | |
| 761 | + | |
| 762 | + | |
| 763 | + | |
| 764 | + | |
| 765 | + | |
| 766 | + | |
| 767 | + | |
| 768 | + | |
| 769 | + | |
| 770 | + | |
| 771 | + | |
| 772 | + | |
| 773 | + | |
| 774 | + | |
| 775 | + | |
| 776 | + | |
| 777 | + | |
| 778 | + | |
| 779 | + | |
| 780 | + | |
| 781 | + | |
| 782 | + | |
| 783 | + | |
| 784 | + | |
| 785 | + | |
| 786 | + | |
| 787 | + | |
| 788 | + | |
| 789 | + | |
| 790 | + | |
| 791 | + | |
| 792 | + | |
726 | 793 | | |
727 | 794 | | |
728 | 795 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
53 | 53 | | |
54 | 54 | | |
55 | 55 | | |
| 56 | + | |
56 | 57 | | |
57 | 58 | | |
58 | 59 | | |
| |||
131 | 132 | | |
132 | 133 | | |
133 | 134 | | |
134 | | - | |
| 135 | + | |
135 | 136 | | |
136 | 137 | | |
137 | 138 | | |
| |||
296 | 297 | | |
297 | 298 | | |
298 | 299 | | |
299 | | - | |
| 300 | + | |
| 301 | + | |
| 302 | + | |
| 303 | + | |
300 | 304 | | |
301 | 305 | | |
302 | 306 | | |
| |||
0 commit comments