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
{{ message }}
Repository navigation
Commit 6814be7
Browse filesBrowse the repository at this point in the historyBrowse files
authored
Added async overload of ChangeToken.OnChange (#129624)
Fixes#69099.
Adds async (`Func<Task>`) overloads of `ChangeToken.OnChange`, so
callers can run asynchronous logic when a change token fires without
resorting to `async void` or blocking:
```csharp
public static IDisposable OnChange(Func<IChangeToken?> changeTokenProducer, Func<Task> changeTokenConsumer);
public static IDisposable OnChange<TState>(Func<IChangeToken?> changeTokenProducer, Func<TState, Task> changeTokenConsumer, TState state);
```
These match the shape approved in #69099.
## Behavior
The existing `ChangeTokenRegistration<TState>` is refactored into an
abstract base class holding the registration/disposal state machine,
with `SyncChangeTokenRegistration` and `AsyncChangeTokenRegistration`
subclasses. The synchronous overloads behave exactly as before.
For the async overloads:
- The consumer is invoked synchronously, so **synchronous** exceptions
from it are propagated to the code that triggers the change token, just
like the synchronous overloads.
- The change token is only re-registered once the returned `Task`
completes. Changes that occur while the consumer's task is still in
flight are coalesced into a single subsequent invocation.
- **Asynchronous** faults from the consumer's task cannot be propagated
without blocking, so they are left unobserved (observable only through
`TaskScheduler.UnobservedTaskException`). This is documented in the API
remarks.
- When the consumer completes synchronously, re-registration happens
without allocating an async state machine.
## Breaking change
This is a source (not binary) breaking change, as called out in the
proposal's Risk section: existing code that passes an `async` lambda to
`OnChange` previously bound to the `Action` overload (compiling to
`async void` / fire-and-forget). With these overloads present, such call
sites now bind to the `Func<Task>` overload and re-registration is
deferred until the returned task completes. The new behavior is
generally more correct. A throwing statement lambda can also become
ambiguous between `Action` and `Func<Task>` (CS0121); the two
pre-existing tests affected were updated with explicit `(Action)` casts.
## Tests
- Null-argument validation for all four overloads.
- Change firing (with and without state) for the async overloads.
- Disposal stops further consumer invocations.
- Re-registration is deferred until the consumer's task completes
(in-flight changes coalesce).
- Synchronous consumer exceptions propagate to the trigger; asynchronous
faults do not; the subscription survives both.
- Producer exceptions during initial registration propagate to the
caller; producer exceptions when the token fires propagate to the
trigger (all four overloads).
- Disposal during a running consumer (both from within the consumer and
from another thread) suppresses re-registration. The cross-thread case
requires real multithreading and is gated with `[ConditionalTheory(...
IsMultithreadingSupported)]`.
> [!NOTE]
> This PR description was generated by GitHub Copilot.
---------
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Tarek Mahmoud Sayed <10833894+tarekgh@users.noreply.github.com>
return new ChangeTokenRegistration<TState>(changeTokenProducer, changeTokenConsumer, state);
61
+
return new SyncChangeTokenRegistration<TState>(changeTokenProducer, changeTokenConsumer, state);
53
62
}
54
63
55
-
private sealed class ChangeTokenRegistration<TState> : IDisposable
64
+
/// <summary>
65
+
/// Registers the <paramref name="changeTokenConsumer"/> function to be called whenever the token produced changes.
66
+
/// </summary>
67
+
/// <param name="changeTokenProducer">Produces the change token.</param>
68
+
/// <param name="changeTokenConsumer">Function called when the token changes. The token is only re-registered once the returned <see cref="Task"/> completes.</param>
69
+
/// <returns>An <see cref="IDisposable"/> that, when disposed, unregisters the consumer.</returns>
70
+
/// <remarks>
71
+
/// Exceptions from <paramref name="changeTokenProducer"/> are propagated to the caller of this method or to the code that triggers the change token.
72
+
/// Synchronous exceptions from <paramref name="changeTokenConsumer"/> are propagated to the code that triggers the change token.
73
+
/// Asynchronous exceptions from <paramref name="changeTokenConsumer"/> are left unobserved.
74
+
/// </remarks>
75
+
public static IDisposable OnChange(Func<IChangeToken?> changeTokenProducer, Func<Task> changeTokenConsumer)
return new AsyncChangeTokenRegistration<Func<Task>>(changeTokenProducer, static callback => callback(), changeTokenConsumer);
87
+
}
88
+
89
+
/// <summary>
90
+
/// Registers the <paramref name="changeTokenConsumer"/> function to be called whenever the token produced changes.
91
+
/// </summary>
92
+
/// <param name="changeTokenProducer">Produces the change token.</param>
93
+
/// <param name="changeTokenConsumer">Function called when the token changes. The token is only re-registered once the returned <see cref="Task"/> completes.</param>
94
+
/// <param name="state">state for the consumer.</param>
95
+
/// <returns>An <see cref="IDisposable"/> that, when disposed, unregisters the consumer.</returns>
96
+
/// <remarks>
97
+
/// Exceptions from <paramref name="changeTokenProducer"/> are propagated to the caller of this method or to the code that triggers the change token.
98
+
/// Synchronous exceptions from <paramref name="changeTokenConsumer"/> are propagated to the code that triggers the change token.
99
+
/// Asynchronous exceptions from <paramref name="changeTokenConsumer"/> are left unobserved.
100
+
/// </remarks>
101
+
public static IDisposable OnChange<TState>(Func<IChangeToken?> changeTokenProducer, Func<TState, Task> changeTokenConsumer, TState state)
0 commit comments