Skip to content

narrow except around socket.getpeername to OSError in SSL handshake handler#3641

Open
HrachShah wants to merge 1 commit into
tornadoweb:masterfrom
HrachShah:fix/iostream-getpeername-oserror-narrow
Open

narrow except around socket.getpeername to OSError in SSL handshake handler#3641
HrachShah wants to merge 1 commit into
tornadoweb:masterfrom
HrachShah:fix/iostream-getpeername-oserror-narrow

Conversation

@HrachShah

Copy link
Copy Markdown

narrow except around socket.getpeername to OSError in SSL handshake handler

The handler at the SSL_ERROR_SSL/SSL_ERROR_SYSCALL branch of the
handshake error path wraps self.socket.getpeername() in a
try/except Exception that falls back to the literal string
'(not connected)'. getpeername() only raises OSError and its
subclasses (ENOTCONN when the socket is not yet connected,
EBADF when the fd is no longer valid, ENOTSOCK for a bad
file descriptor). It does not raise any other Exception type.

Narrowing the handler to OSError lets a real bug in our own
code (AttributeError from a typo on self.socket, NameError
after a partial module reload, TypeError from a wrong type
passed in) surface as a diagnostic instead of being
swallowed and silently replaced with the '(not connected)'
fallback string, which then gets logged as the peer address
for a real peer that we just lost the ability to look up.

…andler

The handler at the SSL_ERROR_SSL/SSL_ERROR_SYSCALL branch of the
handshake error path wraps self.socket.getpeername() in a
try/except Exception that falls back to the literal string
'(not connected)'. getpeername() only raises OSError and its
subclasses (ENOTCONN when the socket is not yet connected,
EBADF when the fd is no longer valid, ENOTSOCK for a bad
file descriptor). It does not raise any other Exception type.

Narrowing the handler to OSError lets a real bug in our own
code (AttributeError from a typo on self.socket, NameError
after a partial module reload, TypeError from a wrong type
passed in) surface as a diagnostic instead of being
swallowed and silently replaced with the '(not connected)'
fallback string, which then gets logged as the peer address
for a real peer that we just lost the ability to look up.
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.

1 participant