[New-bugs-announce] [issue37228] UDP sockets created by create_datagram_endpoint() allow by default multiple processes to bind the same port

Jukka Väisänen report at bugs.python.org
Tue Jun 11 05:36:54 EDT 2019


New submission from Jukka Väisänen <vaizki at vaizki.fi>:

When using loop.create_datagram_endpoint(), the default for reuse_address=True, which sets the SO_REUSEADDR sockopt for the UDP socket. 

This is a dangerous and unreasonable default for UDP, because in Linux it allows multiple processes to create listening sockets for the same UDP port and the kernel will randomly give incoming packets to these processes.

I discovered this by accidentally starting two Python asyncio programs with the same UDP port and instead of getting an exception that the address is already in use, everything looked to be working except half my packets went to the wrong process.

The documentation also refers to behavior with TCP sockets:
https://docs.python.org/3/library/asyncio-eventloop.html#asyncio.loop.create_datagram_endpoint

"reuse_address tells the kernel to reuse a local socket in TIME_WAIT state, without waiting for its natural timeout to expire. If not specified will automatically be set to True on Unix."

This is true for TCP but not UDP.

Either the documentation should reflect the current (dangerous) behavior or the behavior should be changed for UDP sockets. 

Workaround is of course to create your own socket without SO_REUSEADDR and pass it to create_datagram_endpoint().

----------
components: asyncio
messages: 345209
nosy: Jukka Väisänen, asvetlov, yselivanov
priority: normal
severity: normal
status: open
title: UDP sockets created by create_datagram_endpoint() allow by default  multiple processes to bind the same port
type: behavior
versions: Python 3.7

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue37228>
_______________________________________


More information about the New-bugs-announce mailing list