﻿id	summary	reporter	owner	description	type	status	component	version	severity	resolution	keywords	cc	stage	has_patch	needs_docs	needs_tests	needs_better_patch	easy	ui_ux
35577	runserver leaves a database connection infinitely unclosed as a result of its migration check	Klaas van Schelven		"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."	Bug	closed	Database layer (models, ORM)	4.2	Normal	invalid			Unreviewed	0	0	0	0	0	0
