Skip to content

Commit 2198ea7

Browse files
authored
Remove filters synchronously in eth_uninstallFilter (#4381)
The EVM RPC parity test `eth_newBlockFilter uninstalls once (true), then reports gone (false)` fails intermittently because `eth_uninstallFilter` does not remove the filter before it returns. `UninstallFilter` only checks that the filter exists, queues its ID on `toDelete`, and returns true. The cleanup loop deletes it later. A second `eth_uninstallFilter` sent right after the first can still find the filter and also return true, where geth returns false. `UninstallFilter` now deletes the filter under the write lock and returns whether it existed. This removes the `toDelete` channel and its case in the cleanup loop; timeout-based expiry is unchanged. A new unit test calls `UninstallFilter` twice on a block filter and checks the results are true then false. It failed 20 out of 20 runs before this change. Flaked in: https://github.com/sei-protocol/sei-chain/actions/runs/36590599736/job/109484456467
1 parent 2740979 commit 2198ea7

2 files changed

Lines changed: 23 additions & 33 deletions

File tree

‎evmrpc/filter.go‎

Lines changed: 10 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -269,7 +269,6 @@ type FilterAPI struct {
269269
tmClient client.LocalClient
270270
filtersMu sync.RWMutex
271271
filters map[ethrpc.ID]filter
272-
toDelete chan ethrpc.ID
273272
filterConfig *FilterConfig
274273
logFetcher *LogFetcher
275274
connectionType ConnectionType
@@ -345,7 +344,6 @@ func NewFilterAPI(
345344
tmClient: tmClient,
346345
filtersMu: sync.RWMutex{},
347346
filters: filters,
348-
toDelete: make(chan ethrpc.ID, 1000),
349347
filterConfig: filterConfig,
350348
logFetcher: logFetcher,
351349
connectionType: connectionType,
@@ -415,7 +413,6 @@ func (a *FilterAPI) takeBlockHashes(filterID ethrpc.ID) ([]common.Hash, error) {
415413
return hashes, nil
416414
}
417415

418-
// Unified cleanup loop that handles both timeout and manual deletion
419416
func (a *FilterAPI) cleanupLoop(timeout time.Duration) {
420417
ticker := time.NewTicker(timeout / 2) // Check more frequently than timeout
421418
defer func() {
@@ -430,9 +427,6 @@ func (a *FilterAPI) cleanupLoop(timeout time.Duration) {
430427
case <-ticker.C:
431428
// Clean up expired filters
432429
a.cleanupExpiredFilters(timeout)
433-
case filterID := <-a.toDelete:
434-
// Handle manual filter deletion
435-
a.removeFilter(filterID)
436430
}
437431
}
438432
}
@@ -465,16 +459,19 @@ func (a *FilterAPI) cleanupExpiredFilters(timeout time.Duration) {
465459
}
466460
}
467461

468-
func (a *FilterAPI) removeFilter(filterID ethrpc.ID) {
462+
func (a *FilterAPI) removeFilter(filterID ethrpc.ID) bool {
469463
a.filtersMu.Lock()
470464
defer a.filtersMu.Unlock()
471465

472-
if filter, exists := a.filters[filterID]; exists {
473-
delete(a.filters, filterID)
474-
if filter.cancelFunc != nil {
475-
filter.cancelFunc()
476-
}
466+
filter, exists := a.filters[filterID]
467+
if !exists {
468+
return false
469+
}
470+
delete(a.filters, filterID)
471+
if filter.cancelFunc != nil {
472+
filter.cancelFunc()
477473
}
474+
return true
478475
}
479476

480477
func (a *FilterAPI) updateFilterAccess(filterID ethrpc.ID) {
@@ -782,25 +779,7 @@ func (a *FilterAPI) UninstallFilter(
782779
defer func() {
783780
recordMetrics(ctx, fmt.Sprintf("%s_uninstallFilter", a.namespace), a.connectionType, startTime)
784781
}()
785-
786-
// Check if filter exists
787-
a.filtersMu.RLock()
788-
_, found := a.filters[filterID]
789-
a.filtersMu.RUnlock()
790-
791-
if !found {
792-
return false
793-
}
794-
795-
// Queue for deletion in cleanup loop to avoid race conditions
796-
select {
797-
case a.toDelete <- filterID:
798-
return true
799-
default:
800-
// Channel is full, fall back to direct deletion
801-
a.removeFilter(filterID)
802-
return true
803-
}
782+
return a.removeFilter(filterID)
804783
}
805784

806785
// shutdown method for graceful shutdown
@@ -816,8 +795,6 @@ func (a *FilterAPI) shutdown() {
816795
}
817796
a.filters = make(map[ethrpc.ID]filter)
818797
a.filtersMu.Unlock()
819-
820-
close(a.toDelete)
821798
}
822799

823800
type LogFetcher struct {

‎evmrpc/filter_block_notifier_test.go‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,3 +175,16 @@ func TestFilterAPI_NewBlockFilterIgnoresBlocksBeforeCreate(t *testing.T) {
175175
require.NoError(t, err)
176176
require.Equal(t, []common.Hash{after}, got)
177177
}
178+
179+
func TestFilterAPI_UninstallFilterRemovesImmediately(t *testing.T) {
180+
api := newTestFilterAPI(t, NewBlockHeaderNotifier(4))
181+
ctx := context.Background()
182+
183+
id, err := api.NewBlockFilter(ctx)
184+
require.NoError(t, err)
185+
186+
require.True(t, api.UninstallFilter(ctx, id))
187+
require.False(t, api.UninstallFilter(ctx, id))
188+
_, err = api.GetFilterChanges(ctx, id)
189+
require.ErrorContains(t, err, "filter does not exist")
190+
}

0 commit comments

Comments
 (0)