chrysn / aiocoap

The Python CoAP library
Other
264 stars 119 forks source link

Keeping DTLS session alive #337

Open anyc opened 8 months ago

anyc commented 8 months ago

Hello,

I created a client context with create_client_context() and send GET requests using Message() and ctx.request() from time to time. On the server side (libcoap), I noticed that the session is closed immediately after the request and on the client side, I see that a new connection is started on every request:

INFO:coap:No DTLS connection active to (192.168.0.1, 5684, b'user'), creating one

The context is stored in an object that is referenced so the same context should stay "open" during application runtime. For my experimental protocol, it is necessary that the session stays open between requests as the server stores a list of held locks and they are released automatically when the session closes.

Hence, is there a way to keep a (DTLS) session open until it is explicitly shutdown or the context is "garbage collected"?

I started pdb inside the tinydtls _remove_from_pool() method to find out who shuts down the connection but it looks like this is just a task that was scheduled somewhere else:

[...]
-> self.loop.run_forever()
  /usr/lib/python3.11/asyncio/base_events.py(607)run_forever()
-> self._run_once()
  /usr/lib/python3.11/asyncio/base_events.py(1923)_run_once()
-> handle = None  # Needed to break cycles when an exception occurs.
  /home/coap/venv/lib/python3.11/site-packages/aiocoap/transports/tinydtls.py(261)__del__()
-> self.shutdown()
> /home/coap/venv/lib/python3.11/site-packages/aiocoap/transports/tinydtls.py(241)shutdown()
-> self._remove_from_pool()

Thank you!

Python version: 3.11.6 (main, Oct  8 2023, 05:06:43) [GCC 13.2.0]
aiocoap version: 0.4.7
Modules missing for subsystems:
    dtls: everything there
    oscore: missing cbor2, cryptography, filelock, ge25519
    linkheader: everything there
    prettyprint: missing cbor2, termcolor, pygments
Python platform: linux
Default server transports:  tinydtls:tcpserver:tcpclient:tlsserver:tlsclient:udp6
Selected server transports: tinydtls:tcpserver:tcpclient:tlsserver:tlsclient:udp6
Default client transports:  tinydtls:tcpclient:tlsclient:udp6
Selected client transports: tinydtls:tcpclient:tlsclient:udp6
SO_REUSEPORT available (default, selected): True, True
anyc commented 8 months ago

Looks like the session stays open if I keep the last response from await ctx.request(myreq).response referenced somewhere. I can live with this but more explicit control over this behavior would be good.