Skip to content

max_inactive_connection_lifetime param not respected in a conn pool #1161

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
dilshans2k opened this issue Jul 3, 2024 · 1 comment
Closed

Comments

@dilshans2k
Copy link

dilshans2k commented Jul 3, 2024

  • asyncpg version:0.29.0
  • PostgreSQL version:16.1
  • Do you use a PostgreSQL SaaS? If so, which? Can you reproduce
    the issue with a local PostgreSQL install?
    : Local install - YES
  • Python version:3.9.18
  • Platform:Linux-Ubuntu
  • Do you use pgbouncer?:No
  • Did you install asyncpg with pip?:Yes
  • If you built asyncpg locally, which version of Cython did you use?:-
  • Can the issue be reproduced under both asyncio and
    uvloop?
    : Tested with asyncio only.

Based on this configuration, connection should be closed after 5 seconds of idleness.

self.connection_pool = await asyncpg.create_pool(
    database=dbname,
    user=user,
    password=password,
    host=host,
    port=5455,
    min_size=0,
    max_size=10,
    max_inactive_connection_lifetime=5,
)

I am acquiring two connections, doing some operation and then releasing them. In the finally block, i am also checking number of idle connections in the pool and it prints 2 continously. It is also visible from

connection: asyncpg.Connection = await self.connection_pool.acquire()
connection1: asyncpg.Connection = await self.connection_pool.acquire()
try:
    print(f"{datetime.datetime.now()} executing")
    await connection.execute("SELECT 1")
    await connection1.execute("SELECT 1")
    print(f"{datetime.datetime.now()} executed")
finally:
    await self.connection_pool.release(connection)
    await self.connection_pool.release(connection1)
    while True:
        print(
            f"{datetime.datetime.now()} {self.connection_pool.get_idle_size()}"
        )
        time.sleep(1)

image

> sudo netstat -panc | grep 5455
tcp        0      0 172.20.5.200:57938      172.20.0.44:5455        ESTABLISHED 854365/python       
tcp        0      0 172.20.5.200:57946      172.20.0.44:5455        ESTABLISHED 854365/python 

It closes the connections only on close or terminate call and not automatically like it should according to the max_inactive_connection_lifetime param.

Thanks

@elprans
Copy link
Member

elprans commented Jul 17, 2024

You are using a blocking loop, and so the event loop does not get a chance to run the inactive connection cleanup callbacks. Change time.sleep to await asyncio.sleep and you'll see it drop to zero.

@elprans elprans closed this as not planned Won't fix, can't repro, duplicate, stale Jul 17, 2024
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

No branches or pull requests

2 participants