Raj
Raj

Reputation: 329

asyncio "Task was destroyed but it is pending!" in pysnmp sample program

I am testing pysnmp asyncio module and as the start used the sample program provided along with the documentation. When I run the sample program, it gives the Task was destroyed but it is pending! error. I checked SO for similar questions and could not find what is wrong with my (inexperienced) eyes. I am using Python 3.4.2 and asyncio that came with it and pysnmp (4.3.2) on Debian 8.5

The program I using (the same as the sample program in pysnmp documentation)

 
    import asyncio
    from pysnmp.hlapi.asyncio import *


    @asyncio.coroutine
    def getone(snmpEngine, hostname):
        errorIndication, errorStatus, errorIndex, varBinds = yield from getCmd(
            snmpEngine,
            CommunityData('public'),
            UdpTransportTarget(hostname),
            ContextData(),
            ObjectType(ObjectIdentity('SNMPv2-MIB', 'sysDescr', 0))
        )

        if errorIndication:
            print(errorIndication)
        elif errorStatus:
            print('%s at %s' % (
                errorStatus.prettyPrint(),
                errorIndex and varBinds[int(errorIndex) - 1][0] or '?'
            )
                  )
        else:
            for varBind in varBinds:
                print(' = '.join([x.prettyPrint() for x in varBind]))


    @asyncio.coroutine
    def getall(snmpEngine, hostnames):
        for hostname in hostnames:
            yield from getone(snmpEngine, hostname)


    snmpEngine = SnmpEngine()

    loop = asyncio.get_event_loop()
    loop.run_until_complete(getall(snmpEngine, [('demo.snmplabs.com', 1161),
                                                ('demo.snmplabs.com', 2161),
                                                ('demo.snmplabs.com', 3161)]))
  

Error is:

 

     Executing  wait_for= cb=[_raise_stop_error() at /usr/lib/python3.4/asyncio/base_event
    s.py:101] created at /usr/lib/python3.4/asyncio/base_events.py:264> took 0.460 seconds
    SNMPv2-MIB::sysDescr.0 = SunOS zeus.snmplabs.com 4.1.3_U1 1 sun4m
    SNMPv2-MIB::sysDescr.0 = SunOS zeus.snmplabs.com 4.1.3_U1 1 sun4m
    SNMPv2-MIB::sysDescr.0 = SunOS zeus.snmplabs.com 4.1.3_U1 1 sun4m
    Task was destroyed but it is pending!
    source_traceback: Object created at (most recent call last):
      File "multiple-sequential-queries.py", line 58, in 
        ('demo.snmplabs.com', 3161)]))   
      File "/usr/lib/python3.4/asyncio/base_events.py", line 271, in run_until_complete
        self.run_forever()
      File "/usr/lib/python3.4/asyncio/base_events.py", line 244, in run_forever
        self._run_once()
      File "/usr/lib/python3.4/asyncio/base_events.py", line 1075, in _run_once
        handle._run()
      File "/usr/lib/python3.4/asyncio/events.py", line 120, in _run
        self._callback(*self._args)
      File "/usr/lib/python3.4/asyncio/tasks.py", line 237, in _step
        result = next(coro)
      File "/usr/lib/python3.4/asyncio/coroutines.py", line 79, in __next__
        return next(self.gen)
      File "multiple-sequential-queries.py", line 50, in getall
        yield from getone(snmpEngine, hostname)
      File "/usr/lib/python3.4/asyncio/coroutines.py", line 79, in __next__
        return next(self.gen)
      File "multiple-sequential-queries.py", line 31, in getone
        ObjectType(ObjectIdentity('SNMPv2-MIB', 'sysDescr', 0))
      File "/usr/lib/python3.4/asyncio/coroutines.py", line 79, in __next__
        return next(self.gen)
      File "/usr/lib/python3.4/asyncio/coroutines.py", line 141, in coro
        res = func(*args, **kw)
      File "/usr/local/lib/python3.4/dist-packages/pysnmp/hlapi/asyncio/cmdgen.py", line 138, in getCmd
        addrName, paramsName = lcd.configure(snmpEngine, authData, transportTarget)
      File "/usr/local/lib/python3.4/dist-packages/pysnmp/hlapi/lcd.py", line 87, in configure
        transport
      File "/usr/local/lib/python3.4/dist-packages/pysnmp/entity/config.py", line 308, in addTransport
        transport)
      File "/usr/local/lib/python3.4/dist-packages/pysnmp/carrier/asyncio/dispatch.py", line 70, in registerTransport
        self.loopingcall = asyncio.async(self.handle_timeout())
    task: :4> wait_for= created at /usr/local/lib/python3.4/dist-packages/pysnmp/carrier/asyncio/dispatch.py:70>

 

Any help to figure this out is much appreciated!

Thanks.

Upvotes: 1

Views: 1064

Answers (2)

Lex Li
Lex Li

Reputation: 63123

As the new maintainer of this library, our team hold a different opinion from Ilya.

Like we explained in this GitHub issue, the users of SnmpEngine type should explicitly free up resources (including tasks) by calling SnmpEngine.transportDispatcher.closeDispatcher().

Upvotes: 0

Ilya Etingof
Ilya Etingof

Reputation: 5555

There is an internal timer function in pysnmp which is used for handling caches, retries etc. With asyncio transport that timer is driven by asyncio Future. The message you observe warns you about that Future object is still pending right before main loop is shut down.

To fix that you need to cancel pending timer task once you are done with all the SNMP I/O. A [hackerish] way to do that would be to append the following snippet to the example script you mentioned:

...

snmpEngine = SnmpEngine()

loop = asyncio.get_event_loop()

loop.run_until_complete(getall(snmpEngine, [('demo.snmplabs.com', 1161),
                                            ('demo.snmplabs.com', 2161),
                                            ('demo.snmplabs.com', 3161)]))

# run a coroutine to cancel pending timer task

from pysnmp.hlapi.asyncore.cmdgen import lcd

@asyncio.coroutine
def unconfigure(snmpEngine, authData=None):
    lcd.unconfigure(snmpEngine, authData)

loop.run_until_complete(unconfigure(snmpEngine))

I am working on adding similar coroutine into pysnmp so you could run it out-of-the-box.

Upvotes: 1

Related Questions