Skip to content

Commit 5191a29

Browse files
fix(audience): flushAsync accepts CancellationToken, swallows ObjectDisposedException
- Add optional CancellationToken. Caller can cancel the gate-wait and the in-flight HTTP send (default is CancellationToken.None — no behaviour change for existing callers). - Catch ObjectDisposedException thrown when a concurrent Shutdown disposed the transport mid-flush. Previously, the exception propagated to the awaiter. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
1 parent 05c6b08 commit 5191a29

1 file changed

Lines changed: 10 additions & 3 deletions

File tree

src/Packages/Audience/Runtime/ImmutableAudience.cs

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -434,8 +434,9 @@ private static void SyncConsentToBackend(AudienceConfig config, ConsentLevel lev
434434
// Flush / Shutdown
435435
// -----------------------------------------------------------------
436436

437-
// Send pending events now.
438-
public static async Task FlushAsync()
437+
// Sends all pending events now. Respects cancellationToken for both
438+
// the gate wait and the HTTP send.
439+
public static async Task FlushAsync(CancellationToken cancellationToken = default)
439440
{
440441
if (!_initialized) return;
441442

@@ -451,16 +452,22 @@ public static async Task FlushAsync()
451452
// (timer SendBatch or a racing FlushAsync) holds the gate.
452453
while (Interlocked.CompareExchange(ref _sendInFlight, 1, 0) != 0)
453454
{
455+
cancellationToken.ThrowIfCancellationRequested();
454456
await Task.Yield();
455457
}
456458

457459
try
458460
{
459461
while (!transport.IsInBackoffWindow &&
460-
await transport.SendBatchAsync().ConfigureAwait(false))
462+
await transport.SendBatchAsync(cancellationToken).ConfigureAwait(false))
461463
{
462464
}
463465
}
466+
catch (ObjectDisposedException)
467+
{
468+
// Concurrent Shutdown disposed the transport. Exit silently —
469+
// caller is tearing down.
470+
}
464471
finally
465472
{
466473
Interlocked.Exchange(ref _sendInFlight, 0);

0 commit comments

Comments
 (0)