From 8b70eb1da0b48f25781bf9218f40756f2fbf14ea Mon Sep 17 00:00:00 2001 From: Juan Jose Nicola Date: Thu, 10 Oct 2019 15:05:43 +0200 Subject: [PATCH 1/3] Improve error handling when sending data --- ospd/server.py | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/ospd/server.py b/ospd/server.py index 8f5e3700..0fe0aa75 100644 --- a/ospd/server.py +++ b/ospd/server.py @@ -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("Error: Connection closed. %s", e) + self.socket.close() def read(self, bufsize: Optional[int] = DEFAULT_BUFSIZE) -> bytes: @@ -67,12 +71,16 @@ def write(self, data: bytes): while True: if b_end > len(data): - self.socket.send(data[b_start:]) + try: + self.socket.send(data[b_start:]) + except (socket.error, BrokenPipeError) as e: + logger.error("Error sending data to the client. %s", e) + return break 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 From 9f8f612a0e96a12763d18a9ea307204d97a4e179 Mon Sep 17 00:00:00 2001 From: Juan Jose Nicola Date: Thu, 10 Oct 2019 15:09:26 +0200 Subject: [PATCH 2/3] Update changelog --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 924339f3..ec1dcfbc 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 From 8bb7249c42c2ab57b865296b34252467af3abc80 Mon Sep 17 00:00:00 2001 From: Juan Jose Nicola Date: Thu, 10 Oct 2019 15:47:00 +0200 Subject: [PATCH 3/3] Apply @bricks suggestions --- ospd/server.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/ospd/server.py b/ospd/server.py index 0fe0aa75..5523de38 100644 --- a/ospd/server.py +++ b/ospd/server.py @@ -49,7 +49,7 @@ def close(self): try: self.socket.shutdown(socket.SHUT_RDWR) except OSError as e: - logger.debug("Error: Connection closed. %s", e) + logger.debug("Ignoring error while shutting down the connection. %s", e) self.socket.close() @@ -75,8 +75,9 @@ def write(self, data: bytes): self.socket.send(data[b_start:]) except (socket.error, BrokenPipeError) as e: logger.error("Error sending data to the client. %s", e) + finally: return - break + try: b_sent = self.socket.send(data[b_start:b_end])