From 2b5d100cd3e3146711ed5696258cdecceaa8c760 Mon Sep 17 00:00:00 2001 From: Matt Visnovsky Date: Mon, 6 May 2024 15:56:41 -0600 Subject: [PATCH] Ensure SNMP session is closed properly Addresses https://github.com/louislam/uptime-kuma/pull/4717#discussion_r1589858252 Co-Authored-By: Frank Elsinga --- server/monitor-types/snmp.js | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/server/monitor-types/snmp.js b/server/monitor-types/snmp.js index f660df538..ab14bb078 100644 --- a/server/monitor-types/snmp.js +++ b/server/monitor-types/snmp.js @@ -17,8 +17,9 @@ class SNMPMonitorType extends MonitorType { version: parseInt(monitor.snmpVersion), }; + let session; try { - const session = snmp.createSession(monitor.hostname, monitor.snmpCommunityString, options); + session = snmp.createSession(monitor.hostname, monitor.snmpCommunityString, options); // Handle errors during session creation session.on("error", (error) => { @@ -77,11 +78,13 @@ class SNMPMonitorType extends MonitorType { } heartbeat.msg = "SNMP value " + (heartbeat.status ? "passes" : "does not pass") + ` comparison: ${value.toString()} ${monitor.snmpCondition} ${snmpControlValue}`; - session.close(); - } catch (err) { heartbeat.status = DOWN; heartbeat.msg = `SNMP Error: ${err.message}`; + } finally { + if (session) { + session.close(); + } } }