FDB1002: Subspace returned out of a retry-loop handler

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

Message

The subspace returned by this handler belongs to its transaction and can be stale in the next one. Return the location, and call Resolve(tr) inside each transaction.

Why

A subspace resolved inside a transaction is only valid in that transaction: the directory it maps can move afterwards. A ReadAsync or ReadWriteAsync handler that returns a subspace (IKeySubspace, FdbDirectorySubspace) or the State of a layer hands it to code that runs in a later transaction, which then writes under a stale prefix.

The rule reports only a result that escapes its transaction: used inside a later handler or in a call that takes a transaction, returned by the method, or stored in a field or a property. A result that is only read outside a transaction (logged, printed, compared) is not reported.

WriteAsync is exempt: it discards the result, so await db.WriteAsync(tr => db.Root["a"].CreateOrOpenAsync(tr), ct) is the sanctioned way to create a directory at startup. The rule reports after a build, like FDB1001.

Bad

var subspace = await db.ReadWriteAsync(tr => db.Root["a"].CreateOrOpenAsync(tr), ct);
await db.WriteAsync(tr => tr.Set(subspace.Key(id), value), ct);

Good

await db.WriteAsync(tr => db.Root["a"].CreateOrOpenAsync(tr), ct);
var location = db.Root["a"];
await db.WriteAsync(async tr =>
{
	var subspace = await location.Resolve(tr);
	tr.Set(subspace.Key(id), value);
}, ct);

Suppress

#pragma warning disable FDB1002 // reason

or, for a whole project, in .editorconfig:

dotnet_diagnostic.FDB1002.severity = none