Skip to content

Handle OSError on shutdown & TimeoutError on recv #974

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

Merged
merged 2 commits into from
Jan 12, 2022
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
6 changes: 5 additions & 1 deletion proxy/core/base/tcp_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,11 @@ async def handle_writables(self, writables: Writables) -> bool:
async def handle_readables(self, readables: Readables) -> bool:
teardown = False
if self.work.connection.fileno() in readables:
data = self.work.recv(self.flags.client_recvbuf_size)
try:
data = self.work.recv(self.flags.client_recvbuf_size)
except TimeoutError:
logger.info('Client recv timeout error')
return True
if data is None:
logger.debug(
'Connection closed by client {0}'.format(
Expand Down
3 changes: 3 additions & 0 deletions proxy/core/base/tcp_upstream.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,9 @@ async def read_from_descriptors(self, r: Readables) -> bool:
else:
# Tear down because upstream proxy closed the connection
return True
except TimeoutError:
logger.info('Upstream recv timeout error')
return True
except ssl.SSLWantReadError:
logger.info('Upstream SSLWantReadError, will retry')
return False
Expand Down
5 changes: 4 additions & 1 deletion proxy/core/connection/pool.py
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,10 @@ def _remove(self, fileno: int) -> None:
"""Remove a connection by descriptor from the internal data structure."""
conn = self.connections[fileno]
logger.debug('Removing conn#{0} from pool'.format(id(conn)))
conn.connection.shutdown(socket.SHUT_WR)
try:
conn.connection.shutdown(socket.SHUT_WR)
except OSError:
pass
conn.close()
self.pools[conn.addr].remove(conn)
del self.connections[fileno]
5 changes: 4 additions & 1 deletion proxy/http/websocket/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,5 +115,8 @@ def run(self) -> None:
finally:
if not self.closed:
self.selector.unregister(self.sock)
self.sock.shutdown(socket.SHUT_WR)
try:
self.sock.shutdown(socket.SHUT_WR)
except OSError:
pass
self.sock.close()