V3209. Unity Engine. Using await on 'Awaitable' object more than once can lead to exception or deadlock, as such objects are returned to the pool after being awaited.
The analyzer has detected several cases when the same 'UnityEngine.Awaitable' object is used with the 'await' operator. For optimisation purposes, 'Awaitable' objects are stored in an object pool. When the await call happens, the 'Awaitable' object returns to the pool. After this, an exception is thrown when 'await' is applied to the same object again. Deadlock is also possible in some cases.
Look at the synthetic example:
async Awaitable<bool> AwaitableFoo() { .... }
async Awaitable ExampleFoo()
{
Awaitable<bool> awaitable = AwaitableFoo();
if (await awaitable)
{
var result = await awaitable;
....
}
}
In the code above, an exception is thrown (or deadlock occurs) when the 'result' variable is initialized via a value obtained by the 'await' call of 'awaitable'. This happens because 'await' was already applied to 'awaitable' in the condition of the conditional statement.
To secure the code, avoid writing 'Awaitable' to the variable. Instead, you can reuse the value returned by the await call of 'AwaitableFoo()':
async Awaitable<bool> AwaitableFoo() { .... }
async Awaitable ExampleFoo()
{
bool value = await AwaitableFoo();
if (value)
{
var result = value;
....
}
}
Alternatively, you can repeat the direct await call of the 'AwaitableFoo' method where it is needed:
async Awaitable<bool> AwaitableFoo() { .... }
async Awaitable ExampleFoo()
{
if (await AwaitableFoo())
{
var result = await AwaitableFoo();
....
}
}
This solution is also correct because each time 'AwaitableFoo()' is called, a new 'Awaitable' object is returned.
Note that less obvious cases of the issue are possible. For example, when a repeated await-call of the 'Awaitable' value occurs within another method to which this value has been passed as an argument:
async Awaitable<bool> AwaitableFoo() { .... }
async Awaitable<Result> GetResult(Awaitable<bool> awaitable)
{
if (await awaitable){ .... } // <=
else { .... }
}
async Awaitable ExampleFoo()
{
Awaitable<bool> awaitable = AwaitableFoo();
if (await awaitable) // <=
{
....
}
var result = await GetResult(awaitable); // <=
}
In such cases, you can fix the issue using the same options as described above.