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

Long time responding #147

Closed
serkandaglioglu opened this issue Apr 4, 2016 · 28 comments
Closed

Long time responding #147

serkandaglioglu opened this issue Apr 4, 2016 · 28 comments

Comments

@serkandaglioglu
Copy link

On ajax cross domain request flask respond after a long time . For example 10 seconds.
If I call url from browser it respond fastly, but with cors and on ajax request flask wait a long time.
I'm working on localhost.

@corydolphin
Copy link
Owner

Hmm, that seems strange.

Will you please enable debug logging to help me understand what is going on? Flask-CORS doesn't do much work, so I would be surprised if it was slowing down requests substantially.

Are they eventually being returned successfully? If not, it is possible the browser is rejecting them, and there are information error messages in the console.

@serkandaglioglu
Copy link
Author

I followed console it printed these :

DEBUG:flask_cors.extension:No CORS rule matches

DEBUG:flask_cors.extension:Request to '/api/1/prayertime/country' matches CORS resource '/api/*'. Using options: {'automatic_options': True, 'allow_headers': ['.*'], 'send_wildcard': False, 'intercept_exceptions': True, 'origins': ['.*'], 'vary_header': True, 'supports_credentials': False, 'resources': {'/api/*': {'origins': '*'}}, 'methods': 'DELETE, GET, HEAD, OPTIONS, PATCH, POST, PUT'}

DEBUG:flask_cors.core:The request did not contain an 'Origin' header. This means the browser or client did not request CORS, ensure the Origin Header is set.

DEBUG:flask_cors.core:Settings CORS headers: MultiDict([])

Actually I dont know what is the reason of this problem. For example, the request shows pending the request after ajax start. It is pending until I call the ajax url from another chrome tab. After I call the ajax url from another chrome tab the ajax respond successfully.
It is very strange. I put above the log.

@corydolphin
Copy link
Owner

Looks like CORS finished. What is in the browser's console? Or the HTTP request log?

It appears that the request being made is not actually a cross origin request, otherwise the browser would automatically send an Origin header.

If you open the network request tab in chrome, you can inspect requests.

@corydolphin
Copy link
Owner

Closing this issue for now, let me know if you have more information to help me understand what is going on here.

@dland512
Copy link

dland512 commented Jan 17, 2017

I'm seeing a similar issue with a python REST API that I'm writing. The front end is angular 1.2.28. Often times requests hang for 20 seconds or more before returning. Here is the python code to set up the API:

flask_app = Flask(__name__)
flask_app.config.from_object('config')
CORS(flask_app)
api = BaseApi(flask_app)
db_conn.db = SQLAlchemy()
db_conn.db.init_app(flask_app)

api.add_resource(UnfinishedGamesApi, '/unfinishedgames')

The behavior doesn't seem 100% consistent but it happens much less often when I have the Chrome debugger console open for some reason. When it's closed it happens with more regularity (though still not all the time).

If I remove Flask-CORS from the equation and start Chrome with CORS disabled using the

--args --disable-web-security --user-data-dir

command line arguments, everything works fine, debugger console or not.

All this behavior is consistent with Chrome on MacOS and Windows.

Shown below is the Chrome debugger console window for sample request that took almost 1.5 minutes to respond.

image

Here are the Flask-CORS and Werkzeug logs for that same request:

Thread-1--2017-01-16 22:59:24,763 flask_cors.extension DEBUG Request to '/unfinishedgames' matches CORS resource '/'. Using options: {'methods': 'DELETE, GET, HEAD, OPTIONS, PATCH, POST, PUT', 'supports_credentials': False, 'automatic_options': True, 'vary_header': True, 'resources': '/', 'always_send': True, 'max_age': None, 'origins': ['.'], 'intercept_exceptions': True, 'allow_headers': ['.'], 'send_wildcard': False, 'expose_headers': None}
Thread-1--2017-01-16 22:59:24,763 flask_cors.core DEBUG CORS request received with 'Origin' http://localhost:5000
Thread-1--2017-01-16 22:59:24,764 flask_cors.core DEBUG The request's Origin header matches. Sending CORS headers.
Thread-1--2017-01-16 22:59:24,764 flask_cors.core DEBUG Settings CORS headers: MultiDict([('Access-Control-Allow-Origin', 'http://localhost:5000'), ('Vary', 'Origin')])
Thread-1--2017-01-16 22:59:24,766 werkzeug INFO 127.0.0.1 - - [16/Jan/2017 22:59:24] "GET /unfinishedgames HTTP/1.1" 200 -

