ConfigureAwait(this) shouldn't run the continuation if the owner is destroyed.
See original GitHub issueLove your library!
Say you have some code like this:
async void Start()
{
await Await.Seconds(5).ConfigureAwait(this);
transform.position = new Vector3(1, 2, 3); // will crash if object was destroyed
}
Currently, if the GameObject or Component is destroyed, the continuation runs immediately, which would cause a crash. I think it would be more intuitive if the continuation were cancelled instead. This is probably what the user wants most of the time and is consistent with how coroutines work.
Issue Analytics
- State:
- Created 4 years ago
- Comments:5 (3 by maintainers)
Top Results From Across the Web
Why ConfigureAwait(false) does not work while Task.Run() ...
ConfigureAwait(false) can only prevent deadlocks if it is used by every await in the transitive closure of that method and all methods it...
Read more >[Proposal] Forcing ConfigureAwait(false) on assembly level
ConfigureAwait(false) has no effect on completed tasks. If a task is already completed, then the execution continues on the same thread (that ...
Read more >Why I no longer use ConfigureAwait(false)
I've personally chosen to avoid ConfigureAwait(false) where possible, and control the execution context explicitly where it makes sense.
Read more >ConfigureAwait FAQ - .NET Blog
ConfigureAwait (false) involves a task that's already completed by the time it's awaited (which is actually incredibly common), then the ...
Read more >When to use ConfigureAwait(false) : r/dotnet
What ConfigureAwait(false) does is prevent the current execution context from being captured and passed to the continuation. If your method ...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found

Yes, I’m unable to come up with a logical situation where someone would tie it to an owner that doesn’t await on it or at least have some dependencies with the awaiting object so probably no point complicating things with an exception or exception option.
I’ve resolved the potential managed memory leak in 5430f351c9a06377d61018a946dcbb24c55418b4.
So my instinct is that once an object is destroyed, you rarely want to continue with the continuation. Your code is probably going to use some member of the owning object and crash; if the code had no dependencies on that object, it probably wouldn’t be in the object to begin with (or at least, you wouldn’t have called ConfigureAwait(this)). Of course, it’s hard to predict what use cases people might have.
I think the static Exception variable is safe here, but I don’t know enough about async/await to be confident.
One unrelated thing I noticed: in ContinuationProcessor, you don’t clean up the completed IContinuations. This could prevent any references held by the IContinuation from being garbage collected. You could probably add
Array.Clear(currentQueue, 0, count);at the end of Process().