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

Please help! #2

Closed
qwexvf opened this issue Mar 17, 2017 · 16 comments
Closed

Please help! #2

qwexvf opened this issue Mar 17, 2017 · 16 comments
Labels

Comments

@qwexvf
Copy link

qwexvf commented Mar 17, 2017

i got some issue!

ERROR:sanic:Traceback (most recent call last):
  File "/home/xyz/.pyenv/versions/sanic/lib/python3.6/site-packages/sanic/app.py", line 455, in handle_request
    _response = await _response
  File "/home/xyz/.pyenv/versions/sanic/lib/python3.6/site-packages/sanic_cors/extension.py", line 181, in cors_response_middleware
    if resp.headers.get(ACL_ORIGIN):
AttributeError: 'NoneType' object has no attribute 'headers'

Thanks!

@ashleysommer
Copy link
Owner

Hi @qwexvf
I will look into this for you.
Are you able to give any more details of the problem? Can you write a small application that reproduces this error?
I see you are on python 3.6. I don't test on 3.6 because I only have 3.5 installed. It might be 3.6 related, but it might be a bug in my extension.

@ashleysommer
Copy link
Owner

Ok, it looks as if somehow response.headers is set to None when the CORS extension is called.

response.headers should never be None. If no headers are passed to a response creator, sanic sets headers to {}, empty dict.

If you are explicitly setting response.headers = None somewhere in your code, that will break most extensions.

@ashleysommer
Copy link
Owner

Closing as invalid

@qwexvf
Copy link
Author

qwexvf commented Apr 14, 2017

No, I didn't set to None,

2017-04-15 04:52:06,147: ERROR: Traceback (most recent call last):
  File "/home/xyz/.pyenv/versions/sanic/lib/python3.6/site-packages/sanic/app.py", line 471, in handle_request
    response = handler(request, *args, **kwargs)
  File "/home/xyz/.pyenv/versions/sanic/lib/python3.6/site-packages/sanic_cors/decorator.py", line 134, in wrapped_function
    set_cors_headers(req, resp, options)
  File "/home/xyz/.pyenv/versions/sanic/lib/python3.6/site-packages/sanic_cors/core.py", line 235, in set_cors_headers
    if not isinstance(resp.headers, CIDict):
AttributeError: 'coroutine' object has no attribute 'headers'

Now i got this!

@qwexvf
Copy link
Author

qwexvf commented Apr 14, 2017

And on the browser console i got this

XMLHttpRequest cannot load http://localhost:8000/api/auth/request-token. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://192.168.1.14:8080' is therefore not allowed access. The response had HTTP status code 404.

any advice ?

@qwexvf
Copy link
Author

qwexvf commented Apr 14, 2017

Sorry ignore all of them. i was running the examples which those routes does not exist lol.
sorry i was so dumb.

Nice extension <3 keep it up!

Edit:
Btw, i still have

2017-04-15 05:11:26,479: ERROR: NoneType: None

2017-04-15 05:13:24,015: ERROR: Traceback (most recent call last):
  File "/home/xyz/.pyenv/versions/sanic/lib/python3.6/site-packages/sanic/app.py", line 483, in handle_request
    _response = await _response
  File "/home/xyz/.pyenv/versions/sanic/lib/python3.6/site-packages/sanic_cors/extension.py", line 181, in cors_response_middleware
    if resp.headers.get(ACL_ORIGIN):
AttributeError: 'NoneType' object has no attribute 'headers'

@r0fls
Copy link

r0fls commented May 14, 2017

@qwexvf can you make sure you're using the latest version of this module? File "/home/xyz/.pyenv/versions/sanic/lib/python3.6/site-packages/sanic_cors/extension.py", line 181, in cors_response_middleware if resp.headers.get(ACL_ORIGIN): that line is apparently occurring at 237 now (not 181) https://github.com/ashleysommer/sanic-cors/blob/master/sanic_cors/extension.py#L237 so it's quite hard to debug...

@ashleysommer
Copy link
Owner

ashleysommer commented May 14, 2017

Im going to reopen this issue,
Even though I cannot reproduce it, I believe it may affect others now or in the future.