As you can see, nothing appears in the Werkzeug or Flask-Cors logs until right before the request comes back to the browser. Only 3 ms pass between when Flask-Cors gets the request and when werkzeug sends the response back. It's like the browser request isn't even making it to the server, but I can't figure out why removing Flask-Cors fixes it.

Any ideas?

@dland512
Copy link

dland512 commented Jan 17, 2017

Just a little more info. Refreshing the page and using the back buttons seems to create the issue more consistently.

Also, this might be a Chrome issue. It doesn't seem to happen on Firefox or Safari on MacOS.

@dland512
Copy link

dland512 commented Feb 7, 2017

FYI, it appears as though it is related to a thread setting in Flask.

@corydolphin
Copy link
Owner

corydolphin commented Feb 7, 2017

Wow, that is very very interesting. I'll take a closer look to try to understand what is going on here.

Since this is somehow tickled when there is only one thread, it feels like it must be related to the usage of thread locals (current_app and request).

@corydolphin corydolphin reopened this Feb 7, 2017
@LucaPaterlini
Copy link

https://github.com/LucaPaterlini/ajax_flask
In this example, anyone can realize where the problem is.
Running the test in a local environment the example.html page takes more than 25 sec to show the alert message running the flask web service into a terminal and opening the HTML page into chrome's browser.

@corydolphin
Copy link
Owner

Luca, do you understand the cause of the issue? If so, please elaborate or make a pull request. I haven't yet dug into this, my plan was to first enable logging and add more verbose logging to see where things seem to stall.

Thanks for the example!
Cory

@LucaPaterlini
Copy link

127.0.0.1 - - [10/Feb/2017 20:19:27] "GET /user HTTP/1.1" 200 -

Exception happened during processing of request from ('127.0.0.1', 37550)
Traceback (most recent call last):
File "/usr/lib/python2.7/SocketServer.py", line 290, in _handle_request_noblock
self.process_request(request, client_address)
File "/usr/lib/python2.7/SocketServer.py", line 318, in process_request
self.finish_request(request, client_address)
File "/usr/lib/python2.7/SocketServer.py", line 331, in finish_request
self.RequestHandlerClass(request, client_address, self)
File "/usr/lib/python2.7/SocketServer.py", line 654, in init
self.finish()
File "/usr/lib/python2.7/SocketServer.py", line 713, in finish
self.wfile.close()
File "/usr/lib/python2.7/socket.py", line 283, in close
self.flush()
File "/usr/lib/python2.7/socket.py", line 307, in flush
self._sock.sendall(view[write_offset:write_offset+buffer_size])
error: [Errno 32] Broken pipe

This error appears when I've launched the page for the 5th time in a row, the previous 4 got data in milliseconds. I'm starting to fear that there is a race condition inside the code.

@corydolphin
Copy link
Owner

@LucaPaterlini Can you provide more information in terms of OS and python version? I can't seem to replicate this behavior.

As for your code, opening the same file from multiple threads, as your code does, looks suspect and may be an issue.

@LucaPaterlini
Copy link

Python 2.7.12
4.4.0-62-generic #83-Ubuntu SMP Wed Jan 18 14:10:15 UTC 2017 x86_64 x86_64 x86_64 GNU/Linux
Lubuntu 16.04LTS

The issue appears in the demo I've shown before.
I've added this line as you have suggested to me to turn around the problem by now.

app.run(debug=False,threaded=True)

@corydolphin
Copy link
Owner

@LucaPaterlini what version of chrome was this in btw?

@LucaPaterlini
Copy link

I don't think chrome is involved in a python program that runs a terminal, above all
that's is the version Version 56.0.2924.87 (64-bit)

@corydolphin
Copy link
Owner

@LucaPaterlini the reason I think Chrome may be involved is that the other reporter specifically mentioned that it only happened in chrome, and did not happen in Firefox.

Luca, can you replicate this via CURL? A working demo repository that I could see the failed request in would be VERY helpful.

Cheers,
Cory

@LucaPaterlini
Copy link

LucaPaterlini commented Mar 2, 2017 via email

@corydolphin
Copy link
Owner

Haha :-) Lucky or unlucky, it is hard to tell!

@corydolphin
Copy link
Owner

I'm going to close this issue until there is a reproducible case. If anyone has more information, I'd love to hear more!

@cbenz
Copy link

cbenz commented Mar 27, 2017

I all, thanks for investigating like me :)

For your information, I experienced the problem in "Chromium Version 56.0.2924.76 built on Debian 9.0, running on Debian 9.0 (64-bit)" or in Chrome 56 under macOS.

Running my application with flask run --with-threads solved the problem.

I think this is equivalent to app.run(threaded=True).

I do not have a reproductible case with Curl.

I could not find how to debug more, either on the Flask side which displays logs after the hanging, or on the Chrome/Chromium side, which displays the request headers after the response is received. I would like to see the OPTION and GET requests in the Flask logs as soon as they arrive.

@jrhite
Copy link

jrhite commented Aug 28, 2017

Just FYI, I still see the same problem:

I'm using Flask-Cors==3.0.3.

Running Mac OS X 10.11.6, Python 3.5.2. I experience this problem in Chrome (version 60.0.3112.113) with XHR requests. Safari and Firefox don't exhibit the problem. Curl doesn't exhibit any hanging or slowness (not doing XHR).

The hanging/slowness seems to occur about 90% of the time, causing some very trivial requests to hang for 5 minutes or so.

Running flask with --with-threads absolutely solves the problem.

@corydolphin
Copy link
Owner

@jrhite thanks for the report. I'm trying to run your project to see if I can reproduce it (and then investigate it properly). Looks like I am missing a 'repos.json' file? If you can update the instructions to make it possible for me to run that would be awesome.

Thanks!

@jrhite
Copy link

jrhite commented Aug 29, 2017

Thanks for looking into this @corydolphin. Actually I don't have the project uploaded to GitHub, so you won't be able to see the project I'm working on (sorry about that). Let me see if I can quickly put together a barebones demo of this behavior in the next few days and get that uploaded for you (am traveling now).

@corydolphin
Copy link
Owner

Ahh, my apologies -- I got confused by the reference above. That would be amazing, if you do that, I'll definitely look into it.

Thanks for the help with this -- it looks like a bit of a funky problem. Will be a fun one to debug :)

@jrhite
Copy link

jrhite commented Aug 31, 2017

Hey,

So sorry for the noise. This is almost definitely not an issue with flask-cors. This hanging/slowness problem with flask/chrome seems to be a pretty widespread problem and the theme pops up in google searches in lots of contexts not related to flask-cors. The --with-threads solution seems to fix things in all the situations I've read about.

Unfortunately for flask-cors, googling 'hanging chrome flask' and other similar variants pops up pallets/flask#2169 and #147 as the top 2 hits! ;-)

It was just bad timing for me as I just started noticing the hanging problem pretty much around the same time I added flask-cors, and then I saw others with the same problem, so it seemed pretty conclusive.

Still don't know the exact cause...it's intermittent and hard to reproduce reliably, but I can move along with the --with-threads flag.

Anyway, if it helps at all, if someone asks about this again, you can point them to my simple demo cors-test. Just comment/uncomment line 9 of the cors-test.py file and people should still see the app hanging intermittently when using chrome when flask is not running in --with-threads mode.

@jeiting
Copy link

jeiting commented Nov 13, 2017

Hit this same thing today.

The issue is the way Chrome opens speculative connections to speed up subsequent requests. If the speculative connection is accepted by the server first it hangs the singly threaded server. Nothing to do with flask-cors.

I wrote up what I found here.

@jrhite
Copy link

jrhite commented Nov 13, 2017

@jeiting Thanks for the follow up and write up!

@rnama22
Copy link

rnama22 commented Apr 2, 2018

I was having the same problem and setting up threaded = True resolved the issue. Couldn't find out the cause but Flask hangs for a brief time before even hitting the server. I have been seeing this issue often.

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

No branches or pull requests

8 participants