Skip to content

Commit

Permalink
k
Browse files Browse the repository at this point in the history
  • Loading branch information
pablonyx committed Jan 18, 2025
1 parent 7c318a3 commit 2b6a074
Showing 1 changed file with 14 additions and 13 deletions.
27 changes: 14 additions & 13 deletions backend/onyx/onyxbot/slack/listener.py
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ def acquire_tenants(self) -> None:
- Attempt to acquire a Redis lock for each tenant.
- If acquired, check if that tenant actually has Slack bots.
- If yes, store them in self.tenant_ids and manage the socket connections.
- If a tenant in self.tenant_ids no longer has Slack bots, remove it and release the lock.
- If a tenant in self.tenant_ids no longer has Slack bots, remove it (and release the lock in this scope).
"""
all_tenants = get_all_tenant_ids()

Expand Down Expand Up @@ -351,6 +351,17 @@ def acquire_tenants(self) -> None:
f"Tenant {tenant_id} no longer has Slack bots. Removing."
)
self._remove_tenant(tenant_id)

# IMPORTANT: Release the lock here (in the same scope it was acquired)
if tenant_id in self.redis_locks and not DEV_MODE:
try:
self.redis_locks[tenant_id].release()
del self.redis_locks[tenant_id]
logger.info(f"Released lock for tenant {tenant_id}")
except Exception as e:
logger.error(
f"Error releasing lock for tenant {tenant_id}: {e}"
)
else:
# Manage or reconnect Slack bot sockets
for bot in bots:
Expand All @@ -364,8 +375,8 @@ def acquire_tenants(self) -> None:

def _remove_tenant(self, tenant_id: str | None) -> None:
"""
Helper to remove a tenant from `self.tenant_ids` and close any socket clients,
then release the Redis lock if we hold it.
Helper to remove a tenant from `self.tenant_ids` and close any socket clients.
(Lock release now happens in `acquire_tenants()`, not here.)
"""
# Close all socket clients for this tenant
for (t_id, slack_bot_id), client in list(self.socket_clients.items()):
Expand All @@ -381,16 +392,6 @@ def _remove_tenant(self, tenant_id: str | None) -> None:
if tenant_id in self.tenant_ids:
self.tenant_ids.remove(tenant_id)

# Release the lock if we hold it
if tenant_id in self.redis_locks and not DEV_MODE:
try:
self.redis_locks[tenant_id].release()
logger.info(f"Released lock for tenant {tenant_id}")
except Exception as e:
logger.error(f"Error releasing lock for tenant {tenant_id}: {e}")
finally:
del self.redis_locks[tenant_id]

def send_heartbeats(self) -> None:
current_time = int(time.time())
logger.debug(f"Sending heartbeats for {len(self.tenant_ids)} active tenants")
Expand Down

0 comments on commit 2b6a074

Please sign in to comment.