From f3327d0c830ca3afc47b0253e4cf35abd3160881 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thomas=20Herv=C3=A9?= Date: Tue, 23 Feb 2021 11:06:43 +0100 Subject: [PATCH 1/2] Fix blocking connections in dogstatsd This adds back non blocking to UDP and sets non blocking before connect like it was before --- datadog/dogstatsd/base.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/datadog/dogstatsd/base.py b/datadog/dogstatsd/base.py index 919cfdaaf..5db90c37c 100644 --- a/datadog/dogstatsd/base.py +++ b/datadog/dogstatsd/base.py @@ -319,13 +319,14 @@ def get_socket(self, telemetry=False): @staticmethod def _get_uds_socket(socket_path): sock = socket.socket(socket.AF_UNIX, socket.SOCK_DGRAM) - sock.connect(socket_path) sock.setblocking(0) + sock.connect(socket_path) return sock @staticmethod def _get_udp_socket(host, port): sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) + sock.setblocking(0) sock.connect((host, port)) return sock From 7c189c922e0b48ed76d3b7c1c6e2c17ae0fd7395 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thomas=20Herv=C3=A9?= Date: Tue, 23 Feb 2021 15:00:22 +0100 Subject: [PATCH 2/2] Use lock on close, fix small issue in get_socket --- datadog/dogstatsd/base.py | 41 ++++++++++++++++++++------------------- 1 file changed, 21 insertions(+), 20 deletions(-) diff --git a/datadog/dogstatsd/base.py b/datadog/dogstatsd/base.py index 5db90c37c..05002f163 100644 --- a/datadog/dogstatsd/base.py +++ b/datadog/dogstatsd/base.py @@ -307,14 +307,14 @@ def get_socket(self, telemetry=False): self.telemetry_socket = self._get_udp_socket_socket(self.telemetry_host, self.telemetry_port) return self.telemetry_socket - else: - if not self.socket: - if self.socket_path is not None: - self.socket = self._get_uds_socket(self.socket_path) - else: - self.socket = self._get_udp_socket(self.host, self.port) - return self.socket + if not self.socket: + if self.socket_path is not None: + self.socket = self._get_uds_socket(self.socket_path) + else: + self.socket = self._get_udp_socket(self.host, self.port) + + return self.socket @staticmethod def _get_uds_socket(socket_path): @@ -493,19 +493,20 @@ def close_socket(self): """ Closes connected socket if connected. """ - if self.socket: - try: - self.socket.close() - except OSError as e: - log.error("Unexpected error: %s", str(e)) - self.socket = None - - if self.telemetry_socket: - try: - self.telemetry_socket.close() - except OSError as e: - log.error("Unexpected error: %s", str(e)) - self.telemetry_socket = None + with self.lock: + if self.socket: + try: + self.socket.close() + except OSError as e: + log.error("Unexpected error: %s", str(e)) + self.socket = None + + if self.telemetry_socket: + try: + self.telemetry_socket.close() + except OSError as e: + log.error("Unexpected error: %s", str(e)) + self.telemetry_socket = None def _serialize_metric(self, metric, metric_type, value, tags, sample_rate=1): # Create/format the metric packet