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 potential deadlock during process fork #817

Merged
merged 2 commits into from
Mar 12, 2024
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 1 addition & 4 deletions datadog/dogstatsd/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -1370,10 +1370,7 @@ def post_fork(self):

log.debug("[%d] post_fork for %s", os.getpid(), self)

with self._socket_lock:
if self.socket or self.telemetry_socket:
log.warning("Open socket detected after fork. Call pre_fork() before os.fork().")
self.close_socket()
self.close_socket()

self._forking = False

Expand Down
13 changes: 13 additions & 0 deletions tests/unit/dogstatsd/test_statsd.py
Original file line number Diff line number Diff line change
Expand Up @@ -1963,3 +1963,16 @@ def test_max_payload_size(self):
self.assertEqual(statsd._max_payload_size, UDP_OPTIMAL_PAYLOAD_LENGTH)
statsd.socket_path = "/foo"
self.assertEqual(statsd._max_payload_size, UDS_OPTIMAL_PAYLOAD_LENGTH)

def test_post_fork_locks(self):
def inner():
statsd = DogStatsd(socket_path=None, port=8125)
# Statsd should survive this sequence of events
statsd.pre_fork()
statsd.get_socket()
statsd.post_fork()
t = Thread(target=inner)
t.daemon = True
t.start()
t.join(timeout=5)
self.assertFalse(t.is_alive())
Loading