Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Bug]: Synchronous API fails if run in thread context #78

Closed
kroeschl opened this issue Jun 25, 2024 · 2 comments
Closed

[Bug]: Synchronous API fails if run in thread context #78

kroeschl opened this issue Jun 25, 2024 · 2 comments
Labels
area:pysnmp PySNMP package bug Something isn't working wontfix This will not be worked on

Comments

@kroeschl
Copy link

Expected behavior

The synchronous API (pysnmp.hlapi.getCmd and friends) should function when run in a thread or other contexts which may not have a running event loop.

Actual behavior

When running e.g. getCmd in a thread, I see the following error:

Exception in thread Thread-1 (get):
Traceback (most recent call last):
  File ".../python3.12/threading.py", line 1073, in _bootstrap_inner
    self.run()
  File ".../python3.12/threading.py", line 1010, in run
    self._target(*self._args, **self._kwargs)
  File ".../repro.py", line 10, in get
    pysnmp.hlapi.getCmd(
  File ".../python3.12/site-packages/pysnmp/hlapi/asyncio/sync/cmdgen.py", line 97, in getCmd
    loop = asyncio.get_event_loop()
           ^^^^^^^^^^^^^^^^^^^^^^^^
  File ".../python3.12/asyncio/events.py", line 702, in get_event_loop
    raise RuntimeError('There is no current event loop in thread %r.'
RuntimeError: There is no current event loop in thread 'Thread-1 (get)'.

Detailed steps

Here's a simple reproducing script:

import threading

import pysnmp.hlapi
import pysnmp.hlapi.asyncio


def get():
    ip = "10.0.122.191"
    oid = "1.3.6.1.4.1.318.1.1.4.4.2.1.3.7"
    pysnmp.hlapi.getCmd(
        pysnmp.hlapi.SnmpEngine(),
        pysnmp.hlapi.CommunityData("private"),
        pysnmp.hlapi.UdpTransportTarget((ip, 161)),
        pysnmp.hlapi.ContextData(),
        pysnmp.hlapi.ObjectType(pysnmp.hlapi.ObjectIdentity(oid), pysnmp.hlapi.Integer(0)),
    )
    print("Complete")


get()
x = threading.Thread(target=get)
x.start()
x.join()

I'm working around this right now by just using the async API via

asyncio.run(pysnmp.hlapi.asyncio.getCmd(...))

Python package information

6.1.2

Operating system information

Debian 13, Windows 10

Python information

Python 3.8.12, 3.9.13, 3.12.4

(Optional) Contents of your test script

No response

Relevant log output

No response

@kroeschl kroeschl added bug Something isn't working triage New issues that need to be sorted out. labels Jun 25, 2024
@lextm lextm added up for grabs Pull requests are welcome area:pysnmp PySNMP package priority:low Low priority items. and removed triage New issues that need to be sorted out. labels Jul 10, 2024
@lextm
Copy link

lextm commented Jul 10, 2024

The primary goal to introduce those sync methods was to enable quick migration to 6.x releases, but we don't feel the current PySNMP code base was well designed with all asyncio best practices.

The current plan to retire the sync version of those methods in a future release, so people are suggested to fully migrate to the async version.

@lextm lextm added wontfix This will not be worked on and removed up for grabs Pull requests are welcome priority:low Low priority items. labels Jul 17, 2024
@lextm
Copy link

lextm commented Jul 17, 2024

We decided to retire all sync API based on asyncio in release 6.2, and no longer pursue on a solution here.

If you intend to use asyncio based async API in sync pattern, that's your tasks now and you should build your own wrapper.

@lextm lextm closed this as completed Jul 17, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area:pysnmp PySNMP package bug Something isn't working wontfix This will not be worked on
Projects
None yet
Development

No branches or pull requests

2 participants