Skip to content

[Bug]: DiskCache IOEntry wait ignores context cancellation during update #27358

Closed

Description

@XuPeng-SH

Branch Name

main

Commit ID

6f42b19508f31ef04ae2857172893f13d48afddf

Other Environment Information

  • Reproduction is source-level and does not depend on a particular deployment.
  • The same behavior was checked in the fileservice package with GOWORK=off go test -mod=readonly -count=3.
  • The affected path is used by CN fileservice disk-cache reads.

Actual Behavior

When an IOEntry disk-cache file is being updated, DiskCache.Read(ctx, ...) waits in waitUpdateComplete. That helper uses sync.Cond.Wait() in a loop but never checks ctx.Done().

After the read context is canceled, the IOEntry read remains blocked until the unrelated cache update releases the path. In the diagnostic reproduction, it was still blocked 150ms after cancellation and only returned after the update was released. Under a stalled disk-cache update, this can retain canceled request goroutines and their buffers for the entire update duration.

The same method has an asymmetric control path: full-file fallback uses waitUpdateCompleteFor, which checks both the context and a bounded timeout, and returned promptly in the control test.

Expected Behavior

A canceled DiskCache.Read should stop waiting for an in-progress IOEntry disk-cache update and return promptly, with the same cancellation/bounded-wait contract as the full-file fallback. The wait must not retain a canceled request indefinitely.

Steps to Reproduce

Add a temporary white-box test under pkg/fileservice:

cache, _ := NewDiskCache(context.Background(), t.TempDir(),
    fscache.ConstCapacity(1<<20), nil, false, nil, "")
defer cache.Close(context.Background())

path := cache.pathForIOEntry("foo", IOEntry{Offset: 0, Size: 1})
release := cache.startUpdate(path)

ctx, cancel := context.WithCancel(context.Background())
done := make(chan error, 1)
go func() {
    v := &IOVector{FilePath: "foo",
        Entries: []IOEntry{{Offset: 0, Size: 1}}}
    done <- cache.Read(ctx, v)
}()

// Wait until Read has entered waitUpdateComplete, then:
cancel()

select {
case <-done:
    // unexpected: the read returned before the update was released
case <-time.After(150 * time.Millisecond):
    // observed: the read is still blocked
}

release()
// Read returns only after this release.

Run:

GOWORK=off go test -mod=readonly -v -count=3 -timeout 60s ./pkg/fileservice -run '^TestCodexDiskCache(IOEntryWaitIgnoresCancellation|FullFileWaitHonorsCancellation)$'

Observed on all three runs:

  • IOEntry wait remained blocked after cancellation.
  • Releasing the update unblocked the read.
  • The full-file control returned within the 500ms control bound.

Relevant code:

  • pkg/fileservice/disk_cache.go:333 calls waitUpdateComplete(ctx, diskPath).
  • pkg/fileservice/disk_cache.go:669-677 waits on the condition variable without checking the context.
  • pkg/fileservice/disk_cache.go:679-710 implements the bounded, context-aware full-file wait.

Additional information

This is a deterministic white-box counterexample of an asymmetric cancellation contract. The issue becomes operationally significant when disk-cache update latency is high or an update fails to complete. A fix should preserve the condition-variable wakeup behavior while making the wait context-aware and should include cancellation, update-release, and stalled-update tests.

Activity

  1. added
    kind/bug
    on Aug 19, 2026
  2. XuPeng-SH commented on Aug 19, 2026

    @XuPeng-SH
    ContributorAuthor

    Additional white-box control on the same commit:

    DiskCache.Update(ctx, ...) has the same cancellation gap on the writer side. writeFile calls startUpdateWithCleanup, whose condition-variable loop also ignores the context. Holding the path with cache.startUpdate(path), starting an Update with a cancellable context, and canceling it left Update blocked for 150ms; releasing the held update then unblocked it. This reproduced 3/3 runs.

    The temporary diagnostic test now covers:

    • IOEntry Read wait: blocked after cancellation;
    • Update wait: blocked after cancellation;
    • full-file Read wait: returns within the 500ms control bound.

    The shared fix should therefore cover both waitUpdateComplete and startUpdateWithCleanup, or otherwise define and test a single context-aware update-path wait contract.

  3. XuPeng-SH commented on Aug 19, 2026

    @XuPeng-SH
    ContributorAuthor

    Impact clarification:

    • The affected IOEntry read path waits in DiskCache.waitUpdateComplete without observing the request context. If the concurrent cache update is slow, a canceled or timed-out SQL/RPC request can retain its execution goroutine/task until the update completes; if the filesystem stalls, the wait can be unbounded.
    • Under concurrent disk-cache p99 latency, repeated waiters can accumulate and amplify CN tail latency, goroutine/memory/queue pressure, and eventually availability risk.
    • This is not currently shown to directly corrupt data. The scope is requests contending on the same IOEntry/update path; the full-file fallback has a separate bounded context-aware wait path.
    • A second writer-side reproduction is recorded here: DiskCache.Update can also remain blocked after cancellation in startUpdateWithCleanup; the shared cancellation/waiting design should be reviewed.
  4. XuPeng-SH commented on Aug 19, 2026

    @XuPeng-SH
    ContributorAuthor

    Additional white-box reproduction: DiskCache.DeletePaths is also blocked by startUpdate after its context is canceled when the same full-file path is being updated; it returned only after the held update was released, reproduced 3/3. This is the same missing-context termination edge as the IOEntry read and writer paths, not a separate root cause.

  5. XuPeng-SH commented on Aug 19, 2026

    @XuPeng-SH
    ContributorAuthor

    High-depth cause analysis:\n\n1. DiskCache.Read tries the IOEntry cache path before the full-file fallback. The IOEntry branch calls waitUpdateComplete, whose condition-variable loop has no context or deadline.\n2. A writer marks the same path as updating before writeFile and clears it only after all write work and cleanup finish. writeFile includes io.CopyBuffer and f.Sync, neither of which provides a context cancellation edge for a blocked local disk operation.\n3. Therefore a slow or stuck disk write leaves the path marker true. Canceled readers, writers, and DeletePaths callers remain waiters until the physical update eventually releases the marker. The full-file fallback is different because waitUpdateCompleteFor polls context and timeout.\n4. High disk latency plus the ignored async flag in #27359 turns this into a fan-in point: many SQL/RPC requests can retain goroutines, task state, and buffers behind the same cache path. This is a Q2 termination failure with a direct tail-latency and resource-accumulation path.\n\nThe missing termination edge is not in the caller context; it must be added to the update-path wait itself, while preserving same-path exclusion and exactly-once release.

  6. added a commit that references this issue on Aug 21, 2026
    70cd802
  7. added 3 commits that reference this issue on Sep 17, 2026
    2f7ae34
    61f94b0
    1c17af6
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

kind/bugseverity/s-1

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions