Repository navigation
Closed
Description
Hello guys,
I have been testing the performance of an application built with Flask, SQLAlchemy and gevent on Gunicorn==19.7.0 with wrk and it keeps raising Socket Errors, the issue disappears by downgrading to Gunicorn==19.6.0, here is the full stack trace:
[2017-03-20 00:46:39 +0100] [79068] [ERROR] Socket error processing request.
Traceback (most recent call last):
File "/Users/ahmad/Projects/Side-Gigs/sa7beh-app/venv/lib/python3.6/site-packages/gunicorn-19.7.0-py3.6.egg/gunicorn/workers/async.py", line 66, in handle
six.reraise(*sys.exc_info())
File "/Users/ahmad/Projects/Side-Gigs/sa7beh-app/venv/lib/python3.6/site-packages/gunicorn-19.7.0-py3.6.egg/gunicorn/six.py", line 625, in reraise
raise value
File "/Users/ahmad/Projects/Side-Gigs/sa7beh-app/venv/lib/python3.6/site-packages/gunicorn-19.7.0-py3.6.egg/gunicorn/workers/async.py", line 56, in handle
self.handle_request(listener_name, req, client, addr)
File "/Users/ahmad/Projects/Side-Gigs/sa7beh-app/venv/lib/python3.6/site-packages/gunicorn-19.7.0-py3.6.egg/gunicorn/workers/ggevent.py", line 152, in handle_request
super(GeventWorker, self).handle_request(*args)
File "/Users/ahmad/Projects/Side-Gigs/sa7beh-app/venv/lib/python3.6/site-packages/gunicorn-19.7.0-py3.6.egg/gunicorn/workers/async.py", line 129, in handle_request
six.reraise(*sys.exc_info())
File "/Users/ahmad/Projects/Side-Gigs/sa7beh-app/venv/lib/python3.6/site-packages/gunicorn-19.7.0-py3.6.egg/gunicorn/six.py", line 625, in reraise
raise value
File "/Users/ahmad/Projects/Side-Gigs/sa7beh-app/venv/lib/python3.6/site-packages/gunicorn-19.7.0-py3.6.egg/gunicorn/workers/async.py", line 115, in handle_request
resp.write(item)
File "/Users/ahmad/Projects/Side-Gigs/sa7beh-app/venv/lib/python3.6/site-packages/gunicorn-19.7.0-py3.6.egg/gunicorn/http/wsgi.py", line 362, in write
util.write(self.sock, arg, self.chunked)
File "/Users/ahmad/Projects/Side-Gigs/sa7beh-app/venv/lib/python3.6/site-packages/gunicorn-19.7.0-py3.6.egg/gunicorn/util.py", line 321, in write
sock.sendall(data)
File "/Users/ahmad/Projects/Side-Gigs/sa7beh-app/venv/lib/python3.6/site-packages/gevent-1.2.1-py3.6-macosx-10.12-x86_64.egg/gevent/_socket3.py", line 418, in sendall
data_sent += self.send(data_memory[data_sent:], flags)
File "/Users/ahmad/Projects/Side-Gigs/sa7beh-app/venv/lib/python3.6/site-packages/gevent-1.2.1-py3.6-macosx-10.12-x86_64.egg/gevent/_socket3.py", line 391, in send
return _socket.socket.send(self._sock, data, flags)
OSError: [Errno 41] Protocol wrong type for socket
Full list of pip dependencies:
appdirs==1.4.3
bcrypt==3.1.3
cffi==1.9.1
click==6.7
Flask==0.12
Flask-Bcrypt==0.7.1
Flask-Compress==1.4.0
Flask-Login==0.4.0
Flask-Script==2.0.5
Flask-SQLAlchemy==2.2
gevent==1.2.1
greenlet==0.4.12
gunicorn==19.7.0
honcho==0.7.1
itsdangerous==0.24
Jinja2==2.9.5
MarkupSafe==1.0
packaging==16.8
psycogreen==1.0
psycopg2==2.7.1
pycparser==2.17
PyJWT==1.4.2
pyparsing==2.2.0
python-dateutil==2.6.0
requests==2.13.0
setproctitle==1.1.10
six==1.10.0
SQLAlchemy==1.1.6
Werkzeug==0.12.1
Commands:
- Running the app:
gunicorn -k gevent -w 18 sa7beh.app:app - Wrk command:
wrk -c 3600 -t 2000 -d 60 <API URL>
Let me know if you need more information.
Reacted by Steve Dignam, failable and Nobuharu Shimazu
Activity
benoitc commentedon Mar 20, 2017
can you try with #1483 ?
aalhour commentedon Mar 20, 2017
Hello @benoitc,
Thanks for your prompt reply.
I have checked out Gunicorn at the specified branch and commit in #1483 and installed it into the virtual environment. Unfortunately, the same error keeps reappearing.
jamadden commentedon Mar 20, 2017
Hi, gevent developer here. We recently had a somewhat similar issue come up with Python 2 sockets and SOCK_CLOEXEC that makes me wonder if there might be something going on at the gevent level. I primarily run on OS X and I haven't been able to reproduce this yet in a simplified example (not using gunicorn)...but I haven't tried that hard yet :) A few questions that might help narrow things down:
Correct me if I'm wrong, but you're using the default gunicorn listener configuration, so TCP, and not unix sockets?
You look to be on OS X 10.12 sierra?
Did you build gevent from source (looks that way) or did you install the binary egg from PyPI?
jamadden commentedon Mar 20, 2017
I also can't reproduce using gunicorn 19.7.0 and the simplest possible app (on OS X 10.12, Python 3.6 from macports, using the gevent wheel from PyPI:
EDIT: I also cannot reproduce using the python.org installed version of 3.6.0.
aalhour commentedon Mar 20, 2017
Hello @jamadden,
Thanks for trying to help. The error doesn't appear right after starting the app. It actually appears 5-10 times (probably for 5-10 requests) when I test the app with
wrkwhich sends out 20K requests in a minute.Regarding your questions, I am using Gunicorn with the standard configuration, I am binding it to
0.0.0.0:5000, worker class isgeventand my gunicorn configuration file sets the number of workers to(multiprocessing.cpu_count() * 2) + 1which ends forking 18 processes including the master one. I have installgeventusing pip via thepip -r requirements.txtcommand, you can find the complete list of dependencies above. And you guessed it right, I am on macOS Sierra 10.12.3.I don't know what is causing the problem to be honest but I can reproduce it every single time I test the app with
wrk, I just tried to downgrade my Python version (installed viapyenv) from v3.6.0 to v3.5.3 but kept Gunicorn's version at 19.7.0, and the problem persisted. However, by just downgrading the version of Gunicorn to 19.6.0, the problem stopped appearing at all, I ran different tests withwrkagainst different APIs that do touch the database, and the problem didn't appear. The following are the commands I tried to try smoke out the issue:wrk -c 1000 -t 100 -d 60 http://localhost:5000/api/v1.0/userswrk -c 3600 -t 2000 -d 60 http://localhost:5000/api/v1.0/userswrk -c 3600 -t 2000 -d 120 http://localhost:5000/api/v1.0/usersThe above 3 commands always reproduced the problem on Python 3.6.0.
Also, I mentioned in my first comment that the issue disappears when downgrading to Gunicorn==19.6.0, but it still shows up as long as my Python's version is 3.6.0. The issue only disappeared so far with the combination: Python==3.5.3 + Gunciron==19.6.0.
I hope the above information helped.
aalhour commentedon Mar 20, 2017
Hello @benoitc , @jamadden,
The issue looks to be even worse! I have just ran a longer test (5 minutes) with
wrkand the issue appeared again. It doesn't look like downgrading Python and Gunicorn helped, they just made the issue take more time to appear. Here is the test command:Here is how I am currently running Gunicorn:
Here is what happens when I grep the log for errors:
All these 9 cases have the same traceback, here is a sample:
At this point, I am clueless to what is causing the problem. Nothing has changed in the list of dependencies.
EDIT: copy-paste the current command.
jamadden commentedon Mar 20, 2017
I had already tried doing concurrency tests with
aband didn't reproduce a problem with the simple app. I installed the latest wrk from github and tried withwrk -c 1000 -t 100 -d 60 http://localhost:8000/and still did not produce a problem...the worst I saw were a small number (~4000 out of 334,977) 'Connection reset by peer' messages when I turned logging to the DEBUG level. (Note that I'm benchmarking from the same machine gunicorn is running on.)Can you produce a problem using the simple app? If so, that tells us its system dependent somehow. If not, that tells us its related to the code in the app you're running in some way and we'd need to simplify that to pin down the issue (in particular, I would be concerned about C extensions or CFFI code).
jamadden commentedon Mar 21, 2017
I wonder if you're seeing this behaviour, which basically comes down to timing/race-type conditions. The difference in our test machines, and possibly in the amount of data being sent, might explain the reproduction issues, as well as explaining why different versions of Python and gunicorn behave differently.
aalhour commentedon Mar 21, 2017
Hello @jamadden,
I didn't test the simple app yet on my Mac machine, but I have tested it on my Linux (Lenovo) machine with Ubuntu 14.04. The error didn't show up at all. Here are the test results:
Operating System:
Application:
Gunicorn Command:
Python 3.5.3 + Gunicorn 19.6.0:
The log file contained no errors and no critical messages.
Python 3.5.3 + Gunicorn 19.7.0:
The log file contained no errors and no critical messages.
Python 3.6.0 + Gunicorn 19.7.0:
The log file contained no errors and no critical messages.
Python 3.6.0 + Gunicorn 19.7.1:
The log file contained no errors and no critical messages.
After all the tests above have resulted in good news, I went ahead and tested my own Flask application on the same machine with Python 3.6.0 and Gunicorn 19.7.1 and the error didn't appear, not even once! I tested the app twice, once with a standard Gunicorn command, and then with my own Gunicorn configuration, here are the commands:
Gunicorn Commands:
$ gunicorn -k gevent -w 17 sa7beh.app:app > logs/sa7beh_app.log 2>&1$ gunicorn -c config/gunicorn.conf.py sa7beh.app:app > logs/sa7beh_app.log 2>&1Wrk command:
Same command was used twice, sample result:
So apparently the issue, at least so far, only happens on macOS Sierra 10.12.1. I will re-run the same tests this evening on my Mac machine and copy-paste the tests results here as well.
jamadden commentedon Mar 21, 2017
That makes sense to me, based on the research I've done. It appears to be mac-specific, quite probably even mac version specific and possibly machine specific too.
aalhour commentedon Mar 21, 2017
Hello @jamadden,
I have tested the simple app on my Mac machine once with standard configuration and a second time with my app's Gunicorn configuration and the error appeared. I have used the following
wrkparameters. Python is v3.6.0 and Gunicorn is v19.7.1.I also tested my app with Python 3.6.0 + Gunicorn 19.7.1 and the error appeared. The weird thing though is that I tested the index page of my app which only returns an HTML static page, no database or 3rd services being accessed whatsoever, and to be 100% precise, the index page checks a cookie header and redirects to another static page for user login, but still no additional database or network access.
The same error (
OSError: [Errno 41] Protocol wrong type for socket) occurred 5 times:I tried testing one of my API endpoints on Python 3.6.0 + Gunicorn 19.7.1 and
wrkand the same error appeared.It looks like a Mac issue to me. What do you guys think?
CC @benoitc.
P.S. here is my Gunicorn config file in case you guys would like to know about all the details:
racitup commentedon Oct 2, 2017
I'm also getting
OSError: [Errno 41] Protocol wrong type for socketon my mac 10.11.6 with both gunicorn and waitress.With gunicorn I got this:
6 remaining items
racitup commentedon May 9, 2018
Since I couldn't find it on the python bugtracker, I filed it here
We'd probably be waiting forever for Apple to fix it...
@tilgovi It's been a long time since I looked at it, but since it seems to be intermittent and probably some kind of race condition, catch the exception as low as possible in gunicorn and try again, possibly adding a small delay? You could even make the code mac-specific and comment it as a workaround until python/Apple fix it? Alternatively, since the application will probably retry anyway, do nothing and mark as "known issue"?
enugentdt commentedon Oct 15, 2018
I know it's been a while, but I'm going to hop on the bandwagon.
I run Siege to test my servers, using aiohttp, and sometimes it's behind guincorn. It appears to only happen (in my case) when I ctrl+C Siege. So I'd personally agree with the race condition idea? Or maybe it's to do with closed sockets?
But if anybody needs to test ideas, I'm more than happy to get in contact with whomever to help repro bugs. But, with my software, I can consistently produce this.
I'm running OSX 10.12.6, mid-2015 15" MBP.
Sorry if I'm late to the party or compounding something already known, or if I'm even being useless. Let me know if there's anything I can do to help.
jaydeeponly commentedon Oct 29, 2018
The moment I stopped socketIO, the error is gone :-) I believe the problem is related with socketIO.
OrchidHu commentedon May 30, 2019
[ERROR] Socket error processing request
brian-farrell commentedon May 13, 2020
FYI - I came here while searching about this same error that I am seeing with with version 3.0.6 of the Django Development server on MacOS, so this error does not seem specific to gunicorn.
Here is the stack trace I'm seeing:
Just realized that I should have noted my MacOS Version: 10.14.6 (18G4032)
guareber commentedon May 19, 2020
I can +1 brian-farrell's post - older django and python versions, but pretty identical stacktrace. OSX version 10.15.2 (19C57).
arickuter commentedon Jan 21, 2021
I am still seeing this error. Has there been any resolution yet?
ktsitsi commentedon Jan 22, 2021
I am seeing this error on OSX version 10.14. In my use case the error relates to GCS emulator. I can confirm that in Linux the error does not occur in the same scenario. Is there any update or fix on that?
gunicornversion is the required by GCS20.0.4tilgovi commentedon Feb 16, 2021
I'm not sure there's anything we can do here. We have this discussion for anyone who searches for the issue, but it seems to be an issue with macOS. I'm going to close this, but if anyone has suggestions for some action we should take, please open an issue or a PR to address this.
Add SSL support (SMTPS/STARTTLS) to CLI (#252)
Replace ConnectionError with OSError
Fix "pypy3" testenv on Windows, Fix MacOS flakeyness (#254)