Skip to content

fix: delete instance cleanup on baileys failure#2508

Open
antonio-abrantes wants to merge 1 commit intoEvolutionAPI:developfrom
antonio-abrantes:fix/delete-instance-baileys-cleanup
Open

fix: delete instance cleanup on baileys failure#2508
antonio-abrantes wants to merge 1 commit intoEvolutionAPI:developfrom
antonio-abrantes:fix/delete-instance-baileys-cleanup

Conversation

@antonio-abrantes
Copy link
Copy Markdown

📋 Description

🐛 Problem

When calling DELETE /instance/delete/{instanceName} on a corrupted
instance (disconnected/bugged Baileys session), the API returns:

{
  "status": 400,
  "error": "Bad Request",
  "response": { "message": ["[object Object]"] }
}

After this error, the instance name remains permanently blocked in
waInstances memory. Even after manually cleaning the database, Redis,
and session files, calling POST /instance/create with the same name
returns:

{
  "status": 403,
  "message": ["This name \"agent-xxx\" is already in use."]
}

The only workaround was restarting the container — not viable in production environments.


🔍 Root Cause

Two bugs working together:

1. Aborted cleanup

  • deleteInstance() stopped execution if logout() failed

2. Bad error serialization

  • Errors returned as [object Object]

✅ Fix

  • Split try/catch into isolated steps

  • Ensure cleanup always runs

  • Always remove instance from waInstances

  • Fix error serialization using:

    error?.message ?? String(error)
    

🔗 Related Issue

Closes # (leave empty if not applicable)


🧪 Type of Change

  • 🐛 Bug fix (non-breaking change which fixes an issue)
  • ✨ New feature
  • 💥 Breaking change
  • 📚 Documentation update
  • 🔧 Refactoring
  • ⚡ Performance improvement
  • 🧹 Code cleanup
  • 🔒 Security fix

🧪 Testing

  • Manual testing completed
  • Functionality verified in development environment
  • No breaking changes introduced
  • Tested with different connection types

📸 Screenshots (if applicable)

N/A


✅ Checklist

  • My code follows the project's style guidelines
  • I have performed a self-review of my code
  • My changes generate no new warnings
  • I have manually tested my changes thoroughly
  • I have verified the changes work with different scenarios

📝 Additional Notes

Tested in production environment with Docker Swarm + PostgreSQL + Redis.
Fix only affects failure path — no impact on healthy instances.

Copy link
Copy Markdown
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.

Sorry @antonio-abrantes, you have reached your weekly rate limit of 500000 diff characters.

Please try again later or upgrade to continue using Sourcery

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.

1 participant