Skip to content

fix: Deadlock errors and undefined arguments #1473

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
May 21, 2025

Conversation

KokeroO
Copy link
Contributor

@KokeroO KokeroO commented May 19, 2025

Descrição

Este PR corrige diversos problemas relacionados a deadlocks causados por mensagens upsert e update que chegam duplicadas e são processadas simultaneamente pelo script, resultando em conflitos no banco de dados.
Também soluciona falhas ao tentar inserir determinados valores no banco.

Soluções

  • Implementação de cache em memória para identificar e evitar o processamento de mensagens duplicadas, prevenindo deadlocks.
  • Conversão de timestamps de milissegundos para segundos por meio de divisão por 1000, seguindo a mesma lógica adotada em outros pontos do script.
  • Reorganização e correção de métodos que estavam atribuindo valores undefined a certas propriedades de forma indevida.

Summary by Sourcery

Implement in-memory caching to prevent deadlocks from duplicate message processing, convert timestamps to seconds, and enforce null-safe handling of message types and payloads in WhatsApp and Chatwoot integrations

Bug Fixes:

  • Prevent deadlocks by caching and skipping duplicate upsert/update messages in the WhatsApp service
  • Throw errors for messages containing only context info or non-media types to avoid undefined arguments

Enhancements:

  • Standardize timestamp conversion from milliseconds to seconds across edit and update workflows
  • Refactor WhatsApp service to use unique cache keys, reduce TTL to 5 minutes, and skip broadcasts or undefined-ID messages for read receipts and delete events
  • Improve null-safety and default fallbacks in Chatwoot import helper for document and template messages

KokeroO added 4 commits May 19, 2025 23:05
…an INT4 (32-bit signed integer.

## Solução
Após analise dos valores recebidos e seguindo a logica em outras partes também, se confirma que o valor de entrada é de timestamp em ms.
## Erros:
- code: "40P01", message: "deadlock detected".
- Argument status is missing.
## Erros:
- Cannot read properties of null (reading 'fileName')
Copy link
Contributor

sourcery-ai bot commented May 19, 2025

Reviewer's Guide

This PR implements an in-memory deduplication cache across WhatsApp message workflows to prevent simultaneous processing deadlocks, standardizes timestamp values by converting from milliseconds to seconds, and restructures message handling to avoid inserting undefined values, while also refining the Chatwoot import helper and adjusting logging behavior.

File-Level Changes

Change Details Files
Add in-memory dedupe cache to prevent DB deadlocks
  • Generate a unique messageKey from remoteJid, timestamp, and fromMe
  • Wrap update, delete, and read‐receipt workflows with cache.get/set checks
  • Skip processing and emit a distinct '[avoid deadlock]' log on duplicates
  • Reduce cache TTL from 30 min to 5 min
whatsapp.baileys.service.ts
Normalize timestamps in edited message handler
  • Use Math.floor(timestampMs / 1000) for both Long and number cases
  • Replace direct toNumber() conversions with division by 1000
whatsapp.baileys.service.ts
Prevent undefined‐value inserts in message handlers
  • Throw if msg.message only contains messageContextInfo
  • Throw if mediaMessage remains unset to block non‐media inserts
whatsapp.baileys.service.ts
Refactor Chatwoot import helper for cleaner media captions
  • Filter out null/undefined message types before switch
  • Default document fileName to 'document' when undefined
  • Concatenate captions only if present, unify templateMessage handling
chatwoot-import-helper.ts
Refine logging levels and messages
  • Append '[avoid deadlock]' to duplicate‐ignore logs for clarity
  • Demote media processing duplicate logs from error to info level
  • Trim extraneous whitespace in error log statements
whatsapp.baileys.service.ts

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

@KokeroO
Copy link
Contributor Author

KokeroO commented May 20, 2025

@DavidsonGomes Esses problemas vim detectando em um cliente com mais de 10 instancias/ 20 mil msgs desde Abril e vim aplicando na 2.2.3 e testando. Também apliquei em um cliente com mais de 5 mil msgs por dia.
Atualmente os 2 estão com a 2.3.0 desta forma.

@KokeroO
Copy link
Contributor Author

KokeroO commented May 20, 2025

@sourcery-ai review

Copy link
Contributor

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey @KokeroO - I've reviewed your changes - here's some feedback:

  • There’s a lot of repeated cache key generation and get/set logic — consider extracting that into a small helper to DRY up the code and reduce duplication.
  • Instead of throwing bare strings like ‘The message is messageContextInfo’, throw proper Error objects to preserve stack traces and ensure consistent error handling.
  • Converting fromMe to a string for the cache key could be brittle; consider using a consistent boolean or number representation to avoid unexpected key collisions.
Here's what I looked at during the review
  • 🟡 General issues: 3 issues found
  • 🟢 Security: all looks good
  • 🟢 Testing: all looks good
  • 🟢 Documentation: all looks good

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

@KokeroO
Copy link
Contributor Author

KokeroO commented May 20, 2025

@sourcery-ai review

Copy link
Contributor

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey @KokeroO - I've reviewed your changes and they look great!

Here's what I looked at during the review
  • 🟢 General issues: all looks good
  • 🟢 Security: all looks good
  • 🟢 Testing: all looks good
  • 🟢 Documentation: all looks good

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

@DavidsonGomes DavidsonGomes merged commit cc01787 into EvolutionAPI:develop May 21, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants