Skip to content

[Bug] Race condition on NotificationPreferences creation causing Unique Constraint Violation #209

Description

@abarghoud

Criticité : 🔴 Élevée (Empêche l'utilisateur de recevoir les notifications push lors de l'alerte concernée)
Occurrences (sur 28 j) : ~4 occurrences (impactant de nouveaux utilisateurs)

Description of the issue
When multiple BREAK_IN_ALERT or SENTRY_ALERT events are triggered simultaneously for a user who doesn't have a NotificationPreferences record yet, the VehicleAlertNotifierService attempts to create it concurrently. This results in a PostgreSQL unique constraint violation (PK_notification_preferences_userId - code 23505).

Steps to Reproduce

  1. Trigger multiple concurrent alerts for a new user without notification preferences.
  2. The findOrCreatePreferences method triggers multiple concurrent INSERT queries.

Expected Behavior
The service should handle concurrent inserts gracefully, either by catching the unique constraint error and fetching the newly created record, or by using a Postgres upsert mechanism (e.g., ON CONFLICT DO NOTHING).

Logs

[NOTIFICATION_ERROR] Failed to send BREAK_IN_ALERT to user X...
Key ("userId")=(X) already exists. (code: 23505)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions