Skip to content

Commit 61a05ff

Browse files
committed
fix: Replace unreachable return in AsyncLockProvider with throw
The `return true` at the end of TryExecuteAsync was unreachable — the loop only exits when validLock is true, which always returns from inside the loop. If control ever did reach it (e.g. due to a future code change), the caller would incorrectly think the lock succeeded and the callback ran. Replace with throw InvalidOperationException.
1 parent b84937d commit 61a05ff

File tree

1 file changed

+4
-2
lines changed

1 file changed

+4
-2
lines changed

src/Imazen.Common/Concurrency/AsyncLockProvider.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ public async Task<bool> TryExecuteAsync(string key, int timeoutMs, CancellationT
8585
// Tracks whether the lock acquired is still correct
8686
bool validLock = true;
8787
// The lock corresponding to 'key'
88-
SemaphoreSlim itemLock = null;
88+
SemaphoreSlim? itemLock = null;
8989

9090
try {
9191
//We have to loop until we get a valid lock and it stays valid until we lock it.
@@ -167,7 +167,9 @@ public async Task<bool> TryExecuteAsync(string key, int timeoutMs, CancellationT
167167
}
168168
}
169169
// Ideally the only objects in 'locks' will be open operations now.
170-
return true;
170+
// This should be unreachable: the loop only exits when validLock is true,
171+
// and a valid lock always returns from inside the loop.
172+
throw new InvalidOperationException("Unreachable code in AsyncLockProvider.TryExecuteAsync");
171173
}
172174
}
173175
}

0 commit comments

Comments
 (0)