[Django] #36435: CaptureQueriesContext can miss queries after nesting

7 views
Skip to first unread message

Django

unread,
Jun 3, 2025, 6:16:05 PM6/3/25
to django-...@googlegroups.com
#36435: CaptureQueriesContext can miss queries after nesting
-------------------------------------+-------------------------------------
Reporter: Adam | Owner: Adam Johnson
Johnson |
Type: Bug | Status: assigned
Component: Testing | Version: dev
framework |
Severity: Normal | Keywords:
Triage Stage: | Has patch: 1
Unreviewed |
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 0 |
-------------------------------------+-------------------------------------
`CaptureQueriesContext.__exit__()`, as used by `assertNumQueries()`,
unconditionally restores the `reset_queries` signal receiver. This
behaviour can lead to missed queries when the context manager is nested
and a request is made after the inner context manager exits.

The solution is to restore the signal receiver in `__exit__()` only if it
was removed in `__enter__()`.
--
Ticket URL: <https://code.djangoproject.com/ticket/36435>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Jun 3, 2025, 9:51:02 PM6/3/25
to django-...@googlegroups.com
#36435: CaptureQueriesContext can miss queries after nesting
-------------------------------------+-------------------------------------
Reporter: Adam Johnson | Owner: Adam
| Johnson
Type: Bug | Status: assigned
Component: Testing framework | Version: dev
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Simon Charette):

* stage: Unreviewed => Accepted

Comment:

Thanks Adam, patch LGTM as well.
--
Ticket URL: <https://code.djangoproject.com/ticket/36435#comment:1>

Django

unread,
Jun 5, 2025, 3:51:40 AM6/5/25
to django-...@googlegroups.com
#36435: CaptureQueriesContext can miss queries after nesting
-------------------------------------+-------------------------------------
Reporter: Adam Johnson | Owner: Adam
| Johnson
Type: Bug | Status: assigned
Component: Testing framework | Version: dev
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 1 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Sarah Boyce):

* needs_tests: 0 => 1

--
Ticket URL: <https://code.djangoproject.com/ticket/36435#comment:2>

Django

unread,
Jun 5, 2025, 5:30:08 AM6/5/25
to django-...@googlegroups.com
#36435: CaptureQueriesContext can miss queries after nesting
-------------------------------------+-------------------------------------
Reporter: Adam Johnson | Owner: Adam
| Johnson
Type: Bug | Status: assigned
Component: Testing framework | Version: dev
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Adam Johnson):

* needs_tests: 1 => 0

--
Ticket URL: <https://code.djangoproject.com/ticket/36435#comment:3>

Django

unread,
Jun 5, 2025, 6:47:38 AM6/5/25
to django-...@googlegroups.com
#36435: CaptureQueriesContext can miss queries after nesting
-------------------------------------+-------------------------------------
Reporter: Adam Johnson | Owner: Adam
| Johnson
Type: Bug | Status: assigned
Component: Testing framework | Version: dev
Severity: Normal | Resolution:
Keywords: | Triage Stage: Ready for
| checkin
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Sarah Boyce):

* stage: Accepted => Ready for checkin

--
Ticket URL: <https://code.djangoproject.com/ticket/36435#comment:4>

Django

unread,
Jun 5, 2025, 7:49:49 AM6/5/25
to django-...@googlegroups.com
#36435: CaptureQueriesContext can miss queries after nesting
-------------------------------------+-------------------------------------
Reporter: Adam Johnson | Owner: Adam
| Johnson
Type: Bug | Status: assigned
Component: Testing framework | Version: dev
Severity: Normal | Resolution:
Keywords: | Triage Stage: Ready for
| checkin
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Comment (by Sarah Boyce <42296566+sarahboyce@…>):

In [changeset:"9a3f3b84999878e22021c92f9102707ba2648209" 9a3f3b84]:
{{{#!CommitTicketReference repository=""
revision="9a3f3b84999878e22021c92f9102707ba2648209"
Refs #36435 -- Tidied tests for CaptureQueriesContext and
assertNumQueries.

Avoided repeatedly templating the URL and hoisted Person creation to
setUpTestData in AssertNumQueriesContextManagerTests to repeat the same
pattern as in CaptureQueriesContextManagerTests.
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/36435#comment:5>

Django

unread,
Jun 5, 2025, 7:49:51 AM6/5/25
to django-...@googlegroups.com
#36435: CaptureQueriesContext can miss queries after nesting
-------------------------------------+-------------------------------------
Reporter: Adam Johnson | Owner: Adam
| Johnson
Type: Bug | Status: closed
Component: Testing framework | Version: dev
Severity: Normal | Resolution: fixed
Keywords: | Triage Stage: Ready for
| checkin
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Sarah Boyce <42296566+sarahboyce@…>):

* resolution: => fixed
* status: assigned => closed

Comment:

In [changeset:"f0a87895ffaf6532a22143b5e2e304c59b7958ae" f0a87895]:
{{{#!CommitTicketReference repository=""
revision="f0a87895ffaf6532a22143b5e2e304c59b7958ae"
Fixed #36435 -- Made CaptureQueriesContext restore reset_queries
conditionally.
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/36435#comment:6>
Reply all
Reply to author
Forward
0 new messages