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

πŸ› Suppress legit OS errors on socket shutdown #342

Merged
merged 3 commits into from
Dec 5, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
waiting for a ``tick`` event, addressing performance
degradation introduced in v8.1.0.

- :issue:`341` via :pr:`342`: Suppress legitimate OS errors
expected on shutdown.

.. scm-version-title:: v8.4.8

- :issue:`317` via :pr:`337`: Fixed a regression in
Expand Down
22 changes: 22 additions & 0 deletions cheroot/errors.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# -*- coding: utf-8 -*-
"""Collection of exceptions raised and/or processed by Cheroot."""

from __future__ import absolute_import, division, print_function
Expand Down Expand Up @@ -56,3 +57,24 @@ def plat_specific_errors(*errnames):
if sys.platform == 'darwin':
socket_errors_to_ignore.extend(plat_specific_errors('EPROTOTYPE'))
socket_errors_nonblocking.extend(plat_specific_errors('EPROTOTYPE'))


acceptable_sock_shutdown_error_codes = {
errno.ENOTCONN,
errno.EPIPE, errno.ESHUTDOWN, # corresponds to BrokenPipeError in Python 3
Copy link
Contributor

Choose a reason for hiding this comment

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

do both of these map to BrokenPipeError? maybe worth putting on their own line so the comment is unambiguous

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes, that's why I've put them on the same line: https://docs.python.org/3/library/exceptions.html#BrokenPipeError

errno.ECONNRESET, # corresponds to ConnectionResetError in Python 3
}
"""Errors that may happen during the connection close sequence.

* ENOTCONN β€” client is no longer connected
* EPIPE β€” write on a pipe while the other end has been closed
* ESHUTDOWN β€” write on a socket which has been shutdown for writing
* ECONNRESET β€” connection is reset by the peer

Ref: https://github.com/cherrypy/cheroot/issues/341#issuecomment-735884889
"""

try: # py3
acceptable_sock_shutdown_exceptions = (BrokenPipeError, ConnectionResetError)
except NameError: # py2
acceptable_sock_shutdown_exceptions = ()
6 changes: 3 additions & 3 deletions cheroot/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,6 @@
import platform
import contextlib
import threading
import errno

try:
from functools import lru_cache
Expand Down Expand Up @@ -1479,9 +1478,10 @@ def _close_kernel_socket(self):

try:
shutdown(socket.SHUT_RDWR) # actually send a TCP FIN
except errors.acceptable_sock_shutdown_exceptions:
pass
except socket.error as e:
# Suppress "client is no longer connected"
if e.errno != errno.ENOTCONN:
if e.errno not in errors.acceptable_sock_shutdown_error_codes:
raise


Expand Down