Path: redb-identity/src/redb.Identity.Core/Module/IdentityExchangeAccessor.cs Snapshot: redb-identity, development snapshot 2026-12-06. This is the development state of the public mirrors; it may differ from the published packages. License: Apache-2.0. Repository: https://github.com/redbase-app/redb-identity Page: https://redbase.app/src/dev-2026-12-06/redb-identity/src/redb.Identity.Core/Module/IdentityExchangeAccessor.cs ---- using redb.Route.Abstractions; namespace redb.Identity.Core.Module; /// /// Carries the that a per-exchange Identity DI scope was opened for. /// /// This is the missing link that lets everything inside the isolated Identity child container — /// most importantly the OpenIddict stores (RedbTokenStore, RedbAuthorizationStore, …) — /// write on the same IRedbService, and therefore the same DB connection, that a /// route-level redb transaction was opened on. /// /// /// Why it is needed at all. In the .tpkg topology the route-context host provider does /// not carry Identity's registrations (that is the point of module isolation), so Identity resolves /// its services from a child container. Until now every child scope also opened its own host /// scope for — a second connection. A route-level /// transaction runs on the exchange-cached connection, so the stores were writing outside it: no /// atomicity, and — because the second connection blocks on the row locks the first one holds while /// the first awaits the call that opened the second — a deadlock that only cleared on the 30s /// transaction timeout. /// /// /// Scoped. Set once by whoever opens the scope (see IdentityRouteContextExtensions), read by /// HostRedbScope. Deliberately not an AsyncLocal: the scope is created /// explicitly and lives for exactly one exchange, so an explicit holder is simpler, deterministic, /// and cannot leak across tasks. /// /// internal sealed class IdentityExchangeAccessor { /// /// The exchange this scope belongs to, or null when the scope was opened outside a route /// (hosted services, cleanup timers, schema init, diagnostics). In that case there is no /// ambient transaction to join and the caller falls back to its own host scope. /// public IExchange? Exchange { get; set; } }