Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

use new style middleware and catch exceptions to be sure to clear request in context #1188

Merged
merged 6 commits into from
Aug 4, 2023

Conversation

manelclos
Copy link
Member

@manelclos manelclos commented Jun 12, 2023

Description

We caught a problem were the request was reused, causing a strange random behaviour:

  • a test makes a request, so the context is updated
  • an exception happens and process_response is never called, context still contains WRONG request
  • the next test creates some versioned model, and the WRONG request is used

Related Issue

Resolves #1189.

How Has This Been Tested?

Extensive pytest codebase using django-simple-history
Fixes the problem we were facing

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)

Checklist:

  • I have run the pre-commit run command to format and lint.
  • My change requires a change to the documentation.
  • I have updated the documentation accordingly.
  • I have read the CONTRIBUTING document.
  • I have added tests to cover my changes.
  • I have added my name and/or github handle to AUTHORS.rst
  • I have added my change to CHANGES.rst
  • All new and existing tests passed.

also, catch exceptions to be sure to clear request in context
@codecov
Copy link

codecov bot commented Aug 3, 2023

Codecov Report

Merging #1188 (3f46755) into master (60466d6) will increase coverage by 0.08%.
The diff coverage is 100.00%.

@@            Coverage Diff             @@
##           master    #1188      +/-   ##
==========================================
+ Coverage   97.30%   97.38%   +0.08%     
==========================================
  Files          23       23              
  Lines        1260     1263       +3     
  Branches      204      204              
==========================================
+ Hits         1226     1230       +4     
  Misses         16       16              
+ Partials       18       17       -1     
Files Changed Coverage Δ
simple_history/middleware.py 100.00% <100.00%> (+11.11%) ⬆️

📣 We’re building smart automated test selection to slash your CI/CD build times. Learn more

Added a changelog entry, and removed the
"Using django-webtest with Middleware" section in `common_issues.rst`,
as it's not an issue anymore now that `HistoricalRecords.context.request`
is deleted in all cases (due to using a `finally` block).
@ddabble ddabble marked this pull request as ready for review August 3, 2023 17:11
@ddabble
Copy link
Member

ddabble commented Aug 3, 2023

@manelclos I added an exception variable to your code, in addition to adding a test and updating the documentation 🙂

This code was added in 341aec1 - when
`HistoryRequestMiddleware` didn't delete the
`HistoricalRecords.context.request` variable. The middleware class does
now, so this can be removed.
ddabble
ddabble previously approved these changes Aug 3, 2023
Copy link
Member

@ddabble ddabble left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great, thank you! 😊

Do review the changes I pushed, if you have any feedback :) Otherwise, I'll merge in a week's time.

@manelclos
Copy link
Member Author

Hi @ddabble all good! I didn't notice the error was documented already. Thanks for adding the tests and documentation.

Now it also tests that the `request` attribute of
`HistoricalRecords.context` actually exists while handling each request.
@ddabble ddabble merged commit 2e3e325 into master Aug 4, 2023
36 checks passed
@ddabble ddabble deleted the new-style-middleware branch August 4, 2023 16:06
@Tenzer Tenzer mentioned this pull request Sep 15, 2023
11 tasks
ddabble added a commit that referenced this pull request Sep 15, 2023
* Handle if `request` doesn't exist on the context in middleware

The change in #1188
removed a check for if the request context had a "request" entry in it before
trying to delete the request data.

This has a side effect of breaking tests in order projects in very specific
circumstances, where multiple requests pass through the middleware,
concurrently, inside the same thread, as it means the middleware then may try
to remove the request key on a context where it already has been removed.

This shouldn't have any impact in real life scenarios.

* Add changelog entry

* Improvement of changelog for #1256

Also added a missing reference to the previously merged PR.

---------

Co-authored-by: Anders <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

HistoryRequestMiddleware doesn't clean up after itself under some circumstances
2 participants