chrysn/aiocoap

clean shutdown with dtls

Closed this issue · 2 comments

I am using the following script (credentials and exact URI based on https://github.com/glenndehaan/ikea-tradfri-coap-docs)

import asyncio
import aiocoap
import time
import json

from datetime import datetime


async def read():
    raw = {
        'coaps://192.168.1.127:5684/*': {'dtls': {'psk': b'PSK', 'client-identity': b'USER'}}
    }

    protocol = await aiocoap.Context.create_client_context()
    protocol.client_credentials.load_from_dict(raw)

    while True:
        msg = aiocoap.Message(code=aiocoap.GET, uri="coaps://HOST:5684/15001/65538", observe=0)

        pr = protocol.request(msg)
        async for result in pr.observation:
            data = json.loads(result.payload)
            state = data['15015'][0]
            print(datetime.now(), state['5536'])

asyncio.run(read())

The script works and reports data, but when I hit Ctrl-C, then I am getting error

TypeError: an integer is required

The full stack trace

CTraceback (most recent call last):
  File "/home/wrobell/projects/n23-sensor/aiocoap-read", line 30, in <module>
    asyncio.run(read())
  File "/usr/lib/python3.9/asyncio/runners.py", line 44, in run
    return loop.run_until_complete(main)
  File "/usr/lib/python3.9/asyncio/base_events.py", line 629, in run_until_complete
    self.run_forever()
  File "/usr/lib/python3.9/asyncio/base_events.py", line 596, in run_forever
    self._run_once()
  File "/usr/lib/python3.9/asyncio/base_events.py", line 1854, in _run_once
    event_list = self._selector.select(timeout)
  File "/usr/lib/python3.9/selectors.py", line 469, in select
    fd_event_list = self._selector.poll(timeout, max_ev)
KeyboardInterrupt
Exception ignored in: <function ClientObservation._Iterator.__del__ at 0x7f75769a3160>
Traceback (most recent call last):
  File "/home/wrobell/.local/lib/python3.9/site-packages/aiocoap/protocol.py", line 1102, in __del__
asyncio.exceptions.CancelledError:
/home/wrobell/.local/lib/python3.9/site-packages/aiocoap/transports/tinydtls.py:163: UserWarning: DTLS module did not shut down the DTLSSocket perfectly; it still tried to call _write in vain
TypeError: an integer is required
Exception ignored in: 'DTLSSocket.dtls._write'
Traceback (most recent call last):
  File "/home/wrobell/.local/lib/python3.9/site-packages/aiocoap/transports/tinydtls.py", line 244, in shutdown
TypeError: an integer is required
Exception ignored in: <function DTLSClientConnection.__del__ at 0x7f7575b729d0>
Traceback (most recent call last):
  File "/home/wrobell/.local/lib/python3.9/site-packages/aiocoap/transports/tinydtls.py", line 257, in __del__
  File "/home/wrobell/.local/lib/python3.9/site-packages/aiocoap/transports/tinydtls.py", line 250, in shutdown
  File "/usr/lib/python3.9/asyncio/selector_events.py", line 700, in close
  File "/usr/lib/python3.9/asyncio/base_events.py", line 746, in call_soon
  File "/usr/lib/python3.9/asyncio/base_events.py", line 510, in _check_closed
RuntimeError: Event loop is closed
Exception ignored in: <function RecvmsgSelectorDatagramTransport.__del__ at 0x7f75759ec160>
Traceback (most recent call last):
  File "/home/wrobell/.local/lib/python3.9/site-packages/aiocoap/util/asyncio/recvmsg.py", line 95, in __del__
  File "/home/wrobell/.local/lib/python3.9/site-packages/aiocoap/util/asyncio/recvmsg.py", line 83, in close
  File "/usr/lib/python3.9/asyncio/base_events.py", line 746, in call_soon
  File "/usr/lib/python3.9/asyncio/base_events.py", line 510, in _check_closed
RuntimeError: Event loop is closed
Exception ignored in: <function ClientObservation._Iterator.__del__ at 0x7f75769a3160>
Traceback (most recent call last):
  File "/home/wrobell/.local/lib/python3.9/site-packages/aiocoap/protocol.py", line 1102, in __del__
asyncio.exceptions.CancelledError:

Together with experimetnal DTLs server side support, 774a0c2 was added that provides a similar fix to #224; can you verify that this resolves your issue?

Closing as it is probably resolved and the fix neither confirmed nor rejected; please reopen if the issue was not fixed, or open a new issue if different errors appear now.