Issue
Say I have this program in Python the "connects" a UDP socket:
import socket
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
sock.connect(('8.8.8.8', 53))
Can the call to connect hang, and so it's better to set a timeout? So something like:
import socket
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
sock.settimeout(2)
sock.connect(('8.8.8.8', 53))
If so, why? I would have thought connect when using UDP wouldn't connect anything, and at worse it would immediately fail for some reason, rather than hang.
This is in reference to a bug reported in an asyncio Python DNS resolver I wrote: https://github.com/michalc/aiodnsresolver/pull/35 where is does seem to hang at connect, but I'm not sure why.
Solution
It should not hang, because it is not actually "connecting" to anything. UDP is connection-less, after all. Calling connect()
on a UDP socket merely creates a static association with the specific peer IP/Port on the local end of the socket, so subsequent sends/receives on the socket will communicate only with that peer. So there is nothing to hang on. If you are experiencing a hang on connect()
on a UDP socket, then that would have to be a bug in your system's underlying socket stack, not with your code.
Answered By - Remy Lebeau
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.