Skip to content

Make PermanentStore.Stop wait for in-flight blocks, not just queue drain - #62

Open
damilolaedwards wants to merge 1 commit into
ethpandaops:masterfrom
damilolaedwards:fix/permanent-store-stop-waits-for-workers
Open

Make PermanentStore.Stop wait for in-flight blocks, not just queue drain#62
damilolaedwards wants to merge 1 commit into
ethpandaops:masterfrom
damilolaedwards:fix/permanent-store-stop-waits-for-workers

Conversation

@damilolaedwards

Copy link
Copy Markdown

Summary

  • Stop's readiness check watched the queue channel's length, which drops to zero the moment a worker receives a block off the channel, not when it actually finishes processing it. A caller that tore things down (closed the DB pool, exited the process) right after Stop returned could interrupt a block still mid-copy or mid-database-write.
  • Tracks outstanding work with a sync.WaitGroup instead: QueueBlock reserves a slot before a block is admitted to the queue, and the worker releases it only after that block is fully handled, success or failure. Stop now blocks on the WaitGroup rather than queue length, still bounded by the caller's context.
  • A mutex guards the stopped flag against the WaitGroup's Add/Wait sequence, so a QueueBlock call can't race Stop into calling Add after Wait has already returned.

Test plan

  • go build ./... and go vet ./...
  • New tests: Stop waits for a real in-flight Copy to finish before returning (verified against a delayed store wrapper), Stop still returns promptly when idle, Stop respects context cancellation, QueueBlock after Stop doesn't panic the WaitGroup
  • All new tests pass under -race across repeated runs
  • Confirmed the new tests fail against the pre-fix code (queue-length-based Stop returns immediately instead of waiting)
  • Full repo test suite not run locally: some existing test files in this package depend on Docker/testcontainers, unavailable in this environment

Stop's readiness check watched the queue channel's length, which drops
to zero the moment a worker receives a block, not when it finishes
processing it. A caller that tore things down right after Stop
returned could interrupt a block still mid-copy or mid-write.

Track outstanding work with a WaitGroup instead: QueueBlock reserves a
slot before a block is admitted, and each worker releases it only
after that block is fully processed, success or failure. Stop now
blocks on the WaitGroup, still bounded by the caller's context.
@damilolaedwards
damilolaedwards requested a review from Savid as a code owner July 27, 2026 13:44
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.

1 participant