Skip to content

fix(instance): handle stale DB records after restart + prevent Redis connection spam#3

Merged
eksucampusmarketplace-cell merged 1 commit into
mainfrom
devin/1777893489-fix-stale-instance-and-redis
May 4, 2026
Merged

fix(instance): handle stale DB records after restart + prevent Redis connection spam#3
eksucampusmarketplace-cell merged 1 commit into
mainfrom
devin/1777893489-fix-stale-instance-and-redis

Conversation

@devin-ai-integration
Copy link
Copy Markdown

📋 Description

Fixes two critical issues that affect deployments on PaaS platforms (Render, Railway, etc.) where the server restarts frequently:

1. Stale Instance Deadlock After Restart

When Evolution API restarts, the in-memory waInstances map is cleared but database records persist. This creates a deadlock:

  • DELETE instance → 404 (not in memory, guard blocks it)
  • CREATE instance → 403 "name already in use" (DB record still exists)
  • SET WEBHOOK → 500 "Cannot read instanceId" (instance not in memory)
  • CONNECT → 404 (instance never loaded)
  • Result: clients like BotWave cannot pair or reconnect after any restart

Fix: The instanceLoggedGuard now detects when an instance exists in the database but NOT in memory (stale after restart). Instead of returning 403, it auto-cleans the stale DB record via cleaningStoreData() and allows re-creation.

2. Redis Connection Spam When Disabled

The Redis client (rediscache.client.ts) would attempt to connect regardless of CACHE_REDIS_ENABLED setting. Since the Dockerfile copies .env.example as .env (which had CACHE_REDIS_ENABLED=true), deployments without Redis got flooded with "redis disconnected" errors every second.

Fix:

  • Redis client now checks ENABLED flag and URI before attempting connection
  • .env.example defaults changed to CACHE_REDIS_ENABLED=false and CACHE_LOCAL_ENABLED=true so deployments without Redis work out of the box

🔗 Related Issue

Fixes instance creation failures and Redis spam on PaaS deployments (Render free tier).

🧪 Type of Change

  • 🐛 Bug fix (non-breaking change which fixes an issue)

🧪 Testing

  • Manual testing completed
  • Functionality verified in development environment
  • No breaking changes introduced
  • npm run build passes
  • npm run lint:check passes

Files Changed

  • src/api/guards/instance.guard.ts — Added stale instance detection and cleanup
  • src/cache/rediscache.client.ts — Added ENABLED/URI checks before connecting
  • .env.example — Changed Redis default to false, local cache to true

✅ Checklist

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

📝 Additional Notes

This is the third in a series of PaaS deployment fixes:

Link to Devin session: https://app.devin.ai/sessions/812442be799c4cd59ca9c40eee75263b
Requested by: @eksucampusmarketplace-cell

…spam

- Instance guard: when creating an instance, detect if the name exists
  in the database but not in memory (stale after server restart). Auto
  cleanup the stale DB record so re-creation succeeds instead of
  returning 403 'name already in use' followed by 404 on all operations.

- Redis client: skip connection when CACHE_REDIS_ENABLED is false or
  CACHE_REDIS_URI is empty. Previously the client would attempt to
  connect regardless, flooding logs with 'redis disconnected' errors
  every second when no Redis server is available.

- .env.example: change defaults to CACHE_REDIS_ENABLED=false and
  CACHE_LOCAL_ENABLED=true so deployments without Redis work out of
  the box (Dockerfile copies .env.example as .env).

Co-Authored-By: Chris Tolu <eksucampusmarketplace@gmail.com>
@devin-ai-integration
Copy link
Copy Markdown
Author

🤖 Devin AI Engineer

I'll be helping with this pull request! Here's what you should know:

✅ I will automatically:

  • Address comments on this PR. Add '(aside)' to your comment to have me ignore it.
  • Look at CI failures and help fix them

Note: I can only respond to comments from users who have write access to this repository.

⚙️ Control Options:

  • Disable automatic comment and CI monitoring

@eksucampusmarketplace-cell eksucampusmarketplace-cell deleted the devin/1777893489-fix-stale-instance-and-redis branch May 4, 2026 11:22
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