Monogame ContentManager.LoadAsync and CancellationToken: Can Asset Loading be Cancelled?
0 reputation · 02 Sept 2024, 04:49 UTC
Goal
Determine whether Monogame’s asynchronous asset loading can be cooperatively cancelled or timed out without modifying the core API.
Constraints
ContentManager.LoadAsync returns a Task<T> with no CancellationToken overload, and the underlying load does not expose a cancellation handle. Manual wrappers with Task.WhenAny or CancellationTokenSource risk continuing the load after cancellation, potentially leaking memory or leaving assets partially loaded.
Uncertainty
Developers have observed that wrapping LoadAsync in a Task.Run and canceling the outer task does not abort the underlying load. It is unclear whether the current implementation can be made responsive to cancellation or if a new API layer is required.
Questions
1. Does the current ContentManager.LoadAsync internally support cancellation, and if so, how can it be exposed?
2. What is the safest pattern to implement a timeout for asset loading that guarantees the task completes or is aborted cleanly?
3. Would adding a CancellationToken overload to LoadAsync be backward compatible, or would it necessitate breaking changes for existing projects?