Skip to content
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

fix: adopt install uninstall chan to resolve deadlock on subscription #81

Merged
merged 4 commits into from
Oct 24, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 26 additions & 7 deletions jsonrpc/namespaces/eth/filters/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,10 @@
filters map[rpc.ID]*filter
subscriptions map[rpc.ID]*subscription

// Channels for subscription managements
install chan *subscription // install filter for event notification
uninstall chan *subscription // remove filter for event notification

// channels for block and log events
blockChan chan *coretypes.Header
logsChan chan []*coretypes.Log
Expand All @@ -67,14 +71,17 @@

logger: logger,

install: make(chan *subscription),
uninstall: make(chan *subscription),

Check warning on line 76 in jsonrpc/namespaces/eth/filters/api.go

View check run for this annotation

Codecov / codecov/patch

jsonrpc/namespaces/eth/filters/api.go#L74-L76

Added lines #L74 - L76 were not covered by tests
filters: make(map[rpc.ID]*filter),
subscriptions: make(map[rpc.ID]*subscription),
}

go api.clearUnusedFilters()

api.blockChan, api.logsChan, api.pendingChan = app.EVMIndexer().Subscribe()
go api.subscribeEvents()
go api.eventLoop()

Check warning on line 84 in jsonrpc/namespaces/eth/filters/api.go

View check run for this annotation

Codecov / codecov/patch

jsonrpc/namespaces/eth/filters/api.go#L84

Added line #L84 was not covered by tests

return api
}
Expand All @@ -95,7 +102,7 @@
}
}

func (api *FilterAPI) subscribeEvents() {
func (api *FilterAPI) eventLoop() {

Check warning on line 105 in jsonrpc/namespaces/eth/filters/api.go

View check run for this annotation

Codecov / codecov/patch

jsonrpc/namespaces/eth/filters/api.go#L105

Added line #L105 was not covered by tests
for {
select {
case block := <-api.blockChan:
Expand All @@ -105,12 +112,13 @@
f.hashes = append(f.hashes, block.Hash())
}
}
api.filtersMut.Unlock()

Check warning on line 116 in jsonrpc/namespaces/eth/filters/api.go

View check run for this annotation

Codecov / codecov/patch

jsonrpc/namespaces/eth/filters/api.go#L115-L116

Added lines #L115 - L116 were not covered by tests
for _, s := range api.subscriptions {
if s.ty == ethfilters.BlocksSubscription {
s.headerChan <- block
}
}
api.filtersMut.Unlock()
case logs := <-api.logsChan:
if len(logs) == 0 {
continue
Expand All @@ -125,6 +133,8 @@
}
}
}
api.filtersMut.Unlock()

Check warning on line 137 in jsonrpc/namespaces/eth/filters/api.go

View check run for this annotation

Codecov / codecov/patch

jsonrpc/namespaces/eth/filters/api.go#L136-L137

Added lines #L136 - L137 were not covered by tests
for _, s := range api.subscriptions {
if s.ty == ethfilters.LogsSubscription {
logs := filterLogs(logs, s.crit.FromBlock, s.crit.ToBlock, s.crit.Addresses, s.crit.Topics)
Expand All @@ -133,7 +143,6 @@
}
}
}
api.filtersMut.Unlock()
case tx := <-api.pendingChan:
api.filtersMut.Lock()
for _, f := range api.filters {
Expand All @@ -145,6 +154,8 @@
}
}
}
api.filtersMut.Unlock()

Check warning on line 158 in jsonrpc/namespaces/eth/filters/api.go

View check run for this annotation

Codecov / codecov/patch

jsonrpc/namespaces/eth/filters/api.go#L157-L158

Added lines #L157 - L158 were not covered by tests
for _, s := range api.subscriptions {
if s.ty == ethfilters.PendingTransactionsSubscription {
if s.fullTx {
Expand All @@ -154,7 +165,13 @@
}
}
}
api.filtersMut.Unlock()
// subscription managements
case s := <-api.install:
api.subscriptions[s.id] = s
close(s.installed)
case s := <-api.uninstall:
delete(api.subscriptions, s.id)
close(s.err)

Check warning on line 174 in jsonrpc/namespaces/eth/filters/api.go

View check run for this annotation

Codecov / codecov/patch

jsonrpc/namespaces/eth/filters/api.go#L169-L174

