Repository navigation
Fix set_rollback for non_atomic_requests views - #10078
Open
majidkhazaei wants to merge 13 commits into
Open
majidkhazaei wants to merge 13 commits into
majidkhazaei wants to merge 13 commits into
Conversation
\## Description Fixes encode#6921. Added tests that fail before and pass afterwards. Remove the check for `connection.in_atomic_block` to determine if the current request is under a `transaction.atomic` from `ATOMIC_REQUESTS`. Instead, duplicate the method that Django itself uses [in BaseHandler](https://github.com/django/django/blob/964dd4f4f208722d8993a35c1ff047d353cea1ea/django/core/handlers/base.py#L64). This requires fetching the actual view function from `as_view()`, as seen by the URL resolver / BaseHandler. Since this requires `request`, I've also changed the accesses in `get_exception_handler_context` to be direct attribute accesses rather than `getattr()`. It seems the `getattr` defaults not accessible since `self.request`, `self.args`, and `self.kwargs` are always set in `dispatch()` before `handle_exception()` can ever be called. This is useful since `request` is always needed for the new `set_rollback` logic.
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
# Conflicts: # rest_framework/views.py # tests/test_atomic_requests.py
set_rollback() now mirrors Django's BaseHandler.make_view_atomic() logic: when a request is provided, it reads the resolved view's _non_atomic_requests attribute and skips databases that were explicitly excluded from ATOMIC_REQUESTS. This prevents DRF from marking outer transactions (e.g. Django's TestCase wrapper) for rollback when an APIException is raised from a @non_atomic_requests view. The previous signature set_rollback() is deprecated and will emit a RemovedInDRF321Warning. It falls back to the old ATOMIC_REQUESTS + in_atomic_block behavior. Refs encode#6921, encode#6922.
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
This PR re-implements the fix for #6921, superseding the work in #6922.
Per @auvipy's suggestion, this is a fresh PR directly against
maininstead of continuing on the (now quite stale)
#6922branch.What this changes
set_rollback(request=None)— Mirrors Django'sBaseHandler.make_view_atomic()logic. When arequestisprovided, it reads the resolved view's
_non_atomic_requestsattribute and skips databases that were explicitly excluded
from
ATOMIC_REQUESTS.RemovedInDRF321Warning— The old no-arg signature isdeprecated but still works (best-effort fallback to the
previous behavior).
exception_handler— Now passescontext.get('request')to
set_rollback().Tests:
NonAtomicViewUnderTestCaseTests— proper regression testusing
TestCase(notTransactionTestCase), addressingCopilot's feedback that the previous test didn't actually
reproduce the bug.
SetRollbackDeprecationTests— verifies the deprecationwarning.
SetRollbackWithoutResolverMatchTests— verifies thefallback when
resolver_matchis missing(
APIRequestFactory).requestargument.Why this is different from the current state of #6922
TestCaseinstead ofTransactionTestCasefor theregression test, so it actually fails without the fix.
deprecation warning.
resolver_matchis unavailable.adamchainz:fix_issue_6921_atomic_requests.Credit
Original design and analysis: @adamchainz, from the discussion
in #6921 and #6922. Thanks also to @auvipy and @lovelydinosaur
for the guidance.
Testing
All 11 tests in
tests/test_atomic_requests.pypass.Pre-commit is green.
Closes #6921.
Supersedes #6922.