fix(audit_log): prevent deadlock in Stop() when Start() is never called

Updated the AuditLogRetentionRunner to include a 'started' flag, allowing Stop() to return immediately if Start() was never invoked. This change prevents potential deadlocks during graceful shutdown. Additionally, added tests to ensure that Stop() behaves correctly when called before Start() and when the service is nil.

Refs: #1303
This commit is contained in:
wizardchen
2026-05-17 22:27:00 +08:00
committed by lyingbug
parent 884ac44283
commit 14f62a6e14
6 changed files with 173 additions and 33 deletions
@@ -280,6 +280,21 @@ func TestTenantMemberService_AddMember_RejectsDuplicate(t *testing.T) {
}
}
func TestTenantMemberService_AddMember_MapsDuplicateKeyRace(t *testing.T) {
// Simulate the TOCTOU race: Get() saw no row, then a concurrent
// AddMember inserted before us, so our Create hits the partial
// unique index. The DB returns a duplicate-key error, which the
// service must translate into ErrMembershipAlreadyExists so the
// handler returns 409 rather than a generic 500.
svc, repo := newServiceWithRepo()
repo.failCreate = errors.New(
"ERROR: duplicate key value violates unique constraint \"idx_tenant_members_user_tenant_unique\"")
_, err := svc.AddMember(context.Background(), "u_race", 1, types.TenantRoleContributor, nil)
if !errors.Is(err, ErrMembershipAlreadyExists) {
t.Fatalf("want ErrMembershipAlreadyExists on duplicate-key race, got %v", err)
}
}
func TestTenantMemberService_EnsureOwner_Idempotent(t *testing.T) {
svc, repo := newServiceWithRepo()
ctx := context.Background()