Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

core/txpool: change lock in Pending method of legacy pool to read lock #32924

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
pythonberg1997 wants to merge 2 commits into ethereum:master
base: master
Choose a base branch
Loading
from pythonberg1997:txpool

Conversation

@pythonberg1997
Copy link

@pythonberg1997 pythonberg1997 commented Oct 16, 2025

This PR makes a small update to the Pending() method in the legacy pool. By changing the lock from exclusive to read-only, it aims to improve concurrency performance.

Copy link

#31523. I tried this before, but it seems to have some issues. FYI.

Copy link

#31523. I tried this before, but it seems to have some issues. FYI.

SortedMap seems to have complex concurrent semantics. It seems that it is dangerous in concurrent situations.

Pls check and pay more attenttion :D.

hero5512 reacted with thumbs up emoji

Copy link
Member

@will-2012 Thanks for pointing it out, you are right.

func TestSortedMapConcurrency(t *testing.T) {
	m := NewSortedMap()
	// Generate a list of transactions to insert
	key, _ := crypto.GenerateKey()
	txs := make(types.Transactions, 1000)
	for i := 0; i < len(txs); i++ {
		txs[i] = transaction(uint64(i), 0, key)
	}
	for i := 0; i < len(txs)/2; i++ {
		m.Put(txs[i])
	}
	
	var wg sync.WaitGroup
	wg.Add(8)
	for i := 0; i < 8; i++ {
		go func() {
			defer wg.Done()
			m.Flatten()
		}()
	}
	wg.Add(1)
	go func() {
		defer wg.Done()
		for i := len(txs) / 2; i < len(txs); i++ {
			m.Put(txs[i])
		}
	}()
	wg.Add(1)
	go func() {
		defer wg.Done()
		for i := 0; i < len(txs)/2; i++ {
			m.Forward(uint64(i))
		}
	}()
	wg.Wait()
}

I can reproduce the concurrent map read/write panic with this script.

pythonberg1997, joey0612, hero5512, and KyrinCode reacted with thumbs up emoji will-2012 reacted with heart emoji

Copy link
Author

@will-2012 Thanks for pointing it out, you are right.

func TestSortedMapConcurrency(t *testing.T) {
	m := NewSortedMap()
	// Generate a list of transactions to insert
	key, _ := crypto.GenerateKey()
	txs := make(types.Transactions, 1000)
	for i := 0; i < len(txs); i++ {
		txs[i] = transaction(uint64(i), 0, key)
	}
	for i := 0; i < len(txs)/2; i++ {
		m.Put(txs[i])
	}
	
	var wg sync.WaitGroup
	wg.Add(8)
	for i := 0; i < 8; i++ {
		go func() {
			defer wg.Done()
			m.Flatten()
		}()
	}
	wg.Add(1)
	go func() {
		defer wg.Done()
		for i := len(txs) / 2; i < len(txs); i++ {
			m.Put(txs[i])
		}
	}()
	wg.Add(1)
	go func() {
		defer wg.Done()
		for i := 0; i < len(txs)/2; i++ {
			m.Forward(uint64(i))
		}
	}()
	wg.Wait()
}

I can reproduce the concurrent map read/write panic with this script.

Cool. I see.

Copy link
Contributor

buddh0 commented Oct 17, 2025

I checked the code — both Content and Pending can safely use RLock.
The Put and Forward methods of SortedMap are already protected by Lock in their actual usage scenarios,
and Flatten is also guarded by RLock.
Therefore, the test case mentioned in the reply wouldn’t occur in practice.

@rjl493456442

KyrinCode reacted with thumbs up emoji will-2012 reacted with heart emoji

Copy link

I checked the code — both Content and Pending can safely use RLock. The Put and Forward methods of SortedMap are already protected by Lock in their actual usage scenarios, and Flatten is also guarded by RLock. Therefore, the test case mentioned in the reply wouldn’t occur in practice.

@rjl493456442

Got it, SortedMap is not thread-safe, and the upper caller is responsible for concurrent access.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@rjl493456442 rjl493456442 rjl493456442 approved these changes

Labels

None yet

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

AltStyle によって変換されたページ (->オリジナル) /