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

Catch errors setting SO_REUSEPORT #1483

Merged
merged 1 commit into from
Mar 20, 2017
Merged

Catch errors setting SO_REUSEPORT #1483

merged 1 commit into from
Mar 20, 2017

Conversation

tilgovi
Copy link
Collaborator

@tilgovi tilgovi commented Mar 14, 2017

Fix #1480

@tilgovi tilgovi force-pushed the fix/1480-reuse-port-fail branch from 316f2a8 to 9e076f4 Compare March 14, 2017 21:27
@tilgovi
Copy link
Collaborator Author

tilgovi commented Mar 14, 2017

The py36 coverage seems totally spurious.

@aconrad
Copy link
Contributor

aconrad commented Mar 15, 2017

Would it be possible that the tests are not guaranteed to run in a deterministic way where the code path for this branching logic could be different for some test runs? That's usually what the issue is. I've seen this same exact coverage report in other PRs of gunicorn.

@aconrad
Copy link
Contributor

aconrad commented Mar 15, 2017

@tilgovi Just so you know, I put a breakpoint just before raise LimitRequestLine(len(data), limit) and ran tox -e py35 in a loop and indeed, sometimes the breakpoint hits, and sometimes it doesn't (most of the time in my experience). My guess is that a dictionary is at play somewhere, the ordering of the keys is less deterministic in Python 3 than it is in Python 2 (but it might be something else too).

@aconrad
Copy link
Contributor

aconrad commented Mar 15, 2017

Here's what I actually ran (so I don't have to wait so long for all the tests to run):

while true; do tox -e py35 -- tests/test_invalid_requests.py; done

@tilgovi
Copy link
Collaborator Author

tilgovi commented Mar 15, 2017

I think it's more deliberate than that. Search random in https://github.com/benoitc/gunicorn/blob/master/tests/treq.py

@aconrad
Copy link
Contributor

aconrad commented Mar 15, 2017

Hah, there you go. I think it would be a good opportunity to fix the tests so they are deterministic (and also write additional tests to cover both sides of the coin)

gunicorn/sock.py Outdated
try:
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEPORT, 1)
except socket.error as err:
if err[0] in (errno.ENOPROTOOPT, errno.EINVAL):
Copy link
Collaborator

Choose a reason for hiding this comment

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

Or a simpler version:

if err[0] not in (errno.ENOPROTOOPT, errno.EINVAL):
    raise

@tilgovi tilgovi force-pushed the fix/1480-reuse-port-fail branch from 9e076f4 to 15408a3 Compare March 16, 2017 05:14
@tilgovi
Copy link
Collaborator Author

tilgovi commented Mar 16, 2017

Updated.

try:
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEPORT, 1)
except socket.error as err:
if err[0] not in (errno.ENOPROTOOPT, errno.EINVAL):
Copy link
Owner

Choose a reason for hiding this comment

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

i'm wondering if instead of raising an error, we couldn't simply try to start without this option and log a warning. Thoughts?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Looking at the errors that setsockopt(2) can return, I think anything else is probably very bad.

Copy link
Owner

Choose a reason for hiding this comment

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

fair enough :)

Choose a reason for hiding this comment

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

It's OK with this patch~

Copy link

Choose a reason for hiding this comment

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

Fixes #1480 on my system.

@benoitc
Copy link
Owner

benoitc commented Mar 16, 2017

@aconrad what do you mean by more deterministic?

Copy link
Owner

@benoitc benoitc left a comment

Choose a reason for hiding this comment

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

let's wait if we have some feedback but 👍 for the merge anyway

@aconrad
Copy link
Contributor

aconrad commented Mar 16, 2017

@benoitc Couverture.io showed us that the code path execution of each test run may be different each time (not guaranteed, non-deterministic), likely due to the random module used in tests https://github.com/benoitc/gunicorn/blob/master/tests/treq.py, as @tilgovi suggested.

As we can see from the coverage report, line 215 or line 218 of file https://github.com/benoitc/gunicorn/blob/master/gunicorn/http/message.py#L215-L218 may be executed during test runs (unpredictable):

https://app.couverture.io/coverage/github/benoitc/gunicorn/9e076f4246c80f2091163242e7c985ad6ccb78c7/py36

So if the culprit is indeed the random module, we should set a proper seed value so we can guarantee repeatable outcomes of the tests. We might want to set a PYTHONHASHSEED env variable which tox describes here (even though it's a Python feature, not a tox one): https://tox.readthedocs.io/en/latest/example/basic.html#special-handling-of-pythonhashseed

Once fixed (e.g. line 215 is guaranteed to be the code path taken) then we should write additional tests to make sure that line 218 is also covered eventually.

I hope this helps.

try:
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEPORT, 1)
except socket.error as err:
if err[0] not in (errno.ENOPROTOOPT, errno.EINVAL):
Copy link

Choose a reason for hiding this comment

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

Fixes #1480 on my system.

@tilgovi tilgovi merged commit ef53d02 into master Mar 20, 2017
@tilgovi tilgovi deleted the fix/1480-reuse-port-fail branch March 20, 2017 18:55
mjjbell pushed a commit to mjjbell/gunicorn that referenced this pull request Mar 16, 2018
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

Successfully merging this pull request may close these issues.

6 participants