#17251: select_for_update tests threads should close connections manually -------------------------------------+------------------------------------- Reporter: akaariai | Owner: nobody Type: | Status: new Cleanup/optimization | Version: Component: Uncategorized | Resolution: Severity: Normal | Triage Stage: Keywords: | Unreviewed Has patch: 1 | Needs documentation: 0 Needs tests: 0 | Patch needs improvement: 0 Easy pickings: 0 | UI/UX: 0 -------------------------------------+------------------------------------- Changes (by akaariai):
* needs_better_patch: => 0 * needs_tests: => 0 * needs_docs: => 0 Comment: The GCed part of the description is somewhat wrong. Threadlocals do not get GCed when the thread goes away. However it seems they do get GCed if the thread has gone away and _another_ thread is then accessing the same thread-local object. Or at least this is my working hypothesis based on the fact that the connection gets closed on this line in the transactions.py, managed() [L111]: {{{ connection.managed(flag) }}} And this is ran from main thread! I assume this is the first time the default_db_alias' DatabaseWrapper object (which is thread-local) is used after the another thread has gone away. I assume Python GCes the state of the other thread because it has gone away, and it sees it only when the object is accessed. If you want to try tracking this down, I have a branch where you can run: {{{ python runtests.py --settings=test_pgsql select_for_update.SelectForUpdateTests.test_nowait_raises_error_on_block }}} Then wait until you get into PDB. Begin stepping through the code. Note when the 'I got deleted' for thread 1 is printed. Note also which thread you are currently in. It is also good to use postgresql and set log_connections / log_disconnections to ON, and also log at least the backend pid in the log_line_prefix (%p). I think this enforces the point that it would be good to close the connection in the tester threads. The current behavior seems to be based on pure luck :) I am also beginning to see why some of the core developers don't like thread-locals... The test code for this is at: https://github.com/akaariai/django/tree/connection_close -- Ticket URL: <https://code.djangoproject.com/ticket/17251#comment:1> 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 post to this group, send email to django-updates@googlegroups.com. To unsubscribe from this group, send email to django-updates+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-updates?hl=en.