B-1 Audit sink poisons requests: Guid.Empty actor FK + shared DbContext (root cause of flaky tests) #32

Closed
opened 2026-11-03 08:23:56 +01:00 by amiryahaya · 1 comment
Owner

Introduced by commit 997f657 (Mar 24). SsdidAuditSink.cs:36 writes audit rows with ActorId=Guid.Empty — FK violation vs users.id — fire-and-forget into the request-scoped DbContext (Program.cs:99), poisoning handlers' awaited SaveChanges (DbContext is not thread-safe). Result: 371 failed audit_log inserts + 51 'concurrent insert expected but user not found' 500s per test run = the 26–29 flaky tests (task #27); production silently loses ALL SDK audit rows; wallet register/login 500s intermittently.

Fixes:

  1. Make AuditLogEntry.ActorId nullable (Guid?) + OnDelete(SetNull) + migration
  2. SsdidAuditSink: resolve actor or use null; write via own IServiceScopeFactory scope, wrapped in try/catch (pattern: FileActivityService.cs:9-13)
  3. Narrow RegisterVerify.cs:195-201 DbUpdateException catch to the unique-index violation only
  4. Fire-and-forget activity/audit calls: use CancellationToken.None (currently bound to request token — audit lost on client timeout)
  5. Follow-ups: schema-parity test (EnsureCreated vs migrations), xunit.runner.json (maxParallelThreads, offline-skip for RealRegistryFactory), fix dotnet test (add UseMicrosoftTestingPlatformRunner or Test.Sdk + xunit.runner.visualstudio)

Unblocks #27 (flaky triage → verify 3 consecutive zero-failure runs). Full findings + evidence: docs/redesign/2026-11-onboarding-passkey/08-code-baseline-review.md

Introduced by commit 997f657 (Mar 24). SsdidAuditSink.cs:36 writes audit rows with ActorId=Guid.Empty — FK violation vs users.id — fire-and-forget into the request-scoped DbContext (Program.cs:99), poisoning handlers' awaited SaveChanges (DbContext is not thread-safe). Result: 371 failed audit_log inserts + 51 'concurrent insert expected but user not found' 500s per test run = the 26–29 flaky tests (task #27); production silently loses ALL SDK audit rows; wallet register/login 500s intermittently. Fixes: 1. Make AuditLogEntry.ActorId nullable (Guid?) + OnDelete(SetNull) + migration 2. SsdidAuditSink: resolve actor or use null; write via own IServiceScopeFactory scope, wrapped in try/catch (pattern: FileActivityService.cs:9-13) 3. Narrow RegisterVerify.cs:195-201 DbUpdateException catch to the unique-index violation only 4. Fire-and-forget activity/audit calls: use CancellationToken.None (currently bound to request token — audit lost on client timeout) 5. Follow-ups: schema-parity test (EnsureCreated vs migrations), xunit.runner.json (maxParallelThreads, offline-skip for RealRegistryFactory), fix `dotnet test` (add UseMicrosoftTestingPlatformRunner or Test.Sdk + xunit.runner.visualstudio) Unblocks #27 (flaky triage → verify 3 consecutive zero-failure runs). Full findings + evidence: docs/redesign/2026-11-onboarding-passkey/08-code-baseline-review.md
Author
Owner

Fixed in commit be3d502 (TDD: AuditSinkTests red at predicted lines → green).

  • AuditLogEntry.ActorId → Guid?, FK SetNull, migration AuditActorNullable
  • Sink writes via own IServiceScopeFactory scope, try/catch + warning log — audit failures can never surface into a user response
  • RegisterVerify catch narrowed to unique-constraint violations
  • Fire-and-forget activity calls now CancellationToken.None

Verification: 3 consecutive zero-failure runs — 680/680 ×3 (Release), first fully-green suite since March. Unblocks #27 (coverage gate; hook failure-tolerance now retirable).

**Fixed in commit `be3d502`** (TDD: AuditSinkTests red at predicted lines → green). - `AuditLogEntry.ActorId` → `Guid?`, FK `SetNull`, migration `AuditActorNullable` - Sink writes via own `IServiceScopeFactory` scope, try/catch + warning log — audit failures can never surface into a user response - `RegisterVerify` catch narrowed to unique-constraint violations - Fire-and-forget activity calls now `CancellationToken.None` **Verification: 3 consecutive zero-failure runs — 680/680 ×3 (Release)**, first fully-green suite since March. Unblocks #27 (coverage gate; hook failure-tolerance now retirable).
Sign in to join this conversation.
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
primatekuntech/failsulit#32
No description provided.