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 'await' is used, the 'Awaitable' object returns to the pool. An exception is thrown when 'await' is applied to the same object reference 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 the value obtained when using 'await' on '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 a variable. Instead, you can reuse the value returned by the 'AwaitableFoo()' call with 'await':
async Awaitable<bool> AwaitableFoo() { .... }
async Awaitable ExampleFoo()
{
bool value = await AwaitableFoo();
if (value)
{
var result = value;
....
}
}
Alternatively, you can repeat the direct call to the 'AwaitableFoo' method with 'await' 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 the repeated use of 'await' on 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.