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

Fix static _handler pickling error. #1842

Merged
merged 3 commits into from
Jun 3, 2020

Conversation

ashleysommer
Copy link
Member

Moves the subfunction _handler out to a module-level function, and parameterizes it with functools.partial().
Fixes the case when picking a sanic app which has a registered static route handler. This is usually encountered when attempting to use multiprocessing or auto_reload on OSX or Windows.
Fixes #1774

Moves the subfunction _handler out to a module-level function, and parameterizes it with functools.partial().
Fixes the case when picking a sanic app which has a registered static route handler. This is usually encountered when attempting to use multiprocessing or auto_reload on OSX or Windows.
Fixes sanic-org#1774
@codecov
Copy link

codecov bot commented May 7, 2020

Codecov Report

Merging #1842 into master will increase coverage by 0.00%.
The diff coverage is 92.15%.

Impacted file tree graph

@@           Coverage Diff           @@
##           master    #1842   +/-   ##
=======================================
  Coverage   92.17%   92.17%           
=======================================
  Files          23       23           
  Lines        2312     2314    +2     
  Branches      424      424           
=======================================
+ Hits         2131     2133    +2     
  Misses        141      141           
  Partials       40       40           
Impacted Files Coverage Δ
sanic/static.py 93.93% <92.15%> (+0.18%) ⬆️

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 7c04c9a...7c3c532. Read the comment docs.

@Tronic
Copy link
Member

Tronic commented May 7, 2020

This fixes static files but unfortunately there are also problems with route decorators:

# @app.get(...)
_pickle.PicklingError: Can't pickle <function my_handler at 0x10b2129d0>: it's not the same object as __main__.my_handler

# @app.websocket(...)
AttributeError: Can't pickle local object 'Sanic.websocket.<locals>.response.<locals>.websocket_handler'

This can be tried on UNIX by changing sanic/server.py:930 to

    mp = multiprocessing.get_context("spawn")

@ashleysommer
Copy link
Member Author

Ah, ok. I was testing on Linux and not seeing that problem. Thanks for the tip @Tronic

@ashleysommer
Copy link
Member Author

This can probably be still merged, as it does fix the static _handler pickle error, and adds a new test for that problem.
The route decorator pickle problem can probably be created as a secondary issue and receive an additional fix.

Copy link
Member

@Tronic Tronic left a comment

Choose a reason for hiding this comment

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

Fixes a problem and adds a test for it.

Further changes to be done in other PRs (don't pickle app, or fix the route decorators as well, or enable fork mode multiprocessing with 19.12LTS on MacOS py38). Windows cannot use fork mode but also has multiple worker support completely disabled for now.

@ashleysommer
Copy link
Member Author

I was hoping to get this change into the (belated) 20.03 release. Looks like it didn't make it. Still says one more review is required?

@ashleysommer
Copy link
Member Author

ashleysommer commented May 14, 2020

Oh, when I saw the 20.3 PR go through yesterday, I thought we had done the 20.3 release. We should merge this now and it might still make the release.

@ashleysommer
Copy link
Member Author

ashleysommer commented May 14, 2020

@Tronic I've made a fix for the websocket_handler pickle error, I will open a PR with that shortly. However I still cannot reproduce the route decorator pickling error.
I've changed the mp context to 'spawn' as you suggested, and even force-picking the whole app with decorated routes, and cannot get it to fail with Can't pickle <function handler at 0x0>: it's not the same object as __main__.handler error.

Are you able to share some code which reproduces the pickling error with route decorators?

@ahopkins
Copy link
Member

ahopkins commented Jun 3, 2020

This can me merged?

@ashleysommer
Copy link
Member Author

Yes, it's finished and approved and is ready to be merged.

@ahopkins ahopkins merged commit 4658e0f into sanic-org:master Jun 3, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
3 participants