Skip to content

Commit 4e2d1f3

Browse files
authored
Fix UDP socket FD leak: call close_dispatcher() in worker and discovery cleanup (DataDog#23254)
1 parent d31ec85 commit 4e2d1f3

2 files changed

Lines changed: 8 additions & 3 deletions

File tree

snmp/datadog_checks/snmp/discovery.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,9 @@ def discover_instances(config, interval, check_ref):
5353
del check
5454
continue
5555
finally:
56-
# Drain tasks left by this SNMP call; leftover handle_timeout tasks
57-
# would stop the loop before the next host's request completes.
56+
# Close the host's transport dispatcher to release its UDP socket FD, then
57+
# drain any leftover handle_timeout tasks so they don't stop the next host's loop.
58+
host_config._snmp_engine.transport_dispatcher.close_dispatcher()
5859
_pending = asyncio.all_tasks(loop)
5960
for _t in _pending:
6061
_t.cancel()

snmp/datadog_checks/snmp/snmp.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -496,7 +496,11 @@ def _check_device(self, config, isolated_loop=False):
496496
return error, tags
497497
finally:
498498
if loop is not None:
499-
# Cancel pending pysnmp tasks and close the loop to release file descriptors.
499+
# Explicitly close the transport dispatcher to release UDP socket FDs.
500+
# asyncio.all_tasks only cancels Task objects; the UdpAsyncioTransport is a
501+
# DatagramProtocol/Transport pair — its FD is only released by close_dispatcher().
502+
config._snmp_engine.transport_dispatcher.close_dispatcher()
503+
# Cancel remaining pysnmp tasks (e.g. handle_timeout) and close the loop.
500504
pending = asyncio.all_tasks(loop)
501505
for task in pending:
502506
task.cancel()

0 commit comments

Comments
 (0)