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

Commit

Permalink
Merge pull request #147 from jjnicola/brocken-pipe2
Browse files Browse the repository at this point in the history
Improve error handling when sending data. (ospd-2)
  • Loading branch information
bjoernricks authored Oct 10, 2019
2 parents 6f3e6e3 + 8bb7249 commit 0d226f6
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 4 deletions.
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

0 comments on commit 0d226f6

Please sign in to comment.