Our website uses cookies to enhance your browsing experience.
Accept
to the top
>
>
>
V3209. Unity Engine. Using await on 'Aw…
menu mobile close menu
Analyzer diagnostics
General Analysis (C++)
General Analysis (C#)
General Analysis (Java)
Micro-Optimizations (C++)
Diagnosis of 64-bit errors (Viva64, C++)
Customer specific requests (C++)
MISRA errors
AUTOSAR errors
OWASP errors (C++)
OWASP errors (C#)
Problems related to code analyzer
Additional information
toggle menu Contents

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.

Dec 04 2024

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.

close form

Fill out the form in 2 simple steps below:

Your contact information:

Step 1
Congratulations! This is your promo code!

Desired license type:

Step 2
Team license
Enterprise license
** By clicking this button you agree to our Privacy Policy statement
close form
Request our prices
New License
License Renewal
--Select currency--
USD
EUR
* By clicking this button you agree to our Privacy Policy statement

close form
Free PVS‑Studio license for Microsoft MVP specialists
* By clicking this button you agree to our Privacy Policy statement

close form
To get the licence for your open-source project, please fill out this form
* By clicking this button you agree to our Privacy Policy statement

close form
I am interested to try it on the platforms:
* By clicking this button you agree to our Privacy Policy statement

close form
check circle
Message submitted.

Your message has been sent. We will email you at


If you do not see the email in your inbox, please check if it is filtered to one of the following folders:

  • Promotion
  • Updates
  • Spam