Skip to content

Commit

Permalink
Merge pull request #82 from romank0/listen-races-fix
Browse files Browse the repository at this point in the history
Fixes potential races during LISTEN initiation
  • Loading branch information
PaulGilmartin authored Nov 17, 2024
2 parents 3aa099a + b6e586f commit d2081b3
Showing 1 changed file with 6 additions and 3 deletions.
9 changes: 6 additions & 3 deletions pgpubsub/listen.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,9 +111,12 @@ def listen_to_channels(channels: Union[List[BaseChannel], List[str]] = None):
if not channels:
raise ChannelNotFound()
cursor = connection.cursor()
for channel in channels:
logger.info(f'Listening on {channel.name()}\n')
cursor.execute(f'LISTEN {channel.listen_safe_name()};')
# Notifications are started to being delivered only after the transaction commits.
# Check LISTEN documentation for detailed description.
with transaction.atomic():
for channel in channels:
logger.info(f'Listening on {channel.name()}\n')
cursor.execute(f'LISTEN {channel.listen_safe_name()};')
return ConnectionWrapper(connection.connection)


Expand Down

0 comments on commit d2081b3

Please sign in to comment.