Skip to content

Cancelling a FileSystem/Temp/ConfigHome/DataHome persistence call mid-operation throws PersistenceProviderException instead of OperationCanceledException #121

Closed

Description

@matt-edmondson

What's wrong

FileSystemPersistenceProvider and TempPersistenceProvider wrap every operation in catch (Exception ex) { throw new PersistenceProviderException(...) }. That catch also captures the OperationCanceledException / TaskCanceledException thrown by the token-aware calls awaited inside the try:

  • FileSystemPersistenceProvider.StoreAsync: SerializeAsync (line 62) and WriteAllTextAsync (line 73), wrapped at line 82.
  • FileSystemPersistenceProvider.RetrieveAsync: ReadAllTextAsync (line 105) and DeserializeAsync (line 112), wrapped at line 114.
  • TempPersistenceProvider has the same structure, with catches at lines 79 and 111.
  • ConfigHome and DataHome delegate to FileSystemPersistenceProvider, so they inherit the problem.

ThrowIfCancellationRequested() runs before the try, so only a token that is already cancelled on entry produces a real OCE. If cancellation arrives during the operation, the caller gets a PersistenceProviderException whose InnerException is the TaskCanceledException.

The sibling InMemoryPersistenceProvider throws a plain OCE in the same situation. Standard .NET cancellation handling (catch (OperationCanceledException), Task.IsCanceled, ASP.NET request-abort handling, Parallel/Channel consumers) therefore sees a cancelled save as a storage failure. The result is error logs, retries, or a "save failed" message shown to the user.

Reproduction

The setup is FileSystemPersistenceProvider<string> over InMemoryFileSystemProvider, with a test ISerializationProvider whose TrySerialize writes the value and then calls cts.Cancel() to simulate the token firing mid-operation.

await p.StoreAsync("k", "value", cts.Token);
→ ktsu.Essentials.PersistenceProviderException inner=System.Threading.Tasks.TaskCanceledException:
  Failed to store object with key 'k' to file system

Expected: OperationCanceledException.

Suggested fix

Let cancellation pass through every catch-all in both providers. Either:

  • add catch (OperationCanceledException) { throw; } before the wrapping catch, or
  • use catch (Exception ex) when (ex is not OperationCanceledException).

Open PR #116 (for #66) rewrites the body of StoreAsync but keeps the catch-all. This fix should either rebase onto that PR or be folded into it.

Acceptance criteria

  • For FileSystem, Temp, ConfigHome and DataHome, a token cancelled during StoreAsync or RetrieveAsync produces an OperationCanceledException, not a PersistenceProviderException.
  • A contract test covers this using a serializer or file-system test double that cancels mid-operation.
  • Failures that are not cancellations are still wrapped in PersistenceProviderException.

Activity

  1. added
    bug
    on Oct 17, 2026
  2. matt-edmondson commented on Oct 17, 2026

    @matt-edmondson
    ContributorAuthor

    Triage


    Generated by Claude Code

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

bug

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions