#34865: DatabaseWrapper are not GC and connections are not closed
-------------------------------------+-------------------------------------
     Reporter:  Fábio Domingues      |                    Owner:  nobody
         Type:  Bug                  |                   Status:  new
    Component:  Database layer       |                  Version:  4.2
  (models, ORM)                      |
     Severity:  Normal               |               Resolution:
     Keywords:                       |             Triage Stage:  Accepted
    Has patch:  0                    |      Needs documentation:  0
  Needs tests:  0                    |  Patch needs improvement:  0
Easy pickings:  0                    |                    UI/UX:  0
-------------------------------------+-------------------------------------

Comment (by Fábio Domingues):

 Tested using Uvicorn and Daphne, the database engine should not matter, I
 tested with PostgreSQL, `CONN_MAX_AGE` could be any value and the effect
 is the same using ''async'' or ''sync'' views.

 {{{#!python

 get_refs = lambda: [o for o in gc.get_objects() if isinstance(o,
 BaseDatabaseWrapper)]
 plot_refs = lambda: objgraph.show_backrefs(get_refs(), filename="sample-
 graph.png")

 def sync_view(request):
     MyModel.objects.count()
     # gc.collect()
     # plot_refs()
     return HttpResponse(f"Number of BaseDatabaseWrapper instances:
 {len(get_refs())}")

 async def async_view(request):
     await MyModel.objects.acount()
     # gc.collect()
     # plot_refs()
     return HttpResponse(f"Number of BaseDatabaseWrapper instances:
 {len(get_refs())}")
 }}}

 On each refresh you should see the number of instances increase. If you
 plot the references you would see many diagrams like the one in the
 attachment. Throwing `gc.collect()` actually get it, but it take some
 milliseconds, even if the underlying database connection is already
 closed.

-- 
Ticket URL: <https://code.djangoproject.com/ticket/34865#comment:5>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-updates+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/0107018abd249ab6-09f65099-17cf-417b-a230-55b822ecd29c-000000%40eu-central-1.amazonses.com.

Reply via email to