Skip to content
This repository has been archived by the owner on Nov 29, 2021. It is now read-only.

Improve error handling when sending data. (ospd-2) #147

Merged
merged 3 commits into from
Oct 10, 2019
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
- Improve connection handling. [#80](https://github.com/greenbone/ospd/pull/80)
- Fix target_to_ipv4_short(). [#99](https://github.com/greenbone/ospd/pull/99)
- Handle write error if the client disconnects abruptly. [#135](https://github.com/greenbone/ospd/pull/135)
- Improve error handling when sending data. [#147](https://github.com/greenbone/ospd/pull/147)

[2.0]: https://github.com/greenbone/ospd/compare/ospd-1.3...master

Expand Down
17 changes: 13 additions & 4 deletions ospd/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,11 @@ def __init__(self, sock: socket.socket, stream_timeout: int):
def close(self):
""" Close the stream
"""
self.socket.shutdown(socket.SHUT_RDWR)
try:
self.socket.shutdown(socket.SHUT_RDWR)
except OSError as e:
logger.debug("Ignoring error while shutting down the connection. %s", e)

self.socket.close()

def read(self, bufsize: Optional[int] = DEFAULT_BUFSIZE) -> bytes:
Expand All @@ -67,12 +71,17 @@ def write(self, data: bytes):

while True:
if b_end > len(data):
self.socket.send(data[b_start:])
break
try:
self.socket.send(data[b_start:])
except (socket.error, BrokenPipeError) as e:
logger.error("Error sending data to the client. %s", e)
finally:
return


try:
b_sent = self.socket.send(data[b_start:b_end])
except socket.error as e:
except (socket.error, BrokenPipeError) as e:
logger.error("Error sending data to the client. %s", e)
return
b_start = b_end
Expand Down