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

FDB0002: Watch created with the transaction's own token

Analyzer FoundationDB.Client
Severity Error
Code fix Yes

Message

A watch outlives its transaction, so it cannot use tr.Cancellation. Pass the token of the code that awaits the watch.

Why

A watch fires after its transaction commits, so it must survive the transaction. tr.Cancellation is cancelled when the transaction ends, and Watch throws ArgumentException when it receives that token. The rule reports the token argument, whether it is tr.Cancellation itself or a local copied from it.

Bad

await db.WriteAsync(tr =>
{
	var watch = tr.Watch(key, tr.Cancellation);
}, ct);

Good

await db.WriteAsync(tr =>
{
	var watch = tr.Watch(key, ct);
}, ct);

Pass the token of the code that awaits the watch: the token of the retry loop, or CancellationToken.None.

Code fix

When the call is inside a ReadAsync, WriteAsync, or ReadWriteAsync handler, the fix replaces the argument with the token passed to that call. Outside a handler, the rule reports without a fix.

Suppress

#pragma warning disable FDB0002 // reason

or, for a whole project, in .editorconfig:

dotnet_diagnostic.FDB0002.severity = none