HFresh.Shutdown closes downstream task queues before upstream workers drain
perfloop-oss/weaviate · Premature Release
https://perfloop.ai/t/oss/case_pxs500e48d
Verdict
VERIFIED · settled 2026-11-25 · pull request opened as weaviate/weaviate#14021
What happened: The check fails before this change and passes with it.
Hypothesis
During a normal graceful HFresh shutdown, the index closes its four background maintenance queues one after another instead of stopping and draining all workers first. When an earlier queue closes while a worker on a later queue is still running, that worker's follow-up enqueue fails with a closed-queue error. The queue worker classifies that error as permanent, discards the in-flight task, and deletes its on-disk chunk, permanently losing vector replicas after the worker already invalidated prior copies. How often this overlap occurs on production shard shutdowns or tenant unloads is unmeasured.
In adapters/repos/db/vector/hfresh/hfresh.go:306-341, HFresh.Shutdown calls h.taskQueue.Close(ctx) at line 320. TaskQueue.Close (adapters/repos/db/vector/hfresh/task_queue.go:167-189) closes analyzeQueue, splitQueue, reassignQueue, and mergeQueue sequentially. Each DiskQueue.Close (adapters/repos/db/queue/queue.go:234-269) sets q.closed = true and closes its writer before TaskQueue.Close moves on to pause or cancel later queues. Meanwhile, tasks on later queues enqueue into earlier queues: - ReassignTask (doReassign -> appendReassignReplicas -> append, adapters/repos/db/vector/hfresh/insert.go:290) increments the vector's version in VersionMap (invalidating all older copies across postings), appends replica 1, and calls EnqueueAnalyze on analyzeQueue. While splitQueue.Close waits for an active split worker, analyzeQueue is already closed while reassignQueue's batch context is still uncancelled. DiskQueue.Push returns `errors.New("queue closed")` (queue.go:289), which worker.go:103-107 classifies as permanent (`isPermanent` only retries transient, Canceled, and DeadlineExceeded errors). worker.go:94-96 logs `permanent errors, discarding the failed tasks` and calls batch.Done(), deleting the reassign chunk and leaving the vector with only 1 live replica instead of all configured replicas (default 4). - Sibling paths hit the same sequential-close ordering: MergeTask (doMerge, adapters/repos/db/vector/hfresh/merge.go:262) deletes the smaller centroid and calls EnqueueReassign on reassignQueue while reassignQueue is already closed before mergeQueue drains; SplitTask (doSplit, adapters/repos/db/vector/hfresh/split.go:192) calls EnqueueSplit on splitQueue after DiskQueue.Close sets splitQueue.closed = true before unregistering.
Provisional deterministic check: `go test -race -v -count=1 ./adapters/repos/db/vector/hfresh -run '^TestShutdownDoesNotCloseDownstreamQueuesWhileUpstreamWorkersRun$'` runs a normal `HFresh.Shutdown(context.Background())` while a split worker and a reassign worker are concurrently active, then reopens the index. On the accepted revision, the test fails deterministically in ~1.5s: vector 0 loses 3 of 4 replicas (`want 4, got 1 at v3`) and the worker logs `permanent errors, discarding the failed tasks: failed to push analyze operation to queue: queue closed`. With a minimal experimental edit in HFresh.Shutdown that pauses and waits for all four queues before calling h.taskQueue.Close(ctx), the same test passes under `-race`.
Case proof should verify across all four maintenance queues (including reassign->analyze, merge->reassign, and split->split) that clean HFresh.Shutdown never discards in-flight tasks with `queue closed` errors and preserves full replication and searchability on reopen, without introducing deadlocks or shutdown regressions.
Upstream check: open PRs touching adapters/repos/db/vector/hfresh/hfresh.go (PR #13994, parallel posting reads, adds only `postingStore.logger = logger` in New) and recent git history of hfresh.go do not change HFresh.Shutdown or TaskQueue.Close queue teardown ordering.
Change to test: Pause and drain all four HFresh background maintenance queues before marking any queue closed or closing any queue writer during HFresh.Shutdown, so active upstream workers (reassign, merge, and split) never fail with a permanent queue-closed error while pushing follow-up tasks.
Where it lives
Evidence
The check fails before this change and passes with it: `With split and reassign workers active, graceful HFresh.Shutdown preserves all four target-vector replicas, its search result, and every vector in the oversized split posting after reopen.`
The check fails before this change and passes with it: `While all four HFresh maintenance queues have active workers, closing TaskQueue leaves analyze-to-split, split-to-split, reassign-to-analyze and merge-to-reassign handoffs writable until those workers finish.`
The check fails before this change and passes with it: `When an HFresh maintenance worker remains active past a canceled or expired drain context, Shutdown and Drop report that context error without advancing to centroid shutdown or the final ID-sequence flush; a fresh-context retry finishes after the worker releases and concurrent teardown calls do not block.`
The check fails before this change and passes with it: `When a queue task outlives a canceled or expired Close or Drop context, the queue reports the context error and retains its registration and writable handles; a later successful close and reopen replay both the canceled batch and the later append.`
The check fails before this change and passes with it: `When a shard vector index reports a canceled or expired drain, shard Shutdown and Drop retain the same worker-owned buckets and usable metadata; ordinary non-context teardown errors still perform best-effort cleanup.`
The check fails before this change and passes with it: `When an asynchronous vector or geo queue still has an active task at the teardown deadline, shard Shutdown and Drop report that deadline and retain the same worker-owned bucket and usable metadata.`
The check fails before this change and passes with it: `For an eagerly registered shard whose vector-index drain reports cancellation or deadline expiry, shard shutdown completes the unloading gauge transition before returning, and a later drop removes its count exactly once from both legacy and labeled shard gauges.`
Checks: 7 of 7 passed. Verification: no defect found.
Timeline
2026-11-24· Case opened2026-11-25· PR opened