﻿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
30424	Queries within AppConfig.ready() can cause issues with some Django db commands	Rich Rauenzahn	nobody	"I can recreate this in my `AppConfig` simply with the following inside of `ready()`:


{{{
list(MyModel.objects.all())
}}}

If you then run `manage dbshell` you can see the connection still existing after invoking `psql`:


{{{
$ manage dbshell    
Settings: myproj.config.rich.DevelopmentSettings
Expanded display is used automatically.
Null display is ""¤"".
psql (9.2.24)
Type ""help"" for help.

myproj=> SELECT *
FROM pg_stat_activity;
-[ RECORD 1 ]----+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
datid            | 4855283
datname          | myproj
pid              | 2590
usesysid         | 16384
usename          | myproj
application_name | psql
client_addr      | 10.20.72.150
client_hostname  | ¤
client_port      | 53364
backend_start    | 2019-04-29 09:53:14.044483-07
xact_start       | 2019-04-29 09:53:15.280585-07
query_start      | 2019-04-29 09:53:15.280585-07
state_change     | 2019-04-29 09:53:15.28059-07
waiting          | f
state            | active
query            | SELECT *
                 | FROM pg_stat_activity;
-[ RECORD 2 ]----+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
datid            | 4855283
datname          | myproj
pid              | 2589
usesysid         | 16384
usename          | myproj
application_name | 
client_addr      | 10.20.72.150
client_hostname  | ¤
client_port      | 53362
backend_start    | 2019-04-29 09:53:14.013457-07
xact_start       | ¤
query_start      | 2019-04-29 09:53:14.018469-07
state_change     | 2019-04-29 09:53:14.020801-07
waiting          | f
state            | idle
query            | SELECT ...
myproj=> 
}}}

This can cause problems with trying to drop the database from the manage command as the database in still in use by another connection.

Work around seems to be adding this to the end of `ready()`:

{{{
from django.db import connections                                      
connections.close_all()      
}}}

My guess is the underlying fork to `psql` isn't closing all file handles and `psql` is inheriting (but not managing) them."	Uncategorized	new	Uncategorized	1.11	Normal				Unreviewed	0	0	0	0	0	0
