Skip to content

Commit

Permalink
Fix #1: FileNotFoundError on multiple SnmpSession usages
Browse files Browse the repository at this point in the history
  • Loading branch information
dvolodin7 committed Feb 17, 2023
1 parent 81c9288 commit bb15fa5
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 20 deletions.
6 changes: 5 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ To see unreleased changes, please see the [CHANGELOG on the main branch guide](h
### Fixed

* Fix SnmpSession allow_bulk handling.
* Fix [#1][#1]: Getting FileNotFoundError exception if multiple instances
of SnmpSession were previously used.

### Infrastructure

Expand All @@ -25,4 +27,6 @@ To see unreleased changes, please see the [CHANGELOG on the main branch guide](h

### Added

* Initial implementation
* Initial implementation

[#1]: https://github.com/gufolabs/gufo_snmp/issues/1
26 changes: 18 additions & 8 deletions src/gufo/snmp/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,10 +115,13 @@ async def get(self: "SnmpSession", oid: str) -> ValueType:

async def get_response() -> ValueType:
while True:
r_ev = asyncio.Event()
# Wait until data will be available
r_ev = asyncio.Event()
loop.add_reader(self._fd, r_ev.set)
await r_ev.wait()
try:
await r_ev.wait()
finally:
loop.remove_reader(self._fd)
# Read data or get BlockingIOError
# if no valid data available.
try:
Expand All @@ -130,8 +133,10 @@ async def get_response() -> ValueType:
# Wait for socket became writable
w_ev = asyncio.Event()
loop.add_writer(self._fd, w_ev.set)
await w_ev.wait()
loop.remove_writer(self._fd)
try:
await w_ev.wait()
finally:
loop.remove_writer(self._fd)
# Send request
self._sock.send_get(oid)
# Await response or timeout
Expand Down Expand Up @@ -167,10 +172,13 @@ async def get_many(

async def get_response() -> Dict[str, ValueType]:
while True:
r_ev = asyncio.Event()
# Wait until data will be available
r_ev = asyncio.Event()
loop.add_reader(self._fd, r_ev.set)
await r_ev.wait()
try:
await r_ev.wait()
finally:
loop.remove_reader(self._fd)
# Read data or get BlockingIOError
# if no valid data available.
try:
Expand All @@ -182,8 +190,10 @@ async def get_response() -> Dict[str, ValueType]:
# Wait for socket became writable
w_ev = asyncio.Event()
loop.add_writer(self._fd, w_ev.set)
await w_ev.wait()
loop.remove_writer(self._fd)
try:
await w_ev.wait()
finally:
loop.remove_writer(self._fd)
# Send request
self._sock.send_get_many(list(oids))
# Await response or timeout
Expand Down
18 changes: 11 additions & 7 deletions src/gufo/snmp/getbulk.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,13 @@ async def __anext__(self: "GetBulkIter") -> Tuple[str, ValueType]:

async def get_response() -> List[Tuple[str, ValueType]]:
while True:
r_ev = asyncio.Event()
# Wait until data will be available
r_ev = asyncio.Event()
loop.add_reader(self._fd, r_ev.set)
await r_ev.wait()
try:
await r_ev.wait()
finally:
loop.remove_reader(self._fd)
# Read data or get BlockingIOError
# if no valid data available.
try:
Expand All @@ -69,11 +72,12 @@ async def get_response() -> List[Tuple[str, ValueType]]:
raise StopAsyncIteration
# Send request
loop = asyncio.get_running_loop()
# Wait for socket became writable
w_ev = asyncio.Event()
loop.add_writer(self._fd, w_ev.set)
await w_ev.wait()
loop.remove_writer(self._fd)
r_ev = asyncio.Event()
loop.add_writer(self._fd, r_ev.set)
try:
await r_ev.wait()
finally:
loop.remove_writer(self._fd)
# Send request
self._sock.send_getbulk(self._ctx)
# Await response or timeout
Expand Down
13 changes: 9 additions & 4 deletions src/gufo/snmp/getnext.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,13 @@ async def __anext__(self: "GetNextIter") -> Tuple[str, ValueType]:

async def get_response() -> Tuple[str, ValueType]:
while True:
r_ev = asyncio.Event()
# Wait until data will be available
r_ev = asyncio.Event()
loop.add_reader(self._fd, r_ev.set)
await r_ev.wait()
try:
await r_ev.wait()
finally:
loop.remove_reader(self._fd)
# Read data or get BlockingIOError
# if no valid data available.
try:
Expand All @@ -58,8 +61,10 @@ async def get_response() -> Tuple[str, ValueType]:
# Wait for socket became writable
w_ev = asyncio.Event()
loop.add_writer(self._fd, w_ev.set)
await w_ev.wait()
loop.remove_writer(self._fd)
try:
await w_ev.wait()
finally:
loop.remove_writer(self._fd)
# Send request
self._sock.send_getnext(self._ctx)
# Await response or timeout
Expand Down
18 changes: 18 additions & 0 deletions tests/test_snmp.py
Original file line number Diff line number Diff line change
Expand Up @@ -341,3 +341,21 @@ async def inner() -> None:
assert n > 0

asyncio.run(inner())


@pytest.mark.parametrize("allow_bulk", [False, True])
def test_fetch_file_not_found(allow_bulk: bool, snmpd: Snmpd) -> None:
"""Test issue #1 condition."""

async def inner() -> None:
for _ in range(10):
async with SnmpSession(
addr=SNMPD_ADDRESS,
port=SNMPD_PORT,
community=SNMP_COMMUNITY,
allow_bulk=allow_bulk,
) as session:
async for _ in session.fetch("1.3.6.1.2.1.1.3"):
pass

asyncio.run(inner())

0 comments on commit bb15fa5

Please sign in to comment.