Skip to content

Commit

Permalink
Protect against closing non-existant SNMP handles
Browse files Browse the repository at this point in the history
Snmp.__init__() can now raise exceptions before `self.handle` is ever
created, thereby causing errors to be logged from Snmp.__del_ everytime
the garbage collector deletes such defunct object.  This protects
against that slight problem.
  • Loading branch information
lunkwill42 committed Nov 2, 2023
1 parent 03eba24 commit a613588
Showing 1 changed file with 3 additions and 2 deletions.
5 changes: 3 additions & 2 deletions python/nav/Snmp/pynetsnmp.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ def __init__(
:param priv_password: SNMPv3 privacy password
"""

self.handle = None
self.host = host
self.community = str(community)
self.version = str(version)
Expand Down Expand Up @@ -197,7 +197,8 @@ def _build_cmdline(self):
return tuple(params)

def __del__(self):
self.handle.close()
if self.handle:
self.handle.close()

def get(self, query="1.3.6.1.2.1.1.1.0"):
"""Performs an SNMP GET query.
Expand Down

0 comments on commit a613588

Please sign in to comment.