Opened 2 years ago

Closed 5 weeks ago

#22414 closed Bug (fixed)

Persistent connections not closed by LiveServerTestCase, preventing dropping test databases

Reported by: Koterpillar Owned by: Chris Jerdonek
Component: Testing framework Version: 1.6
Severity: Normal Keywords:
Cc: chris.jerdonek@… Triage Stage: Ready for checkin
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

Whenever a PostgreSQL connection is marked as persistent (CONN_MAX_AGE = None) and a LiveServerTestCase is executed, the connection from the server thread is never closed, leading to inability to drop the test database.

Attachments (1)

patch (1.7 KB) - added by Koterpillar 2 years ago.
test and patch

Download all attachments as: .zip

Change History (16)

comment:1 Changed 2 years ago by Koterpillar

Has patch: set
Needs documentation: unset
Needs tests: unset
Patch needs improvement: unset

comment:2 Changed 2 years ago by Claude Paroz

Component: UncategorizedTesting framework
Triage Stage: UnreviewedAccepted
Type: UncategorizedBug

comment:3 Changed 2 years ago by anonymous

We are using only TestCase and TransactionTestCases in our code. CONN_MAX_AGE is set to 180.

comment:4 Changed 2 years ago by Jake Rothenbuhler

Owner: changed from nobody to Jake Rothenbuhler
Status: newassigned

comment:5 Changed 2 years ago by Jake Rothenbuhler

Owner: Jake Rothenbuhler deleted
Status: assignednew

comment:6 Changed 2 years ago by Claude Paroz

Patch needs improvement: set

The test is currently failing for me, as wrapped_close seems to be called after the test assertion.

I also wonder if closing the connections should happen in LiveServerTestCase._tearDownClassInternal, but then it will be even more difficult to test :-(

Changed 2 years ago by Koterpillar

Attachment: patch added

test and patch

comment:7 Changed 2 years ago by Koterpillar

Patch needs improvement: unset

It turns out the patch was not closing _all_ connections properly - just whatever happened to be in conn. Fixed.

comment:8 Changed 2 years ago by Mathieu Agopian

Patch needs improvement: set

This patch is still not working for me on an up to date master.

On a side note, it might be related to #22420 and #22003.

comment:9 Changed 2 years ago by Koterpillar

Can you share the results you are getting, along with the environment? I'm always getting a success.

comment:10 Changed 3 months ago by Chris Jerdonek

I also ran into this issue with Django 1.9.7.

What I observed is that with CONN_MAX_AGE=None, after LiveServerTestCase.tearDownClass(), querying PostgreSQL's pg_stat_activity shows a lingering connection in state "idle" (which was a connection created by the test case).

It looks like the culprit may be Django's LiveServerThread. The idle connections are not closed when the thread terminates. It looks like that's what the most recent patch proposes.

Other info:

PostgreSQL 9.3.13, psycopg2 2.6.2, Python 3.5.2.

comment:11 Changed 3 months ago by Chris Jerdonek

Cc: chris.jerdonek@… added

comment:12 Changed 6 weeks ago by Chris Jerdonek

Owner: set to Chris Jerdonek
Status: newassigned

comment:13 Changed 6 weeks ago by Chris Jerdonek

I posted a pull request for this here. Also, I confirmed on the CI servers that the test I added fails on six of the build configurations (mysql, postgres, and postgis) without the change.

Last edited 6 weeks ago by Chris Jerdonek (previous) (diff)

comment:14 Changed 5 weeks ago by Tim Graham

Patch needs improvement: unset
Triage Stage: AcceptedReady for checkin

comment:15 Changed 5 weeks ago by Tim Graham <timograham@…>

Resolution: fixed
Status: assignedclosed

In f6cd669f:

Fixed #22414 -- Ensured that LiveServerTestCase closes connections.

Note: See TracTickets for help on using tickets.
Back to Top