Skip to content

Listener Short Lifespan #189

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
arcanefeenix opened this issue Sep 11, 2017 · 3 comments
Closed

Listener Short Lifespan #189

arcanefeenix opened this issue Sep 11, 2017 · 3 comments
Labels

Comments

@arcanefeenix
Copy link

  • asyncpg version: 0.12.0
  • PostgreSQL version: 9.4.12
  • Python version: 3.6.1
  • Platform: Windows x64
  • Do you use pgbouncer?: No
  • Did you install asyncpg with pip?: Yes
  • If you built asyncpg locally, which version of Cython did you use?: N/A
  • Can the issue be reproduced under both asyncio and uvloop?: Unknown

When using conn.add_listener(), the listener only seems to be listening for NOTIFY messages immediately afterwards. If I execute the following code:

async with Database(self.bot) as db:
   async with db.pool.acquire() as conn:
      await conn.add_listener('announcements', listener)
      await conn.execute('NOTIFY "announcements", \'Test!\'')

I receive the notification in the log. If I just add the listener and execute the NOTIFY line elsewhere, it is not received. Also NOTIFY messages from other sources (such as the server via command line or function/trigger) are not received as well. Am I using the add_listener call incorrectly? Is the listener dropped after this call is complete?

Thanks for any help you can offer.

@elprans
Copy link
Member

elprans commented Sep 11, 2017

The code is incorrect. You are attempting to listen on a short-lived connection, which is returned to the pool once the async with pool.acquire() block is exited. Your listener is removed once the connection is returned to the pool.

The proper way is to open a long-lived connection and listen on that.

conn = await asyncpg.connect()
await conn.add_listener('announcements', listener)

# ... later, when done listening
await conn.remove_listener('announcements', listener)
await conn.close()

@elprans
Copy link
Member

elprans commented Sep 11, 2017

On a second thought, we should probably issue a warning when a connection is returned to the pool with unclosed listeners to give a better hint. I just opened #190 for that.

@vladyslav2
Copy link

vladyslav2 commented Jun 12, 2018

@elprans thanks for a warning
I start digging a code and found out what I did wrong

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants