DataDog / datadogpy

The Datadog Python library
https://datadoghq.com/
Other
605 stars 303 forks source link

Fix potential deadlock during process fork #817

Closed vickenty closed 4 months ago

vickenty commented 4 months ago

What does this PR do?

Fix a potential deadlock in post_fork().

Description of the Change

close_socket acquires _socket_lock, and calling it with the lock held deadlocks.

On top of this the warning did not prove very useful, as it would also trigger when the application is sending metrics from another thread when the process forks, in which case the warning is a bit misleading.

Without the warning there is no reason to check for socket being open, so just silently call close_socket() after the fork to avoid sharing the file descriptor between processes if another thread re-opened it between pre_fork() and fork itself.

Alternate Designs

Possible Drawbacks

Verification Process

Additional Notes

Release Notes

Review checklist (to be filled by reviewers)

aweldy2 commented 4 months ago

+1 to this being a pretty major issue -- we encountered this exact bug earlier this week. @vickenty another option would be to change socket_lock to be a threading.RLock instead of a threading.Lock so that it's reentrant.