Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix blocking connections in dogstatsd #634

Merged
merged 2 commits into from
Feb 25, 2021
Merged
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
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