You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
[SharovBot] engineapi: fix flaky tests caused by TOCTOU port races (#20328)
## Problem
Tests in `execution/engineapi` were failing intermittently in the
`race-tests / tests-linux (ubuntu-latest, execution-other)` CI job:
- `TestEngineApiHighGasContractsFillBlock` → `listen tcp
127.0.0.1:46053: bind: address already in use`
- `TestEngineApiBlockGasOverflowSpillsToNextBlock` → `empty response
from JSON-RPC server`
Observed on commits a9db702 and acc9898 (2 out of 10 recent
commits).
**Root cause:** The test infrastructure used `freeport.NextFreePort()`
to pick a free port, then later called `net.Listen()` with that port.
This TOCTOU (time-of-check/time-of-use) race meant another concurrent
test could bind the same port in between, causing `address already in
use`. Separately, the server might not be ready when the first client
request arrived, causing `empty response from JSON-RPC server`.
## Fix
Pre-create `net.Listener` instances on port `:0` (kernel-assigned,
guaranteed free and already bound) before any server starts, then hand
the bound socket directly to `StartHTTPEndpoint`. The kernel guarantees
no other process can steal the port once it's bound.
**Changes:**
- `node/endpoints.go`: add optional `Listener` field to
`HttpEndpointConfig`; if set, skip the `net.Listen` call and use the
pre-bound socket
- `cmd/rpcdaemon/cli/httpcfg/http_cfg.go`: add `HttpListener` /
`AuthRpcListener` fields to `HttpCfg`
- `cmd/rpcdaemon/cli/config.go`: wire up pre-created listeners in both
the regular RPC and engine API listener paths
- `execution/engineapi/engineapitester/engine_api_tester.go`: replace
`freeport.NextFreePort()` with `net.Listen("tcp", "127.0.0.1:0")` for
the JSON-RPC and engine API ports
## Testing
- `go build ./...` ✅
- No `*_test.go` files modified ✅
- 5 consecutive `go test -race ./execution/engineapi/...` runs all pass
✅
CI job reference:
https://github.com/erigontech/erigon/actions/runs/23983504381/job/69951584262
---------
Co-authored-by: erigon-copilot[bot] <erigon-copilot[bot]@users.noreply.github.com>
Co-authored-by: Giulio Rebuffo <giulio.rebuffo@gmail.com>
Co-authored-by: Alexey Sharov <AskAlexSharov@gmail.com>
Co-authored-by: Matt Joiner <anacrolix@gmail.com>
Co-authored-by: yperbasis <andrey.ashikhmin@gmail.com>
Co-authored-by: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
Co-authored-by: Andrew Ashikhmin <34320705+yperbasis@users.noreply.github.com>
0 commit comments