asyncio "Task was destroyed but it is pending!" in pysnmp sample program
<p>I am testing pysnmp asyncio module and as the start used the <a href="http://pysnmp.sourceforge.net/_downloads/multiple-sequential-queries.py" rel="nofollow">sample program</a> provided along with the documentation. When I run the sample program, it gives the <code>Task was destroyed but it is pending!</code> 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</p>

<p>The program I using (the same as the sample program in pysnmp documentation)</p>

<pre class="lang-py prettyprint-override"> <code>
    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)]))
 </code> </pre>

<p>Error is:</p>

<pre> <code>

     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>

</code> </pre>

<p>Any help to figure this out is much appreciated!</p>

<p>Thanks.</p>

<p>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 <code>Future</code>. The message you observe warns you about that Future object is still pending right before main loop is shut down.</p>

<p>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:</p>

<pre><code>...

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))
</code></pre>

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

python python-3.x python-asyncio pysnmp