Skip to content

fix(table): fix race condition in scanner - #445

Merged
zeroshade merged 1 commit into
apache:mainfrom
zeroshade:metrics-eval-race
Jun 5, 2025
Merged

zeroshade merged 1 commit into
apache:mainfrom
zeroshade:metrics-eval-race

Conversation

@zeroshade

Copy link
Copy Markdown
Member

fixes #443

Eliminates potential race condition in scanning caused by the metrics evaluator used for manifest pruning.

Since the stats are only used for a given run of Eval, we don't need to modify the bound version of the inclusiveMetricsEval object and can instead copy the type and expression (cheap) to populate for the call. This avoids the race condition without giving up performance (no locking needed).

@zeroshade
zeroshade requested review from Fokko and kevinjqliu May 28, 2025 18:37

@kevinjqliu kevinjqliu left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

@zeroshade
zeroshade merged commit d72437b into apache:main Jun 5, 2025
@zeroshade
zeroshade deleted the metrics-eval-race branch June 5, 2025 19:20
zeroshade pushed a commit that referenced this pull request Apr 29, 2026
)

## Problem

Two evaluator types share a receiver across concurrent goroutine calls,
causing data races detected by `-race`.

**Race 1 — `exprEvaluator.Eval`** (`visitors.go`)

`table.Scan.collectManifestEntries` fans out manifest opens via
`errgroup` (`scanner.go`), and each goroutine calls the bound
`*exprEvaluator.Eval` method value returned by `ExpressionEvaluator`.
`Eval` writes the per-call `StructLike` to the receiver's shared `st`
field (`e.st = st`). Because the `*exprEvaluator` is shared across the
goroutine pool, parallel `Eval` calls race on `e.st`.

**Race 2 — `manifestEvalVisitor.Eval`** (`table/evaluators.go`)

`newManifestEvaluator` returns a closure that captures a shared
`*manifestEvalVisitor`. When multiple goroutines call that closure
concurrently (e.g. the `errgroup` in `classifyFilesForFilteredDeletions`
and parallel manifest scans in `collectManifestEntries`), they race on
the `partitionFields` write in `Eval` vs. the reads inside
`VisitLess`/`VisitGreater`.

## Fix

Both fixes mirror the approach used in #445 (closes #443) for
`inclusiveMetricsEval`: instead of mutating the shared receiver, `Eval`
builds a per-call stack-local copy of the visitor and passes `&ev` to
`VisitExpr`. The captured receiver is read-only after construction; all
per-call mutable state lives in the local copy.

```go
// exprEvaluator
func (e *exprEvaluator) Eval(st StructLike) (bool, error) {
    ev := exprEvaluator{bound: e.bound, st: st}
    return VisitExpr(ev.bound, &ev)
}

// manifestEvalVisitor
func (m *manifestEvalVisitor) Eval(manifest iceberg.ManifestFile) (bool, error) {
    parts := manifest.Partitions()
    if len(parts) == 0 {
        return rowsMightMatch, nil
    }
    ev := manifestEvalVisitor{partitionFields: parts, partitionFilter: m.partitionFilter}
    return VisitExpr(ev.partitionFilter, &ev)
}
```

No API changes.

## Testing

- Adds `TestManifestEvalVisitorEvalRace` in
`table/evaluator_race_test.go`: 256 goroutines share one cached
evaluator closure and call it concurrently. Confirmed to trip `-race`
before the fix and pass after.
- Existing `TestKeyDefaultMapRaceCondition` (the only prior race test)
still passes.
- Full `go test -race ./table/ ./...` passes (pre-existing races in
miniohq-specific tests are unrelated).

## Related

- Sibling precedent: #445 (closes #443) — same pattern for
`inclusiveMetricsEval`.

---------

Co-authored-by: Varun Venkatesh <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Data race in inclusiveMetricsEval

2 participants