I think it has something to do with the extension attempting to modify the headers of an Exception response.

@ashleysommer ashleysommer reopened this May 14, 2017
@qwexvf
Copy link
Author

qwexvf commented May 15, 2017

@r0fls since now im using cors to specific resource i'm not having this issue anymore.
not sure but maybe the problem was the websockets..

@ashleysommer
Copy link
Owner

ashleysommer commented May 16, 2017

I believe this issue was created before Websockets were added to Sanic.
It was not the cause for the original problem.

It is possible that when this issue was opened, the error was originally caused by using the example code on routes which did not exist, and now the same error is seen when Websockets are used with Sanic-CORS.

@ashleysommer
Copy link
Owner

This should be fixed by: 6d88deb
This is released on version 0.5.4.1
I also found a versioning bug which means that updating using pip might (from the pypi dist) might not have been working correctly. This is fixed in 0.5.4.1 so you should be able to upgrade correctly now.

@vromanov
Copy link

Hi all,
Have the same problem on latest sanic + sanic_cors
Error is AttributeError: 'NoneType' object has no attribute 'headers'.
It means that response is None and it happens on websocket handler.
6d88deb doesn't fix the issue because it checks resp.headers for None instead of resp.
I think it shoud be code like
if resp is None: return
in cors_response_middleware function.

sanic version: 0.6.0
sanic_cors: 0.6.0.0
error:
Exception occured in one of response middleware handlers Traceback (most recent call last): File "/usr/local/lib/python3.6/site-packages/sanic/app.py", line 527, in handle_request response) File "/usr/local/lib/python3.6/asyncio/coroutines.py", line 109, in __next__ return self.gen.send(None) File "/usr/local/lib/python3.6/site-packages/sanic/app.py", line 660, in _run_response_middleware _response = await _response File "/usr/local/lib/python3.6/asyncio/coroutines.py", line 109, in __next__ return self.gen.send(None) File "/usr/local/lib/python3.6/site-packages/sanic_cors/extension.py", line 256, in cors_response_middleware set_cors_headers(req, resp, res_options) File "/usr/local/lib/python3.6/site-packages/sanic_cors/core.py", line 237, in set_cors_headers if not isinstance(resp.headers, CIDict): AttributeError: 'NoneType' object has no attribute 'headers'

@ashleysommer
Copy link
Owner

Thanks for the report.

I am aware sanic-cors does not currently work on websocket connections.

I can push a patch later today to fix that crash.

ashleysommer added a commit that referenced this issue Aug 29, 2017
in the case of using websockets
Fix other edge cases where `response` might be `None` and don't try to apply CORS to these cases
Bump to version 0.6.0.1
@ashleysommer
Copy link
Owner

@vromanov
I've released sanic-cors v 0.6.0.1
Can you please test if this fixes your issue.

@vromanov
Copy link

Thanks for the quick support! It works perfectly!

@boboldehampsink
Copy link

boboldehampsink commented Feb 19, 2018

I still see this issue on the latest version. I'm using a websocket.

ERROR:sanic.error:Exception occurred in one of response middleware handlers
Traceback (most recent call last):
  File "/usr/local/lib/python3.6/site-packages/sanic/app.py", line 597, in handle_request
    response = await response
GeneratorExit

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/usr/local/lib/python3.6/site-packages/sanic/app.py", line 597, in handle_request
    response = await response
  File "/usr/local/lib/python3.6/site-packages/sanic/app.py", line 781, in _run_response_middleware
    _response = await _response
  File "/usr/local/lib/python3.6/site-packages/spf/framework.py", line 501, in run_bp_post_response_mw
    _response = await _response
  File "/usr/local/lib/python3.6/site-packages/sanic_cors/extension.py", line 306, in cors_response_middleware
    set_cors_headers(req, resp, context, res_options)
  File "/usr/local/lib/python3.6/site-packages/sanic_cors/core.py", line 247, in set_cors_headers
    if not isinstance(resp.headers, CIDict):
AttributeError: 'coroutine' object has no attribute 'headers'

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

5 participants