UNIX socket and SOCK_DGRAM

264 views
Skip to first unread message

Victor Stinner

unread,
Feb 19, 2014, 7:22:09 AM2/19/14
to python-tulip
Hi,

I see that new methods were added to support the AF_UNIX family. The
socket type is hardcoded to SOCK_STREAM. It looks like AF_UNIX can be
used with SOCK_DGRAM.
http://stackoverflow.com/questions/18711996/using-abstract-sockets-with-af-unix-sock-dgram

Why not adding an option to make the user choose the socket type?

Victor

Guido van Rossum

unread,
Feb 19, 2014, 10:55:03 AM2/19/14
to Victor Stinner, python-tulip
Because that would require duplicating the logic in create_datagram_endpoint(), which is totally separate (all the way down to the I/O callbacks).

Feel free to add a feature request to the tracker for this though. Be sure to indicate an actual use case (rather than just abstract interest). E.g. do you expect to need this in OpenStack? How?
--
--Guido van Rossum (python.org/~guido)

Yury Selivanov

unread,
Feb 19, 2014, 11:02:15 AM2/19/14
to python...@googlegroups.com

On 2/19/2014, 10:55 AM, Guido van Rossum wrote:
> Because that would require duplicating the logic in
> create_datagram_endpoint(), which is totally separate (all the way down to
> the I/O callbacks).
Exactly. I was hesitant to do that myself because my experience
with UDP is fairly limited.

Yury

Victor Stinner

unread,
Feb 19, 2014, 11:37:06 AM2/19/14
to Guido van Rossum, python-tulip
2014-02-19 16:55 GMT+01:00 Guido van Rossum <gu...@python.org>:
> Because that would require duplicating the logic in
> create_datagram_endpoint(), which is totally separate (all the way down to
> the I/O callbacks).

Oh, I didn't notice that TCP and UDP are handled differently. I only
expected to pass type=SOCK_DGRAM and it would just work :-D

> Feel free to add a feature request to the tracker for this though. Be sure
> to indicate an actual use case (rather than just abstract interest). E.g. do
> you expect to need this in OpenStack? How?

The new UNIX methods were not documented. When I wrote the
documentation, I noticed that only SOCK_STREAM was supported. That's
why I asked the question. I don't need UNIX+SSL nor UNIX on UDP :-)

Victor

Guido van Rossum

unread,
Feb 19, 2014, 11:40:59 AM2/19/14
to Victor Stinner, python-tulip

Cool. Since we do have UNIX + SSL, maybe we should provide an example?

Ludovic Gasc

unread,
Aug 15, 2017, 6:52:29 PM8/15/17
to python-tulip, victor....@gmail.com, gu...@python.org
On Wednesday, February 19, 2014 at 4:55:03 PM UTC+1, Guido van Rossum wrote:
Because that would require duplicating the logic in create_datagram_endpoint(), which is totally separate (all the way down to the I/O callbacks).

Feel free to add a feature request to the tracker for this though. Be sure to indicate an actual use case (rather than just abstract interest). E.g. do you expect to need this in OpenStack? How?

Hi everybody,

I would wake-up this thread because I have a concrete use case to have that in AsyncIO, even if it would the only one concrete use case from a production environment:
systemd has a watchdog mechanism and a way to communicate between a daemon and systemd.
It uses a UNIX socket with DGRAM, the mechanism is named sd_notify.

For example, a Python that implements sd_notify: https://github.com/bb4242/sdnotify/blob/master/sdnotify/__init__.py#L35

More technical documentation about sd_notify protocol: https://www.freedesktop.org/software/systemd/man/sd_notify.html

In fact, if somebody has a workaround or a way to implement that gently with AsyncIO, it would be awesome, because I need to ping-pong with systemd to say that my daemon is still alive.
Thanks for your help.

I have tried to find a way with loop.create_datagram_endpoint() that would be the most closer what I want and a custom socket, I have this error:

   File "/usr/local/pythonz/pythons/CPython-3.6.2/lib/python3.6/asyncio/base_events.py", line 951, in create_datagram_endpoint
     yield from waiter
 AssertionError: yield from wasn't used with future

My piece of code:

sock = socket.socket(socket.AF_UNIX, socket.SOCK_DGRAM)
sock.setblocking(False)
sock.connect(os.environ["NOTIFY_SOCKET"])
transport, protocol = loop.create_datagram_endpoint(
lambda: WatchDogClientProtocol(loop),
sock=sock)

Guido van Rossum

unread,
Aug 17, 2017, 12:40:34 AM8/17/17
to Ludovic Gasc, python-tulip, Victor Stinner
OK, that looks like a real use case and proof that UNIX sockets used as datagrams are actually used. If you submit a PR (to CPython -- the separate asyncio repo is dead) it should be possible to get this into Python 3.7.

Ludovic Gasc

unread,
Aug 17, 2017, 3:21:49 AM8/17/17
to Guido van Rossum, Victor Stinner, python-tulip
Except if the implementation is a candy for somebody's else (please raise your hand ;-) ), I will follow the official process to contribute in CPython to add that.

Thanks Guido for your response. 

Ludovic Gasc

unread,
Aug 17, 2017, 5:34:21 AM8/17/17
to Victor Stinner, Guido van Rossum, python-tulip
2017-08-17 10:51 GMT+02:00 Victor Stinner <victor....@gmail.com>:
Hi Ludo,


2017-08-17 9:21 GMT+02:00 Ludovic Gasc <gml...@gmail.com>:
> I will follow the official process to contribute in CPython to add that.

I can help you on that part if you want ;-)

Thank you a lot Victor, I will contact you directly when I have something to show :-)

Ludovic Gasc

unread,
Aug 22, 2017, 6:09:55 PM8/22/17
to python-tulip, gu...@python.org, victor....@gmail.com
Hi,

Finally, it became a candy for one of my developers, he has made an issue+PR:

Thanks a lot Victor for your code review.

All automatic checks are green now, thanks for your feedbacks.

Have a nice week.
Reply all
Reply to author
Forward
0 new messages