Skip to content

Commit

Permalink
Merge pull request #8 from permitio/rk/fix-relesed-conns-bug
Browse files Browse the repository at this point in the history
Postgres: Safe removal of listeners on unsubscribe + disconnect
  • Loading branch information
roekatz authored Aug 23, 2023
2 parents c79b70d + e2527f4 commit 1e179c1
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 3 deletions.
2 changes: 1 addition & 1 deletion broadcaster/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from ._base import Broadcast, Event

__version__ = "0.2.4"
__version__ = "0.2.5"
__all__ = ["Broadcast", "Event"]
13 changes: 11 additions & 2 deletions broadcaster/_backends/postgres.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,24 @@ async def connect(self) -> None:
self._conn.add_termination_listener(self._termination_listener)

async def disconnect(self) -> None:
self._conn.remove_termination_listener(self._termination_listener)
try:
self._conn.remove_termination_listener(self._termination_listener)
except Exception:
# Best effort, would fail if conn already closed (thus released)
pass

await (await self._get_pool()).release(self._conn)
self._conn = None

async def subscribe(self, channel: str) -> None:
await self._conn.add_listener(channel, self._listener)

async def unsubscribe(self, channel: str) -> None:
await self._conn.remove_listener(channel, self._listener)
try:
await self._conn.remove_listener(channel, self._listener)
except Exception:
# Best effort, would fail if conn already closed (thus released)
pass

async def publish(self, channel: str, message: str) -> None:
await self._conn.execute("SELECT pg_notify($1, $2);", channel, message)
Expand Down

0 comments on commit 1e179c1

Please sign in to comment.