Added lines #L169 - L174 were not covered by tests
}
}
}
Expand Down Expand Up @@ -285,7 +302,9 @@
func (api *FilterAPI) UninstallFilter(id rpc.ID) bool {
api.filtersMut.Lock()
_, found := api.filters[id]
delete(api.filters, id)
if found {
delete(api.filters, id)
}

Check warning on line 307 in jsonrpc/namespaces/eth/filters/api.go

View check run for this annotation

Codecov / codecov/patch

jsonrpc/namespaces/eth/filters/api.go#L305-L307

Added lines #L305 - L307 were not covered by tests
beer-1 marked this conversation as resolved.
Show resolved Hide resolved
api.filtersMut.Unlock()
return found
}
Expand All @@ -295,7 +314,7 @@
func (api *FilterAPI) GetFilterLogs(ctx context.Context, id rpc.ID) ([]*coretypes.Log, error) {
api.filtersMut.Lock()
f, found := api.filters[id]
api.filtersMut.Lock()
api.filtersMut.Unlock()

Check warning on line 317 in jsonrpc/namespaces/eth/filters/api.go

View check run for this annotation

Codecov / codecov/patch

jsonrpc/namespaces/eth/filters/api.go#L317

Added line #L317 was not covered by tests

if !found || f.ty != ethfilters.LogsSubscription {
return nil, errFilterNotFound
Expand Down
70 changes: 54 additions & 16 deletions jsonrpc/namespaces/eth/filters/subscriptions.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import (
"context"
"sync"

"github.com/ethereum/go-ethereum/common"
coretypes "github.com/ethereum/go-ethereum/core/types"
Expand All @@ -12,6 +13,7 @@
)

type subscription struct {
id rpc.ID
ty ethfilters.Type
crit ethfilters.FilterCriteria
fullTx bool
Expand All @@ -20,6 +22,11 @@
logsChan chan []*coretypes.Log
txChan chan *rpctypes.RPCTransaction
hashChan chan common.Hash

// Channels to signal the subscription is installed or uninstalled
installed chan struct{} // closed when the subscription is installed
err chan error // closed when the subscription is uninstalled
unsubOnce sync.Once
}

// NewHeads send a notification each time a new (header) block is appended to the chain.
Expand All @@ -35,15 +42,19 @@
)

id := rpc.NewID()
api.filtersMut.Lock()
api.subscriptions[id] = &subscription{
s := &subscription{
id: id,

Check warning on line 46 in jsonrpc/namespaces/eth/filters/subscriptions.go

View check run for this annotation

Codecov / codecov/patch

jsonrpc/namespaces/eth/filters/subscriptions.go#L45-L46

Added lines #L45 - L46 were not covered by tests
ty: ethfilters.BlocksSubscription,
headerChan: headerChan,

installed: make(chan struct{}),
err: make(chan error),

Check warning on line 51 in jsonrpc/namespaces/eth/filters/subscriptions.go

View check run for this annotation

Codecov / codecov/patch

jsonrpc/namespaces/eth/filters/subscriptions.go#L49-L51

Added lines #L49 - L51 were not covered by tests
}
api.filtersMut.Unlock()
api.install <- s
<-s.installed

Check warning on line 54 in jsonrpc/namespaces/eth/filters/subscriptions.go

View check run for this annotation

Codecov / codecov/patch

jsonrpc/namespaces/eth/filters/subscriptions.go#L53-L54

Added lines #L53 - L54 were not covered by tests

go func() {
defer api.clearSubscription(id)
defer api.clearSubscription(s)

Check warning on line 57 in jsonrpc/namespaces/eth/filters/subscriptions.go

View check run for this annotation

Codecov / codecov/patch

jsonrpc/namespaces/eth/filters/subscriptions.go#L57

Added line #L57 was not covered by tests

for {
select {
Expand Down Expand Up @@ -93,17 +104,21 @@
}

id := rpc.NewID()
api.filtersMut.Lock()
api.subscriptions[id] = &subscription{
s := &subscription{
id: id,

Check warning on line 108 in jsonrpc/namespaces/eth/filters/subscriptions.go

View check run for this annotation

Codecov / codecov/patch

jsonrpc/namespaces/eth/filters/subscriptions.go#L107-L108

Added lines #L107 - L108 were not covered by tests
ty: ethfilters.LogsSubscription,
crit: crit,

logsChan: logsChan,

installed: make(chan struct{}),
err: make(chan error),

Check warning on line 115 in jsonrpc/namespaces/eth/filters/subscriptions.go

View check run for this annotation

Codecov / codecov/patch

jsonrpc/namespaces/eth/filters/subscriptions.go#L113-L115

Added lines #L113 - L115 were not covered by tests
}
api.filtersMut.Unlock()
api.install <- s
<-s.installed

Check warning on line 118 in jsonrpc/namespaces/eth/filters/subscriptions.go

View check run for this annotation

Codecov / codecov/patch

jsonrpc/namespaces/eth/filters/subscriptions.go#L117-L118

Added lines #L117 - L118 were not covered by tests

go func() {
defer api.clearSubscription(id)
defer api.clearSubscription(s)

Check warning on line 121 in jsonrpc/namespaces/eth/filters/subscriptions.go

View check run for this annotation

Codecov / codecov/patch

jsonrpc/namespaces/eth/filters/subscriptions.go#L121

Added line #L121 was not covered by tests
for {
select {
case logs := <-logsChan:
Expand Down Expand Up @@ -136,17 +151,21 @@
)

id := rpc.NewID()
api.filtersMut.Lock()
api.subscriptions[id] = &subscription{
s := &subscription{
id: id,

Check warning on line 155 in jsonrpc/namespaces/eth/filters/subscriptions.go

View check run for this annotation

Codecov / codecov/patch

jsonrpc/namespaces/eth/filters/subscriptions.go#L154-L155

Added lines #L154 - L155 were not covered by tests
ty: ethfilters.PendingTransactionsSubscription,
fullTx: fullTx != nil && *fullTx,
txChan: txChan,
hashChan: hashChan,

installed: make(chan struct{}),
err: make(chan error),

Check warning on line 162 in jsonrpc/namespaces/eth/filters/subscriptions.go

View check run for this annotation

Codecov / codecov/patch

jsonrpc/namespaces/eth/filters/subscriptions.go#L160-L162

Added lines #L160 - L162 were not covered by tests
}
api.filtersMut.Unlock()
api.install <- s
<-s.installed

Check warning on line 165 in jsonrpc/namespaces/eth/filters/subscriptions.go

View check run for this annotation

Codecov / codecov/patch

jsonrpc/namespaces/eth/filters/subscriptions.go#L164-L165

Added lines #L164 - L165 were not covered by tests

go func() {
defer api.clearSubscription(id)
defer api.clearSubscription(s)

Check warning on line 168 in jsonrpc/namespaces/eth/filters/subscriptions.go

View check run for this annotation

Codecov / codecov/patch

jsonrpc/namespaces/eth/filters/subscriptions.go#L168

Added line #L168 was not covered by tests

for {
select {
Expand All @@ -163,8 +182,27 @@
return rpcSub, nil
}

func (api *FilterAPI) clearSubscription(id rpc.ID) {
api.filtersMut.Lock()
delete(api.subscriptions, id)
api.filtersMut.Unlock()
func (api *FilterAPI) clearSubscription(s *subscription) {
s.unsubOnce.Do(func() {
uninstallLoop:
for {
// write uninstall request and consume logs/hashes. This prevents
// the eventLoop broadcast method to deadlock when writing to the
// filter event channel while the subscription loop is waiting for
// this method to return (and thus not reading these events).
select {
case api.uninstall <- s:
break uninstallLoop
case <-s.logsChan:
case <-s.txChan:
case <-s.hashChan:
case <-s.headerChan:

Check warning on line 199 in jsonrpc/namespaces/eth/filters/subscriptions.go

View check run for this annotation

Codecov / codecov/patch

jsonrpc/namespaces/eth/filters/subscriptions.go#L185-L199

Added lines #L185 - L199 were not covered by tests
}
}

// wait for filter to be uninstalled in work loop before returning
// this ensures that the manager won't use the event channel which
// will probably be closed by the client asap after this method returns.
<-s.err

Check warning on line 206 in jsonrpc/namespaces/eth/filters/subscriptions.go

View check run for this annotation

Codecov / codecov/patch

jsonrpc/namespaces/eth/filters/subscriptions.go#L206

Added line #L206 was not covered by tests
beer-1 marked this conversation as resolved.
Show resolved Hide resolved
})
}
Loading