Skip to content

Conversation

@petrprikryl
Copy link
Contributor

Hi, I've just hit error psycopg_pool.PoolTimeout: couldn't get a connection after 30.0 sec when using Django with psycopg[pool] Connection pool. Like in psycopg/psycopg#544. It looks like Celery is sharing the pool between multiple processes/workers.

This PR is adding explicit pool closing similar like in Django itself https://github.com/django/django/blob/6.0a1/django/db/backends/postgresql/creation.py#L61

I've been testing this PR for few hours on big project with multiple workers with no more connection issues.

For reference:
https://docs.djangoproject.com/en/dev/ref/databases/#postgresql-pool
https://www.psycopg.org/psycopg3/docs/advanced/pool.html

Copy link
Member

@auvipy auvipy left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do this features work from django 4.2 to 6.0?

@codecov
Copy link

codecov bot commented Oct 16, 2025

Codecov Report

❌ Patch coverage is 80.00000% with 1 line in your changes missing coverage. Please review.
✅ Project coverage is 78.67%. Comparing base (2731860) to head (c228dbe).
⚠️ Report is 1 commits behind head on main.

Files with missing lines Patch % Lines
celery/fixups/django.py 80.00% 1 Missing ⚠️
Additional details and impacted files
@@           Coverage Diff           @@
##             main    #9953   +/-   ##
=======================================
  Coverage   78.67%   78.67%           
=======================================
  Files         153      153           
  Lines       19299    19304    +5     
  Branches     2211     2212    +1     
=======================================
+ Hits        15184    15188    +4     
- Misses       3816     3817    +1     
  Partials      299      299           
Flag Coverage Δ
unittests 78.65% <80.00%> (+<0.01%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Copy link
Member

@auvipy auvipy left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

also the change needs test coverage

@petrprikryl
Copy link
Contributor Author

do this features work from django 4.2 to 6.0?

Connection pool works from Django 5.1+ only. But this Celery change is backward compatible and safe as it checks for close_pool attribute existence. So this is also safe for other Django DB backends without Connection pool support.

also the change needs test coverage

I've added some unit tests. Not sure if you want test some more special cases or direct psycopg[pool] integration. It would require installing it and extending the requirements.

@petrprikryl petrprikryl requested a review from auvipy October 17, 2025 13:54
@auvipy
Copy link
Member

auvipy commented Oct 18, 2025

thanks

@auvipy auvipy requested a review from Copilot October 18, 2025 07:44
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR adds support for Django's connection pool functionality by ensuring that psycopg connection pools are properly closed when Celery closes database connections. The change prevents psycopg_pool.PoolTimeout errors that occur when connection pools are shared between multiple processes/workers.

Key changes:

  • Modified _close_database() to call close_pool() on connections that support it (Django + psycopg[pool])
  • Added suppression of KeyError exceptions during pool closure (handles already-closed pools)

Reviewed Changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

File Description
celery/fixups/django.py Added connection pool closing logic with error suppression in _close_database()
t/unit/fixups/test_django.py Added three test cases covering pool closure, missing pool method, and error suppression

Copy link
Member

@auvipy auvipy left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should also document the new pool feature and also point to django documentation for that and how this will work in celery

@petrprikryl
Copy link
Contributor Author

I think we should also document the new pool feature and also point to django documentation for that and how this will work in celery

Did my best. Not sure about Changelog. Tell me if you need addition there as well.

@petrprikryl petrprikryl requested a review from auvipy October 20, 2025 09:27
@auvipy auvipy added this to the 5.6.0 milestone Oct 20, 2025
@auvipy auvipy requested a review from Copilot October 20, 2025 12:50
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

Copilot reviewed 3 out of 3 changed files in this pull request and generated 1 comment.

@auvipy auvipy merged commit 1ad3bd4 into celery:main Oct 20, 2025
107 checks passed
kumuthu53 pushed a commit to lynxx-apac/celery that referenced this pull request Oct 22, 2025
* Add support for Django Connection pool

https://docs.djangoproject.com/en/dev/ref/databases/#postgresql-pool

* tests for close_pool

* close_pool called only if DB pool is enabled in Django settings

* conn pool docs

---------

Co-authored-by: Asif Saif Uddin {"Auvi":"অভি"} <auvipy@gmail.com>
@auvipy
Copy link
Member

auvipy commented Dec 6, 2025

we got this bug report, can you cross check please #10018 ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants