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

Root app middleware receives wrong app context #2550

Merged
merged 5 commits into from
Nov 27, 2017

Conversation

popravich
Copy link
Member

Long story short

When handling request to sub-application root application's middlewares see wrong
request.app instance — sub-app instead of the one they attached to.

This PR demonstrates it (adds failing test) but has no fix yet.

@popravich popravich changed the title [WIP] Root app middleware receives wrong app context Root app middleware receives wrong app context Nov 23, 2017
@asvetlov
Copy link
Member

Is it related to #2454 ?

@popravich
Copy link
Member Author

not sure

@popravich
Copy link
Member Author

request.app property always returns the last app in match_info.apps list.
We need to fix this.

@popravich
Copy link
Member Author

So, the fix is as following:

  • make request.app normal property, not reify'ed attribute;
  • make request.app return match_info.current_app
  • add match_info.current_app property which, by default, returns last app (same as request.app did).
  • add a context manager to match_info that updates current_app;
  • update application's _prepare_middleware method to append one extra middleware
    _fix_request_current_app that switches current application in match info and, as a result, in request.

Whew... profit!

Guys, your thoughts?

@codecov-io
Copy link

codecov-io commented Nov 27, 2017

Codecov Report

Merging #2550 into master will increase coverage by 0.01%.
The diff coverage is 100%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master    #2550      +/-   ##
==========================================
+ Coverage   97.55%   97.57%   +0.01%     
==========================================
  Files          40       40              
  Lines        8057     8078      +21     
  Branches     1409     1411       +2     
==========================================
+ Hits         7860     7882      +22     
+ Misses         75       74       -1     
  Partials      122      122
Impacted Files Coverage Δ
aiohttp/web_urldispatcher.py 99.15% <100%> (+0.01%) ⬆️
aiohttp/web_middlewares.py 100% <100%> (ø) ⬆️
aiohttp/web.py 97.87% <100%> (+0.38%) ⬆️
aiohttp/web_request.py 99.68% <100%> (ø) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update bbefc20...91cc566. Read the comment docs.

@asvetlov
Copy link
Member

Adding a new middleware is not ideal maybe but I can live with it -- an extra middleware is better than broken functionality.
We can invite a better fix later anyway.

@asvetlov asvetlov merged commit 0710570 into master Nov 27, 2017
@asvetlov asvetlov deleted the nested_apps_with_middleware branch November 27, 2017 22:11
asvetlov pushed a commit that referenced this pull request Nov 27, 2017
* add failing test showing invalid request.app in middleware

* implement UrlMappingMatchInfo.current_app attribute with context manger setter

* fix bad inlined-inlined-loop)

* Add changes
asvetlov pushed a commit that referenced this pull request Nov 27, 2017
* add failing test showing invalid request.app in middleware

* implement UrlMappingMatchInfo.current_app attribute with context manger setter

* fix bad inlined-inlined-loop)

* Add changes
@SoumenDass
Copy link
Contributor

SoumenDass commented Nov 30, 2017

This change has broken existing functionality e.g. earlier within a view in a sub-application, I could always do request.app and get the sub-application instance. Now it always returns the main application instance which basically defeats the purpose of sub-application. E.g. if I have a routes defined in the sub-application, earlier they used to work fine via request.app.router['<route_name>']. Now since request.app always returns the main app, it is not aware of the subapplication route names and errors out:

    rpt_url = request.app.router['getreport'].url_for(rpt_id=rpt_id)
  File "/home/sdass/apivenv/lib/python3.6/site-packages/aiohttp/web_urldispatcher.py", line 851, in __getitem__
    return self._named_resources[name]
KeyError: 'getreport'

@popravich
Copy link
Member Author

Hi, @SoumenDass ,
does your applications (main and sub) have any middlewares setup?

@SoumenDass
Copy link
Contributor

All my middlewares are in the main app

@popravich
Copy link
Member Author

ok, thanks, I will check your issue

@SoumenDass
Copy link
Contributor

Thanks - for now, I've rolled back my deployment to 2.3.3 where this issue does not happen.

@popravich
Copy link
Member Author

I've just updated the test and made a fix.
Indeed, if sub-app has no middleware it was accessing main app instance.

asvetlov pushed a commit that referenced this pull request Dec 4, 2017
* yield _fix_request_current_app middleware uncoditionally; update test; (see #2550)

* Add change notes
asvetlov pushed a commit that referenced this pull request Dec 4, 2017
* yield _fix_request_current_app middleware uncoditionally; update test; (see #2550)

* Add change notes
@lock
Copy link

lock bot commented Oct 28, 2019

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a [new issue] for related bugs.
If you feel like there's important points made in this discussion, please include those exceprts into that [new issue].
[new issue]: https://github.com/aio-libs/aiohttp/issues/new

@lock lock bot added the outdated label Oct 28, 2019
@lock lock bot locked as resolved and limited conversation to collaborators Oct 28, 2019
@psf-chronographer psf-chronographer bot added the bot:chronographer:provided There is a change note present in this PR label Oct 28, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bot:chronographer:provided There is a change note present in this PR bug outdated server
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants