Repository navigation
Closed
Labels
3.7 (EOL)end of lifeextension-modulesC modules in the Modules dirtype-bugAn unexpected behavior, bug, or error
Description
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
assignee = 'https://github.com/tiran'
closed_at = <Date 2021-10-21.12:05:20.757>
created_at = <Date 2016-09-13.19:13:31.625>
labels = ['extension-modules', 'type-bug', '3.7']
title = 'socket.socket(fileno=fd) does not work as documented'
updated_at = <Date 2021-10-21.12:05:20.756>
user = 'https://github.com/tiran'bugs.python.org fields:
activity = <Date 2021-10-21.12:05:20.756>
actor = 'christian.heimes'
assignee = 'christian.heimes'
closed = True
closed_date = <Date 2021-10-21.12:05:20.757>
closer = 'christian.heimes'
components = ['Extension Modules']
creation = <Date 2016-09-13.19:13:31.625>
creator = 'christian.heimes'
dependencies = []
files = ['44687']
hgrepos = []
issue_num = 28134
keywords = ['patch']
message_count = 25.0
messages = ['276327', '276328', '276330', '276363', '276395', '276660', '276684', '276866', '276879', '285972', '308433', '309207', '309225', '311175', '311176', '311178', '311181', '311252', '312681', '312684', '312690', '312708', '313153', '332704', '404590']
nosy_count = 8.0
nosy_names = ['pitrou', 'vstinner', 'christian.heimes', 'r.david.murray', 'njs', 'martin.panter', 'yselivanov', 'YoSTEALTH']
pr_nums = ['1349', '5435']
priority = 'normal'
resolution = 'fixed'
stage = 'resolved'
status = 'closed'
superseder = None
type = 'behavior'
url = 'https://bugs.python.org/issue28134'
versions = ['Python 3.5', 'Python 3.6', 'Python 3.7']Metadata
Metadata
Assignees
Labels
3.7 (EOL)end of lifeextension-modulesC modules in the Modules dirtype-bugAn unexpected behavior, bug, or error
Activity
tiran commentedon Sep 13, 2016
Documentation of socket.socket(fileno) https://docs.python.org/3/library/socket.html#socket.socket says:
The feature does not work. fileno does not infer the values for family, type and proto from the fd. Instead if uses the other arguments. I don't see how this feature should have ever worked on POSIX. There are no calls to getsockopt() with SO_DOMAIN, SO_TYPE and SO_PROTOCOL.
tiran commentedon Sep 13, 2016
I expected that socket.socket(fileno) would fill in all socket options like my own implementation https://github.com/tiran/socketfromfd/blob/master/socketfromfd.py
bitdancer commentedon Sep 13, 2016
See also bpo-27377?
vadmium commentedon Sep 14, 2016
The documentation says that the family, type and proto attributes correspond to the constructor arguments. Although it is unfortunate and quirky, I think your behaviour does match the documentation.
Do the mismatched settings cause any serious problems with socket methods, or just affect the Python-level attributes and repr()?
Perhaps the way forward is to deprecate fileno=... in favour of Neil’s fromfd2() function. That would avoid any confusion about conflicting socket() constructor arguments and defaults. I.e. what does socket(AF_INET, SOCK_STREAM, fileno=unix_datagram_fd) mean, and is it equivalent to socket(filno=unix_datagram_fd)?
tiran commentedon Sep 14, 2016
Martin, the documentation says "If fileno is specified, the other arguments are ignored, causing the socket with the specified file descriptor to return." It's a direct quote from Python 3's socket library documentation. For a non-native speaker like me, this sentence implies that socket.socket(fileno) not only ignores the arguments (which it does not) but that the constructor uses the fileno to set family, type and proto.
The socket module uses self->sock_family in several places to calculate the addr len or when it handles arguments and address information. Just look at this simple example:
vadmium commentedon Sep 16, 2016
I agree the doc is far from perfect. The bit I was going off is just above <https://docs.python.org/3.5/library/socket.html#socket.socket.family\>, saying “these (read-only) attributes that correspond to the values given to the socket constructor”.
My instinct would be to clarify that for existing versions 2.7, 3.5, etc, that the constructor arguments are _not_ ignored and should correspond to the file descriptor. Then in the next Python version we can make it more automatic using the getsockopt() techniques.
tiran commentedon Sep 16, 2016
How about we fix the code and only document the limitations instead? :) After all it works fine on Windows and it is documented to work on all operating systems. Since it's a bug we can fix it in 3.5, too.
My patch implements a best-effort to get type, family and proto from the socket. It ignores any errors.
vadmium commentedon Sep 18, 2016
Personally, I’m not too enthusiastic, because it is rather magical, and does not work in all cases. It seems more like a feature than a bug fix. But I have rarely used the fileno=... parameter, and it shouldn’t have much negative impact, so I’m not too fussed.
According to bpo-27377, these are some cases where parts won’t work:
Also, if we are going to read SO_PROTOCOL when fileno=... is given, why not also read it in the normal case when proto=0 (unspecified) is given?
tiran commentedon Sep 18, 2016
I'm well aware that it does not work in all cases. However it works good enough in most cases. Right now the fileno argument must be considered broken because it leads to wrong results. It is a problem and possible security issue for a couple of use cases, e.g. passing of sockets through AF_UNIX AUX data or systemd socket activation.
On Windows it is less problematic because socket(filno) works correctly with WSAPROTOCOL_INFO. It's only broken for integer fd.
I have considered to set type, family and proto to 0 (unspec) when the getsockopt and getsockname fail.
I have a differnt ticket for the protocol issue, bpo-27816.
tiran commentedon Jan 21, 2017
Here is another example of how broken and dangerous fileno argument is. getpeername() is neither a valid IPv4 tuple nor a valid IPv6 tuple. It's all messed up:
1st1 commentedon Dec 16, 2017
Let's quickly iterate over what's possible first:
It's possible to check the type of the FD using "getsockopt(SOL_SOCKET, SO_TYPE)" on all platforms.
It's possible to check family/proto of the FD using "getsockopt(SOL_SOCKET, SO_DOMAIN/SO_PROTOCOL)" on Linux.
Given the above I propose the following:
Passing a wrong type can be considered as a serious error. SOCK_STREAM is fundamentally different from SOCK_DGRAM. Because we have a way to validate socket type on all platforms, I propose to do this validation when we create a socket from an FD, and raise an error if the passed socket type isn't correct.
When Python is running with '-X dev' (new dev mode added by Victor), I propose to also validate socket.family and socket.proto on Linux, and raise a RuntimeWarning when they don't match.
1 remaining item
tiran commentedon Dec 30, 2017
Issue bpo-32454 adds socket.close(fd) function.
tiran commentedon Jan 29, 2018
New changeset b6e43af by Christian Heimes in branch 'master':
bpo-28134: Auto-detect socket values from file descriptor (bpo-1349)
b6e43af
tiran commentedon Jan 29, 2018
I'm leaving the ticket open to remind me that I have to add a whatsnew entry and maybe consider a backport.
vstinner commentedon Jan 29, 2018
x86-64 Sierra 3.x is grumpy:
http://buildbot.python.org/all/#/builders/14/builds/659
======================================================================
FAIL: test_uknown_socket_family_repr (test.test_socket.GeneralModuleTests)
----------------------------------------------------------------------
tiran commentedon Jan 29, 2018
The test for proto isn't super critical. It's mostly ignored any way. I'll submit a band-aid.
tiran commentedon Jan 30, 2018
New changeset 2e0ecde by Christian Heimes in branch 'master':
bpo-28134: Ignore proto in unknown socket test (GH-5435)
2e0ecde
YoSTEALTH commentedon Feb 24, 2018
I am using 3.7.0b1 i don't think this issue is fixed!
# simple mockup:
# --------------
So the "laddr" and "raddr" goes missing the second its out of the function???
This has wasted days of my time, to even get to this point of figuring out whats going on wasn't easy! extremely frustrating.
tiran commentedon Feb 24, 2018
The problem is fixed and your example behaves as expected.
The laddr string is the result of inside.getsockname() and raddr string is the result of inside.getpeername(). In your example, inside and client share the same file descriptor. When the function exits client goes out of scope, the reference count of client drops to 0, the object gets deallocated and Python closes the shared file descriptor. In outside, the shared fd is gone and outside.getsockname() and outside.getpeername() fail. Basically the outside socket is dead because its fd has been closed.
You have to duplicate the fd or detach the socket to keep the fd open.
YoSTEALTH commentedon Feb 24, 2018
Christian thank you for your reply, i really appreciate it.
Lets analyze this a bit:
id(client): 140340037397192
id(inside): 140340010863560
I could understand making a duplicate of the fd if it were being passed across thread/process but in its original thread making a duplicate! This behavior is extremely odd.
I fell like everyone that uses socket(fileno) will run into days wasted and frustration just to figure out these solutions.
tiran commentedon Feb 24, 2018
File descriptors are a advanced features and expose low level operating system resources. You really have to understand how the OS works. They cannot be reference counted. In fact they *are* the reference to entries in the Kernel's global open file table. I gave a talked about FDs at PyCon US two years ago, maybe https://speakerdeck.com/tiran/pycon-2016-file-descriptors-unix-sockets-and-other-posix-magic will help you understand fds better.
You can make your example work with https://docs.python.org/3/library/socket.html#socket.socket.detach
The feature works as intended. It's designed to turn an inherited file descriptor (e.g. systemd socket activation) or transfered fds (e.g. through AF_UNIX SCM_RIGHTS). In both cases the fd is already a duplicated fd.
YoSTEALTH commentedon Mar 2, 2018
It would be nice if "python" accounted for such low level os things. None the less client.detach() method works fine.
I really did enjoy your talk, kinda bummed it was short and didn't get into more details.
Thanks for your help and patience Christian :)
njsmith commentedon Dec 29, 2018
Am I right that this is considered fixed (or at least as fixed as it can be), and the issue should be closed?
Also, small note in case anyone stumbles across this in the future and is confused: Python does *not* handle this correctly on Windows. I suspect Christian was confused because there's an undocumented features on Windows where if you pass fileno=<opaque string returned by socket.share()>, then that correctly reinstantiates the socket object. But fileno=<raw socket handle> doesn't seem to do any special autodetection of type/family/proto.
tiran commentedon Oct 21, 2021
Yes, the fix works well enough.