#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.


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

 def sync_view(request):
     # gc.collect()
     # plot_refs()
     return HttpResponse(f"Number of BaseDatabaseWrapper instances:

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

 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

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 

Reply via email to