Skip to content

Commit

Permalink
Fix blocking connections in dogstatsd (#634)
Browse files Browse the repository at this point in the history
* Fix blocking connections in dogstatsd

This adds back non blocking to UDP and sets non blocking before connect
like it was before

* Use lock on close, fix small issue in get_socket
  • Loading branch information
therve authored Feb 25, 2021
1 parent d6d77b5 commit 689eb20
Showing 1 changed file with 23 additions and 21 deletions.
44 changes: 23 additions & 21 deletions datadog/dogstatsd/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -307,25 +307,26 @@ 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):
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

Expand Down Expand Up @@ -492,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
Expand Down

0 comments on commit 689eb20

Please sign in to comment.