question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

MAX_CONNS not being respected

See original GitHub issue

Hello, I’m make some experimentations running django 3 under gevent.

I’ve created an endpoint that execute a pg_sleep faking slow queries, then, I used vegeta for tests my setup with high load.

my MAX_CONNS was 10.

But some of my api response was 500 :

Traceback (most recent call last):
  File "/home/danilo/.local/share/virtualenvs/django_db__pool_issues_example-SoJGTr3V/lib/python3.6/site-packages/django/core/handlers/exception.py", line 34, in inner
    response = get_response(request)
  File "/home/danilo/.local/share/virtualenvs/django_db__pool_issues_example-SoJGTr3V/lib/python3.6/site-packages/django/core/handlers/base.py", line 115, in _get_response
    response = self.process_exception_by_middleware(e, request)
  File "/home/danilo/.local/share/virtualenvs/django_db__pool_issues_example-SoJGTr3V/lib/python3.6/site-packages/django/core/handlers/base.py", line 113, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "/home/danilo/Code/django_db__pool_issues_example/app/sleep_db.py", line 9, in sleep_in_db
    with connection.cursor() as cursor:
  File "/home/danilo/.local/share/virtualenvs/django_db__pool_issues_example-SoJGTr3V/lib/python3.6/site-packages/django/utils/asyncio.py", line 26, in inner
    return func(*args, **kwargs)
  File "/home/danilo/.local/share/virtualenvs/django_db__pool_issues_example-SoJGTr3V/lib/python3.6/site-packages/django/db/backends/base/base.py", line 260, in cursor
    return self._cursor()
  File "/home/danilo/.local/share/virtualenvs/django_db__pool_issues_example-SoJGTr3V/lib/python3.6/site-packages/django/db/backends/base/base.py", line 236, in _cursor
    self.ensure_connection()
  File "/home/danilo/.local/share/virtualenvs/django_db__pool_issues_example-SoJGTr3V/lib/python3.6/site-packages/django/utils/asyncio.py", line 26, in inner
    return func(*args, **kwargs)
  File "/home/danilo/.local/share/virtualenvs/django_db__pool_issues_example-SoJGTr3V/lib/python3.6/site-packages/django/db/backends/base/base.py", line 220, in ensure_connection
    self.connect()
  File "/home/danilo/.local/share/virtualenvs/django_db__pool_issues_example-SoJGTr3V/lib/python3.6/site-packages/django/db/utils.py", line 90, in __exit__
    raise dj_exc_value.with_traceback(traceback) from exc_value
  File "/home/danilo/.local/share/virtualenvs/django_db__pool_issues_example-SoJGTr3V/lib/python3.6/site-packages/django/db/backends/base/base.py", line 220, in ensure_connection
    self.connect()
  File "/home/danilo/.local/share/virtualenvs/django_db__pool_issues_example-SoJGTr3V/lib/python3.6/site-packages/django/utils/asyncio.py", line 26, in inner
    return func(*args, **kwargs)
  File "/home/danilo/.local/share/virtualenvs/django_db__pool_issues_example-SoJGTr3V/lib/python3.6/site-packages/django/db/backends/base/base.py", line 197, in connect
    self.connection = self.get_new_connection(conn_params)
  File "/home/danilo/.local/share/virtualenvs/django_db__pool_issues_example-SoJGTr3V/lib/python3.6/site-packages/django_db_geventpool/backends/postgresql_psycopg2/base.py", line 49, in get_new_connection
    self.connection = self.pool.get()
  File "/home/danilo/.local/share/virtualenvs/django_db__pool_issues_example-SoJGTr3V/lib/python3.6/site-packages/django_db_geventpool/backends/postgresql_psycopg2/psycopg2_pool.py", line 48, in get
    conn = self.create_connection()
  File "/home/danilo/.local/share/virtualenvs/django_db__pool_issues_example-SoJGTr3V/lib/python3.6/site-packages/django_db_geventpool/backends/postgresql_psycopg2/psycopg2_pool.py", line 84, in create_connection
    conn = self.connect(*self.args, **self.kwargs)
  File "/home/danilo/.local/share/virtualenvs/django_db__pool_issues_example-SoJGTr3V/lib/python3.6/site-packages/psycopg2/__init__.py", line 126, in connect
    conn = _connect(dsn, connection_factory=connection_factory, **kwasync)
  File "/home/danilo/.local/share/virtualenvs/django_db__pool_issues_example-SoJGTr3V/lib/python3.6/site-packages/psycogreen/gevent.py", line 32, in gevent_wait_callback
    state = conn.poll()
django.db.utils.OperationalError: FATAL:  sorry, too many clients already

I’ve uploaded a project to show it. https://github.com/dnp1/django_db_gevent_pool_issues_example

My attack was: echo “GET http://localhost:8000/sleep_in_db” | vegeta attack -duration=10s --rate 100 | tee results.bin | vegeta report

When I watched the database: SELECT * FROM "pg_stat_activity" WHERE "client_addr" = '172.26.0.1' there was 99 connections of my application.

I’m using python 3.6.10 (CPython) under Linux. My postgres is running inside a docker container

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:6 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
s4kecommented, Jul 23, 2020

Thanks for the fix @bellini666 . Just had an issue with a lot of extra connections being created and this fixed it.

1reaction
bellini666commented, May 15, 2020

@joekohlsdorf from what I can tell, the previous version had a small issue here: https://github.com/jneight/django-db-geventpool/blob/a53a258eec27f76939516792020192c565d6f8ee/django_db_geventpool/backends/postgresql_psycopg2/psycopg2_pool.py#L48

As you can see, when that new_connection was used, it would create a new connection without doing self.size += 1. That could probably lead to more connections being used.

Either way, I don’t know if you saw my update to my PR, but I changed it a lot based on your comment and we are waiting for your feedback so it can get merged. In it I already fixed that issue you are mentioning by making sure that all new connections will increase self.size

Read more comments on GitHub >

github_iconTop Results From Across the Web

max_connections is not respected and connections pool ...
max_connections is not respected and connections pool grows until connections are closed by idle timeout #648.
Read more >
Re: maxconn not respecting idle connections? - The Mail Archive
Yes, unfortunately it does create a problem. > > Each backend server (a SOAP API) can only handle up to 7 concurrent >...
Read more >
maxconn not respecting idle connections?
Hi, I've set "hard limits" with maxconn for each backend server but it seems that established (keep-alive) connections are not accounted for in...
Read more >
maxconn limit per backend in haproxy - Server Fault
Why maxconn is not respected with http-keep-alive ? I verified with ss that the opened backend connections are in ESTABLISHED state.
Read more >
maxconn limit per backend in haproxy - Stack Overflow
Why maxconn is not respected with http-keep-alive ? I verified with ss that the opened backend connections are in ESTABLISHED state.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found