Skip to content

Commit 3f3f8d0

Browse files
authored
Merge pull request #59 from binance-chain/issue_58
[R4R]fix potential deadlock of pub/sub module
2 parents 894b1e3 + 3d3f969 commit 3f3f8d0

File tree

2 files changed

+14
-7
lines changed

2 files changed

+14
-7
lines changed

eth/filters/api.go

+3-7
Original file line numberDiff line numberDiff line change
@@ -107,20 +107,16 @@ func (api *PublicFilterAPI) NewPendingTransactionFilter() rpc.ID {
107107
pendingTxs = make(chan []common.Hash)
108108
pendingTxSub = api.events.SubscribePendingTxs(pendingTxs)
109109
)
110-
110+
f := &filter{typ: PendingTransactionsSubscription, deadline: time.NewTimer(deadline), hashes: make([]common.Hash, 0), s: pendingTxSub}
111111
api.filtersMu.Lock()
112-
api.filters[pendingTxSub.ID] = &filter{typ: PendingTransactionsSubscription, deadline: time.NewTimer(deadline), hashes: make([]common.Hash, 0), s: pendingTxSub}
112+
api.filters[pendingTxSub.ID] = f
113113
api.filtersMu.Unlock()
114114

115115
go func() {
116116
for {
117117
select {
118118
case ph := <-pendingTxs:
119-
api.filtersMu.Lock()
120-
if f, found := api.filters[pendingTxSub.ID]; found {
121-
f.hashes = append(f.hashes, ph...)
122-
}
123-
api.filtersMu.Unlock()
119+
f.hashes = append(f.hashes, ph...)
124120
case <-pendingTxSub.Err():
125121
api.filtersMu.Lock()
126122
delete(api.filters, pendingTxSub.ID)

eth/filters/filter_system.go

+11
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,17 @@ func (sub *Subscription) Unsubscribe() {
174174
// this ensures that the manager won't use the event channel which
175175
// will probably be closed by the client asap after this method returns.
176176
<-sub.Err()
177+
178+
drainLoop:
179+
for {
180+
select {
181+
case <-sub.f.logs:
182+
case <-sub.f.hashes:
183+
case <-sub.f.headers:
184+
default:
185+
break drainLoop
186+
}
187+
}
177188
})
178189
}
179190

0 commit comments

Comments
 (0)