{"record":{"id":"97c81cd0edfa053d","repo":"louislam/uptime-kuma","slug":"error-creating-snmp-session-error-message","errorCode":null,"errorMessage":"Error creating SNMP session: ${error.message}","messagePattern":"Error creating SNMP session: (.+?)","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"server/monitor-types/snmp.js","lineNumber":39,"sourceCode":"            if (monitor.snmpVersion === \"3\") {\n                if (!monitor.snmp_v3_username) {\n                    throw new Error(\"SNMPv3 username is required\");\n                }\n                // SNMPv3 currently defaults to noAuthNoPriv.\n                // Supporting authNoPriv / authPriv requires additional inputs\n                // (auth/priv protocols, passwords), validation, secure storage,\n                // and database migrations, which is intentionally left for\n                // a follow-up PR to keep this change scoped.\n                sessionOptions.securityLevel = snmp.SecurityLevel.noAuthNoPriv;\n                sessionOptions.username = monitor.snmp_v3_username;\n                session = snmp.createV3Session(monitor.hostname, monitor.snmp_v3_username, sessionOptions);\n            } else {\n                session = snmp.createSession(monitor.hostname, monitor.radiusPassword, sessionOptions);\n            }\n\n            // Handle errors during session creation\n            session.on(\"error\", (error) => {\n                throw new Error(`Error creating SNMP session: ${error.message}`);\n            });\n\n            const varbinds = await new Promise((resolve, reject) => {\n                session.get([monitor.snmpOid], (error, varbinds) => {\n                    error ? reject(error) : resolve(varbinds);\n                });\n            });\n            log.debug(\n                this.name,\n                `SNMP: Received varbinds (Type: ${snmp.ObjectType[varbinds[0].type]} Value: ${varbinds[0].value})`\n            );\n\n            if (varbinds.length === 0) {\n                throw new Error(`No varbinds returned from SNMP session (OID: ${monitor.snmpOid})`);\n            }\n\n            if (varbinds[0].type === snmp.ObjectType.NoSuchInstance) {\n                throw new Error(`The SNMP query returned that no instance exists for OID ${monitor.snmpOid}`);","sourceCodeStart":21,"sourceCodeEnd":57,"githubUrl":"https://github.com/louislam/uptime-kuma/blob/6b5ea0155793e666666745fb8d6fef1e829543a2/server/monitor-types/snmp.js#L21-L57","documentation":"Thrown from the 'error' event handler registered on a net-snmp Session/V3Session. net-snmp emits 'error' when the underlying UDP socket experiences an asynchronous failure that is not tied to a specific in-flight request (e.g. socket bind error, EACCES, ENETUNREACH on send). The handler stringifies error.message into a new Error and re-throws it. Note: because this fires inside an EventEmitter callback, the throw does NOT reject the awaited session.get() Promise on line 42 — it surfaces as an uncaught exception, so the surrounding monitor run is terminated abnormally.","triggerScenarios":"Triggered when snmp.createSession/createV3Session succeeds in returning a session object but the underlying dgram socket later emits 'error'. Common causes: binding to a restricted/invalid source port, sending on a down interface, UDP ICMP port-unreachable from the agent (ECONNREFUSED), or a malformed transport error raised by net-snmp internals after construction.","commonSituations":"Misconfigured SNMP port (e.g. port already bound), agent host unreachable at the IP layer after the session was created, SNMPv3 session created with a username but the agent actively rejects, or running under a user without raw/datagram socket privileges. Also seen when the session object is reused after close().","solutions":["Verify network reachability of the agent host and that UDP port 161 (or the configured monitor.port) is open and not blocked by a firewall.","Confirm the Uptime-Kuma process has permission to open UDP sockets on the chosen port and that no other process is squatting on it.","If using SNMPv3, ensure the username and securityLevel (noAuthNoPriv) are accepted by the agent; capture a packet trace to see if the agent returns an SNMP report error.","Refactor the handler so socket errors reject the active request: instead of 'throw' inside session.on('error'), store the error and reject the pending Promise, or attach the handler inside the Promise executor where reject is in scope."],"exampleFix":"// before\nsession.on(\"error\", (error) => {\n    throw new Error(`Error creating SNMP session: ${error.message}`);\n});\n\n// after — wire the socket error to the in-flight request so the throw actually rejects\nconst varbinds = await new Promise((resolve, reject) => {\n    session.get([monitor.snmpOid], (error, varbinds) => {\n        error ? reject(error) : resolve(varbinds);\n    });\n});","handlingStrategy":"try-catch","validationCode":"// Before creating the session, sanity-check reachability so socket errors surface early.\nconst { promisify } = require(\"util\");\nconst dnsLookup = promisify(require(\"dns\").lookup);\nasync function preflightSnmp(host) {\n  try { await dnsLookup(host); }\n  catch (e) { throw new Error(`SNMP host unreachable: ${e.message}`); }\n}","typeGuard":"function isSnmpSession(obj) {\n  return obj && typeof obj.get === \"function\" && typeof obj.on === \"function\" && typeof obj.close === \"function\";\n}","tryCatchPattern":"// Wrap the whole get in a single Promise and reject on BOTH the callback error and the socket 'error' event.\nconst varbinds = await new Promise((resolve, reject) => {\n  const onError = (err) => reject(err);\n  session.on(\"error\", onError);\n  session.get([monitor.snmpOid], (error, vbs) => {\n    session.off(\"error\", onError);\n    error ? reject(error) : resolve(vbs);\n  });\n});","preventionTips":["Validate agent hostname reachability before creating the session.","Always attach the 'error' listener before performing the first request so no socket error is ever unhandled.","Wire socket errors to the in-flight Promise instead of throwing inside the EventEmitter callback."],"tags":["snmp","network","event-emitter","net-snmp","udp"],"backgroundTag":null,"analyzedSha":"6b5ea0155793e666666745fb8d6fef1e829543a2","analyzedAt":"2026-08-12T23:42:12.959Z","schemaVersion":2},"datasetVersion":"2026-08-13T04:17:16.726Z"}