Cette page n'est pas encore traduite. La version anglaise est affichée ci-dessous.

FDB1004: Id or clock read inside a retry-loop handler

Analyzer FoundationDB.Client
Severity Warning (correctness)
Code fix Yes

Message

The handler runs again on every retry, so '{0}' produces a different value on each attempt. Compute it before the call and capture it.

Why

ReadAsync, WriteAsync, and ReadWriteAsync run their handler again after a conflict or a transient error. A Guid.NewGuid() or a DateTime.UtcNow read inside the handler yields a different value on each attempt, so the key or value written by the committed attempt differs from what the caller computed, logged, or returned. A retry can also leave a record under an id that nothing else knows about.

The rule reports Guid.NewGuid(), Guid.CreateVersion7(...), DateTime.UtcNow, DateTime.Now, DateTimeOffset.UtcNow, DateTimeOffset.Now, and TimeProvider.GetUtcNow() inside a handler, when the result is an argument of a transaction call, directly or through a local declared in the handler. A value that stays out of the transaction (a log line, a timing) is not reported.

Bad

await db.WriteAsync(tr => tr.Set(subspace.Key(Guid.NewGuid()), value), ct);

Good

var id = Guid.NewGuid();
await db.WriteAsync(tr => tr.Set(subspace.Key(id), value), ct);

Code fix

The fix hoists a parameterless Guid.NewGuid() or Guid.CreateVersion7() into a local named id (or id2 when id is taken), declared before the statement that runs the retry loop. A clock read gets no fix, because the caller decides which instant to capture. A static lambda gets no fix, because it cannot capture the local.

Suppress

#pragma warning disable FDB1004 // reason

or, for a whole project, in .editorconfig:

dotnet_diagnostic.FDB1004.severity = none