0

We detected a bug in our code, that we assume that is related to a race condition, but we don't understand why is happening. We had the following code

private static async Task<bool> IsMigrationPending(DependencyContainer dependencies)
{
    bool isMigrationPending = false;
    await WithCacheLock(MyRedisKey, dependencies, async () =>
    {
        isMigrationPending = await MyFunction();

        LogMessage($"isMigrationPending = {isMigrationPending}").wait();
        return isMigrationPending;
    });
    return isMigrationPending;
}

Despite of MyFunction() returns true the method IsMigrationPeding, most of the times we invoke it returned false. Chanigng the code line LogMessage($"isMigrationPending = {isMigrationPending}").wait() to

Await LogMessage($"isMigrationPending = {isMigrationPending}")

Then it fixed the problem, returning the expected value. For us doesn't make sense that changing a wait to await fix our problem. We expect that wait will block the execution until isMigrationPending value is available, or our assumption is incorrect?

rene
  • 41,474
  • 78
  • 114
  • 152
Marc Cals
  • 2,963
  • 4
  • 30
  • 48
  • `await` will block execution until `isMigrationPending` complete too. But it will block only "local" execution. – Fabio Sep 05 '17 at 09:08
  • what does ```WithCacheLock``` ? – tym32167 Sep 05 '17 at 09:10
  • @tym32167 It creates a Redis Key, if the Redis key exists throws an exception. – Marc Cals Sep 05 '17 at 09:11
  • @Fabio I don't understant what you meant by "local" execution? – Marc Cals Sep 05 '17 at 09:11
  • @MarcCals, I mean, that execution flow will be returned to the caller of your anonymous method on line `isMigrationPending = await MyFunction();`. And will be continued only when `Task` retuned by `MyFunction` will be complete – Fabio Sep 05 '17 at 09:14
  • @MarcCals ok, but how this function ```WithCacheLock``` will call passed lambda? – tym32167 Sep 05 '17 at 09:15

0 Answers0