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

Handle more socket exceptions #349

Merged
merged 4 commits into from
Feb 5, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
31 changes: 19 additions & 12 deletions datadog/dogstatsd/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import logging
import os
import socket
from threading import Lock

# datadog
from datadog.dogstatsd.context import TimedContextManagerDecorator
Expand Down Expand Up @@ -59,6 +60,8 @@ def __init__(self, host='localhost', port=8125, max_buffer_size=50, namespace=No
:type socket_path: string
"""

self.lock = Lock()

# Connection
if socket_path is not None:
self.socket_path = socket_path
Expand Down Expand Up @@ -114,16 +117,17 @@ def get_socket(self):
Note: connect the socket before assigning it to the class instance to
avoid bad thread race conditions.
"""
if not self.socket:
if self.socket_path is not None:
sock = socket.socket(socket.AF_UNIX, socket.SOCK_DGRAM)
sock.connect(self.socket_path)
sock.setblocking(0)
self.socket = sock
else:
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
sock.connect((self.host, self.port))
self.socket = sock
with self.lock:
if not self.socket:
if self.socket_path is not None:
sock = socket.socket(socket.AF_UNIX, socket.SOCK_DGRAM)
sock.connect(self.socket_path)
sock.setblocking(0)
self.socket = sock
else:
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
sock.connect((self.host, self.port))
self.socket = sock

return self.socket

Expand Down Expand Up @@ -289,9 +293,12 @@ def _send_to_server(self, packet):
except socket.timeout:
# dogstatsd is overflowing, drop the packets (mimicks the UDP behaviour)
return
except socket.error:
log.info("Error submitting packet, dropping the packet and closing the socket")
except (socket.error, socket.herror, socket.gaierror) as se:
log.warning("Error submitting packet: {}, dropping the packet and closing the socket".format(se))
self.close_socket()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we be closing the socket in the second except clause as well?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure, # If set, use socket directly, would the last exception cover the case of not using the socket and thus not needing to close it? Or am I on the wrong track there?

except Exception as e:
log.error("Unexpected error: %s", str(e))
return

def _send_to_buffer(self, packet):
self.buffer.append(packet)
Expand Down
2 changes: 1 addition & 1 deletion tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,4 @@ deps =
commands = flake8 datadog

[flake8]
max-line-length = 100
max-line-length = 120