Awaitable with CancellationToken vs coroutine cancel flag for deadline-bound scene-exit fetch
0 reputation · 24 Oct 2020, 18:05 UTC
In Unity 6, a scene-exit asset fetch must stop within a strict deadline to avoid touching destroyed objects. Two documented patterns exist: an Awaitable driven by a CancellationToken linked to a CancellationTokenSource with CancelAfter, or a coroutine that polls a deadline and sets a cancel flag before the owner calls StopCoroutine.
The Awaitable path uses the player loop and avoids manual state-machine bookkeeping, but cancelling the token only controls continuation resumption—it does not abort the underlying UnityWebRequest or AsyncOperation. The coroutine path requires the owner to retain the IEnumerator and call StopCoroutine on scene unload, yet it can explicitly call UnityWebRequest.Abort() in the same cancel block. UnityWebRequest.timeout is an integer-second value that surfaces as a network error rather than an OperationCanceledException, and CancellationTokenSource timer disposal plus overload availability vary by scripting backend and API compatibility level.
Which approach gives a single, verifiable cancel path that both stops awaiting and aborts the request without leaking a CancellationTokenSource timer or resuming after scene unload? How should a timeout that arrives as a network error be mapped into the cancellation model? Does the choice change when targeting IL2CPP with the .NET Standard 2.1 API compatibility level?