This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: asyncio.selector_events._SelectorTransport: Add logging when sock.getpeername() fails
Type: Stage:
Components: asyncio Versions: Python 3.8
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: asvetlov, dsternlicht, yselivanov
Priority: normal Keywords: patch

Created on 2020-02-20 13:00 by dsternlicht, last changed 2022-04-11 14:59 by admin.

Files
File name Uploaded Description Edit
log-peername-and-sockname-errors.patch dsternlicht, 2020-02-20 15:36
Messages (3)
msg362317 - (view) Author: David (dsternlicht) Date: 2020-02-20 13:00
`sock.getpeername` can fail for multiple reasons (see https://pubs.opengroup.org/onlinepubs/7908799/xns/getpeername.html) but in  `asyncio.selector_events._SelectorTransport` it's try/excepted without any logging of the error:

```
        if 'peername' not in self._extra:
            try:
                self._extra['peername'] = sock.getpeername()
            except socket.error:
                self._extra['peername'] = None
```

This makes it very difficult to debug. Would it be OK if I added here a log with information on the error?

Thanks!
msg362588 - (view) Author: Andrew Svetlov (asvetlov) * (Python committer) Date: 2020-02-24 14:21
I'm not sure.
Logging can pollute the logger and make it almost useless.
Most of mentioned errno codes are programming errors, a few may happen due network issues.
Would you share what reasons did you observe?
msg362595 - (view) Author: David (dsternlicht) Date: 2020-02-24 15:51
Hi asvetlov,

Thank you for your reply.

I'm currently trying to debug a network issue, but I cannot determine the root cause of it because of lack of logs. It would be extremely helpful for my debugging if we could log the error that was raised by getpeername.

I noticed that in asyncio.proactor_events._set_socket_extra there *is* some logging of exceptions. 

```
def _set_socket_extra(transport, sock):
    transport._extra['socket'] = trsock.TransportSocket(sock)

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

    if 'peername' not in transport._extra:
        try:
            transport._extra['peername'] = sock.getpeername()
        except socket.error:
            # UDP sockets may not have a peer name
            transport._extra['peername'] = None
```

Although I see that there there's also a check `if transport._loop.get_debug()` so that it won't pollute the log. Would you like me to add that check to my patch too?

Thanks!
History
Date User Action Args
2022-04-11 14:59:26adminsetgithub: 83881
2020-02-24 15:51:51dsternlichtsetmessages: + msg362595
2020-02-24 14:21:40asvetlovsetmessages: + msg362588
2020-02-20 15:36:59dsternlichtsetfiles: + log-peername-and-sockname-errors.patch
keywords: + patch
2020-02-20 13:00:09dsternlichtcreate