[Django] #35577: runserver leaves a database connection infinitely unclosed as a result of its migration check

8 views
Skip to first unread message

Django

unread,
4:32 AM (14 hours ago) 4:32 AM
to django-...@googlegroups.com
#35577: runserver leaves a database connection infinitely unclosed as a result of
its migration check
-------------------------------------+-------------------------------------
Reporter: Klaas van Schelven | Type:
| Uncategorized
Status: new | Component:
| Uncategorized
Version: 4.2 | Severity: Normal
Keywords: | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
When starting the "Debug server" (runserver command), Django does a check
on the migrations ("check_migrations").

This check touches the DB (to see which migrations have run)

However, after the call there's no call to `connection.close()`. (or some
conditional form thereof).

This means that for the debugserver one connection will remain permanently
open. The name of the thread on which this connection is created is
`django-main-thread`.

Looking at [https://docs.djangoproject.com/en/5.0/ref/databases
/#persistent-connections the documentation] the per-request model of
connection lifetimes is clear. Other than that the following caveat is
documented:

> If a connection is created in a long-running process, outside of
Django’s request-response cycle, the connection will remain open until
explicitly closed, or timeout occurs.

But one would assume that this caveat applies to connections opened by the
developers themselves, not by what Django does internally.

Here's the call to `check_migrations`:
https://github.com/django/django/blob/stable/4.2.x/django/core/management/commands/runserver.py#L136

I've seen this behavior on Django 4.2, have not tested it with a more
recent version yet, but I don't have a reason to think it's been solved in
the meantime.
--
Ticket URL: <https://code.djangoproject.com/ticket/35577>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
4:32 AM (14 hours ago) 4:32 AM
to django-...@googlegroups.com
#35577: runserver leaves a database connection infinitely unclosed as a result of
its migration check
------------------------------------+--------------------------------------
Reporter: Klaas van Schelven | Owner: (none)
Type: Uncategorized | Status: new
Component: Uncategorized | Version: 4.2
Severity: Normal | Resolution:
Keywords: | Triage Stage: Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
------------------------------------+--------------------------------------
Description changed by Klaas van Schelven:

Old description:

> When starting the "Debug server" (runserver command), Django does a check
> on the migrations ("check_migrations").
>
> This check touches the DB (to see which migrations have run)
>
> However, after the call there's no call to `connection.close()`. (or some
> conditional form thereof).
>
> This means that for the debugserver one connection will remain
> permanently open. The name of the thread on which this connection is
> created is `django-main-thread`.
>
> Looking at [https://docs.djangoproject.com/en/5.0/ref/databases
> /#persistent-connections the documentation] the per-request model of
> connection lifetimes is clear. Other than that the following caveat is
> documented:
>
> > If a connection is created in a long-running process, outside of
> Django’s request-response cycle, the connection will remain open until
> explicitly closed, or timeout occurs.
>
> But one would assume that this caveat applies to connections opened by
> the developers themselves, not by what Django does internally.
>
> Here's the call to `check_migrations`:
> https://github.com/django/django/blob/stable/4.2.x/django/core/management/commands/runserver.py#L136
>
> I've seen this behavior on Django 4.2, have not tested it with a more
> recent version yet, but I don't have a reason to think it's been solved
> in the meantime.

New description:

When starting the "Debug server" (runserver command), Django does a check
on the migrations ("check_migrations").

This check touches the DB (to see which migrations have run)

However, after the call there's no call to `connection.close()`. (or some
conditional form thereof).

This means that for the debugserver one connection will remain permanently
open. The name of the thread on which this connection is created is
`django-main-thread`.

Looking at [https://docs.djangoproject.com/en/5.0/ref/databases
/#persistent-connections the documentation] the per-request model of
connection lifetimes is clear. Other than that the following caveat is
documented:

> If a connection is created in a long-running process, outside of
Django’s request-response cycle, the connection will remain open until
explicitly closed, or timeout occurs.

But one would assume that this caveat applies to connections opened by the
developers themselves, not by what Django does internally.

Here's the call to `check_migrations`:
https://github.com/django/django/blob/stable/4.2.x/django/core/management/commands/runserver.py#L136

I've seen this behavior on Django 4.2, have not tested it with a more
recent version yet, but I don't have a reason to think it's been solved in
the meantime.

--
--
Ticket URL: <https://code.djangoproject.com/ticket/35577#comment:1>

Django

unread,
6:31 AM (12 hours ago) 6:31 AM
to django-...@googlegroups.com
#35577: runserver leaves a database connection infinitely unclosed as a result of
its migration check
-------------------------------------+-------------------------------------
Reporter: Klaas van Schelven | Owner: (none)
Type: Bug | Status: closed
Component: Database layer | Version: 4.2
(models, ORM) |
Severity: Normal | Resolution: invalid
Keywords: | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Mariusz Felisiak):

* component: Uncategorized => Database layer (models, ORM)
* resolution: => invalid
* status: new => closed
* type: Uncategorized => Bug

Comment:

Django 4.2 is in extended support and no longer receives bugfixes (except
security patches). Moreover, it was fixed in Django 5.1
(bcccea3ef31c777b73cba41a6255cd866bf87237).
--
Ticket URL: <https://code.djangoproject.com/ticket/35577#comment:2>

Django

unread,
7:11 AM (11 hours ago) 7:11 AM
to django-...@googlegroups.com
#35577: runserver leaves a database connection infinitely unclosed as a result of
its migration check
-------------------------------------+-------------------------------------
Reporter: Klaas van Schelven | Owner: (none)
Type: Bug | Status: closed
Component: Database layer | Version: 4.2
(models, ORM) |
Severity: Normal | Resolution: invalid
Keywords: | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Comment (by Klaas van Schelven):

> it was fixed in Django 5.1

I'm glad it was, this is indeed the fix.

I still can't find any other bug report leading up to the fix so perhaps
the present ticket can lead others to the fix.
--
Ticket URL: <https://code.djangoproject.com/ticket/35577#comment:3>
Reply all
Reply to author
Forward
0 new messages