Skip to content

Commit

Permalink
fix potential deadlock of pub/sub module
Browse files Browse the repository at this point in the history
  • Loading branch information
unclezoro committed Dec 30, 2020
1 parent f6ec491 commit 3d3f969
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 7 deletions.
10 changes: 3 additions & 7 deletions eth/filters/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,20 +107,16 @@ func (api *PublicFilterAPI) NewPendingTransactionFilter() rpc.ID {
pendingTxs = make(chan []common.Hash)
pendingTxSub = api.events.SubscribePendingTxs(pendingTxs)
)

f := &filter{typ: PendingTransactionsSubscription, deadline: time.NewTimer(deadline), hashes: make([]common.Hash, 0), s: pendingTxSub}
api.filtersMu.Lock()
api.filters[pendingTxSub.ID] = &filter{typ: PendingTransactionsSubscription, deadline: time.NewTimer(deadline), hashes: make([]common.Hash, 0), s: pendingTxSub}
api.filters[pendingTxSub.ID] = f
api.filtersMu.Unlock()

go func() {
for {
select {
case ph := <-pendingTxs:
api.filtersMu.Lock()
if f, found := api.filters[pendingTxSub.ID]; found {
f.hashes = append(f.hashes, ph...)
}
api.filtersMu.Unlock()
f.hashes = append(f.hashes, ph...)
case <-pendingTxSub.Err():
api.filtersMu.Lock()
delete(api.filters, pendingTxSub.ID)
Expand Down
11 changes: 11 additions & 0 deletions eth/filters/filter_system.go
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,17 @@ func (sub *Subscription) Unsubscribe() {
// this ensures that the manager won't use the event channel which
// will probably be closed by the client asap after this method returns.
<-sub.Err()

drainLoop:
for {
select {
case <-sub.f.logs:
case <-sub.f.hashes:
case <-sub.f.headers:
default:
break drainLoop
}
}
})
}

Expand Down

0 comments on commit 3d3f969

Please sign in to comment.