evolution-api icon indicating copy to clipboard operation
evolution-api copied to clipboard

IsOnWhatsapp unique constraint violation on remoteJid (race on placeholder value [email protected])

Open BenkiNew opened this issue 3 months ago • 1 comments

Bug: IsOnWhatsapp unique constraint violation on remoteJid (race on placeholder value [email protected])

Environment

  • Image: evoapicloud/evolution-api:2.4.0-rc2 (package.json reports "version": "2.4.0")
  • Baileys: 7.0.0-rc.9
  • Database: PostgreSQL 18, via Prisma
  • Integration: Baileys (WHATSAPP-BAILEYS)

Description

Under concurrent load, IsOnWhatsapp upserts intermittently fail with a Postgres unique-constraint violation on remoteJid. It looks like two separate rows/requests both try to write the sentinel value [email protected] (returned by Baileys' number-check when a lookup is empty/invalid), and the second write hits the @unique constraint because the app updates by id instead of upserting on remoteJid.

Prisma schema (relevant model)

model IsOnWhatsapp {
  id         String   @id @default(cuid())
  remoteJid  String   @unique @db.VarChar(100)
  jidOptions String
  lid        String?  @db.VarChar(100)
  createdAt  DateTime @default(now()) @db.Timestamp
  updatedAt  DateTime @updatedAt @db.Timestamp
}

Observed error (PostgreSQL log, 4 occurrences within ~12 minutes, none since)

2026-08-15 12:22:32.547 EEST [838008] ERROR:  duplicate key value violates unique constraint "IsOnWhatsapp_remoteJid_key"
2026-08-15 12:22:32.547 EEST [838008] DETAIL:  Key ("remoteJid")=([email protected]) already exists.
2026-08-15 12:22:32.547 EEST [838008] STATEMENT:  UPDATE "public"."IsOnWhatsapp" SET "remoteJid" = $1, "jidOptions" = $2, "lid" = $3, "updatedAt" = $4 WHERE ("public"."IsOnWhatsapp"."id" = $5 AND 1=1) RETURNING "public"."IsOnWhatsapp"."id", "public"."IsOnWhatsapp"."remoteJid", "public"."IsOnWhatsapp"."jidOptions", "public"."IsOnWhatsapp"."lid", "public"."IsOnWhatsapp"."createdAt", "public"."IsOnWhatsapp"."updatedAt"

(Repeated at 12:23:32, 12:33:49, 12:34:49 with different backend PIDs — always the same conflicting value [email protected].)

Steps to reproduce (best guess from the log pattern)

  1. Trigger multiple concurrent onWhatsapp/number-check calls (e.g. via /chat/whatsappNumbers or internal contact-sync) close together in time.
  2. When Baileys returns an empty/invalid result for more than one of these checks, it appears each one resolves to the same placeholder JID [email protected].
  3. The app updates an existing IsOnWhatsapp row (matched by its own id) and sets remoteJid to this placeholder. The first write succeeds; a concurrent second write with the same placeholder value fails the @unique constraint on remoteJid, since the update path doesn't use upsert/ON CONFLICT (remoteJid) DO UPDATE.

Impact

Low — the error is caught/logged and doesn't appear to break message flow or crash the instance. Not reproduced outside this ~12-minute window in ~2+ hours of subsequent operation, so likely tied to a burst of concurrent number-check calls rather than a steady-state issue.

Suggested fix

Use prisma.isOnWhatsapp.upsert({ where: { remoteJid }, ... }) (or ON CONFLICT (remoteJid) DO UPDATE) instead of update by id, so a second writer for the same remoteJid merges into the existing row instead of violating the unique constraint. Alternatively, treat [email protected] (or any non-JID sentinel) as "no result" and skip persisting it at all.

BenkiNew avatar Aug 15 '26 11:08 BenkiNew

Closing this on our side: we have moved off Evolution API, so we can no longer reproduce or follow up on it. The report stays here for anyone who hits the same behaviour.

BenkiNew avatar Sep 26 '26 08:09 BenkiNew