[issue27822] Fail to create _SelectorTransport with unbound socket

2019-12-06 Thread STINNER Victor


STINNER Victor  added the comment:

> In writing a simple UDP client using asyncio, I tripped over a call to 
> getsockname() in the _SelectorTransport class in asyncio/selector_events.py. 
> (...)

This bug has been fixed recently by:

commit 63deaa5b70108ef441c57728322da6b4321db4fc
Author: Vincent Michel 
Date:   Tue May 7 19:18:49 2019 +0200

bpo-31922: Do not connect UDP sockets when broadcast is allowed (GH-423)


Extract of the change:

diff --git a/Lib/asyncio/selector_events.py b/Lib/asyncio/selector_events.py
index 93b6889509..29968214f8 100644
--- a/Lib/asyncio/selector_events.py
+++ b/Lib/asyncio/selector_events.py
@@ -587,7 +587,10 @@ class _SelectorTransport(transports._FlowControlMixin,
 def __init__(self, loop, sock, protocol, extra=None, server=None):
 super().__init__(extra, loop)
 self._extra['socket'] = sock
-self._extra['sockname'] = sock.getsockname()
+try:
+self._extra['sockname'] = sock.getsockname()
+except OSError:
+self._extra['sockname'] = None
 if 'peername' not in self._extra:
 try:
 self._extra['peername'] = sock.getpeername()


Thanks for your contribution and your proposed patch Paul McGuire! Sorry for 
the late reply. I mark this change as a duplicate of bpo-31922.

--
resolution:  -> duplicate
stage:  -> resolved
status: open -> closed
superseder:  -> Can't receive replies from multicast UDP with asyncio
versions: +Python 3.9 -Python 3.5, Python 3.6

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue27822] Fail to create _SelectorTransport with unbound socket

2016-08-21 Thread Paul McGuire

Paul McGuire added the comment:

Patch file attached.

--
keywords: +patch
Added file: http://bugs.python.org/file44182/ptm_27822.patch

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue27822] Fail to create _SelectorTransport with unbound socket

2016-08-21 Thread Guido van Rossum

Guido van Rossum added the comment:

Can you supply a patch yourself?

--Guido (mobile)

--

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue27822] Fail to create _SelectorTransport with unbound socket

2016-08-21 Thread Paul McGuire

Paul McGuire added the comment:

(issue applies to both 3.5.2 and 3.6)

--
versions: +Python 3.5

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue27822] Fail to create _SelectorTransport with unbound socket

2016-08-21 Thread Paul McGuire

Paul McGuire added the comment:

To clarify how I'm using a socket without a bound address, I am specifying the 
destination address in the call to transport.sendto(), so there is no address 
on the socket itself, hence getsockname() fails.

--

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue27822] Fail to create _SelectorTransport with unbound socket

2016-08-21 Thread Paul McGuire

New submission from Paul McGuire:

In writing a simple UDP client using asyncio, I tripped over a call to 
getsockname() in the _SelectorTransport class in asyncio/selector_events.py.


def __init__(self, loop, sock, protocol, extra=None, server=None):
super().__init__(extra, loop)
self._extra['socket'] = sock
self._extra['sockname'] = sock.getsockname()

Since this is a sending-only client, the socket does not get bound to an 
address. On Linux, this is not a problem; getsockname() will return ('0.0.0.0', 
0) for IPV4, ('::', 0, 0, 0) for IPV6, and so on. But on Windows, a socket that 
is not bound to an address will raise this error when getsockname() is called:

OSError: [WinError 10022] An invalid argument was supplied

This forces me to write a wrapper for the socket to intercept getsockname() and 
return None.

In asyncio/proactor_events.py, this is guarded against, with this code in the 
_ProactorSocketTransport class:

try:
self._extra['sockname'] = sock.getsockname()
except (socket.error, AttributeError):
if self._loop.get_debug():
logger.warning("getsockname() failed on %r",
 sock, exc_info=True)


Please add similar guarding code to the _SelectorTransport class in 
asyncio/selector_events.py.

--
components: asyncio
messages: 273290
nosy: Paul McGuire, gvanrossum, haypo, yselivanov
priority: normal
severity: normal
status: open
title: Fail to create _SelectorTransport with unbound socket
type: behavior
versions: Python 3.6

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com