Repository navigation
add sniff sockopts - #804
Conversation
njsmith
left a comment
There was a problem hiding this comment.
Made a few comments below.
I don't understand why the tests are failing :-). For some reason it's consistently getting the type as SOCK_STREAM when it should be SOCK_DGRAM... do the tests pass for you locally? If they're only failing on Travis/Appveyor, then one hack is to add a bunch of print calls to trace what it's doing, and pushing that temporarily, so you can see what's happening in the logs. (Then of course you take the print calls out again before we merge :-).)
You'll also want to eventually add a newsfragment
| for family_type in ['ipv4_tcp', 'ipv4_udp', 'ipv6_tcp', 'ipv6_udp']: | ||
| family, type = family_type.split('_') | ||
| family = stdlib_socket.AF_INET if family == 'ipv4' else stdlib_socket.AF_INET6 | ||
| type = stdlib_socket.SOCK_STREAM if type == 'tcp' else stdlib_socket.SOCK_DGRAM |
There was a problem hiding this comment.
Maybe it would be nicer to write this like
for family in [stdlib_socket.AF_INET, stdlib_socket.AF_INET6]:
for type in [stdlib_socket.SOCK_STREAM, stdlib_socket.SOCK_DGRAM]:
...?
I guess it'd also be good to test AF_UNIX on systems where it's defined.
There was a problem hiding this comment.
Changed it, did a bit of wiggling to avoid over indentation/yapf butchering my code...
|
Just so it's clear, I haven't yet abandoned this, just incredibly busy with work and RL... |
|
No worries, work and RL definitely come first :-) |
Codecov Report
@@ Coverage Diff @@
## master #804 +/- ##
==========================================
- Coverage 99.4% 99.39% -0.02%
==========================================
Files 102 102
Lines 12717 12461 -256
Branches 928 915 -13
==========================================
- Hits 12641 12385 -256
Misses 56 56
Partials 20 20
|
|
RE newsfragments, is this considered a feature? Seems like a bit of a stretch, and I'm pretty sure this isn't a bugfix... |
|
(FYI: I edited the initial comment to include a I dug into this a bit more on Windows. Apparently the answer is that I was working from incorrect information, and it isn't expected to work on Windows! Also AFAICT it can't work on Windows – even the Speaking of validating assumptions, it looks like the macOS tests did fail exactly the way we expected. So... that's nice to have confirmed, I guess? And, squinting at this output: I notice that the OTOH AFAICT there's no hope of getting Up to you whether you want to skip the test on macOS and leave it at that for now, or implement the
I think calling it a feature is fine. Before you had to specify manually, now you don't, yay. It's not the biggest feature, but who's counting? |
|
I finally had a chance to dig into this a bit tonight. It's extremely weird. Running with 3.7 on windows, I do always get a failure on that test. The error happens when I guess there is a workaround: make that line be sockobj = _stdlib_socket.socket(family, type, proto, fileno=fileno)(I.e., passing in the "initial guesses" that I think there's a Python bug here that we really ought to isolate and report though... |
|
@tiran I know you made some changes to the behavior of |
|
I remember an issue with |
|
@njsmith A couple of notes: |
|
Aaah, there is another issue. On most platforms it is not possible to retrieve socket information from a new, unconfigured socket fd. The socket most either be connected to a peer or in listening mode. Linux is one of the few platforms that supports this for the majority of sockets. |
|
@tiran So, by extension, one might say this feature is only applicable to linux? |
|
The behavior depends on the platform and platform version.
One more thing, AFAIK Windows doesn't have the concept of protocols. In my experiments, the protocol value was always 0. |
|
@tiran Sorry, my question is much more specific :-). As far as I can tell, calling socket.socket(fileno=fileno) # where 'fileno' is a valid socket descriptorBut starting in 3.7, on Windows only, it now sometimes raises this exception instead: We don't yet understand what the condition is to trigger this; I can't reproduce it at the REPL. But our test suite triggers it 100% of the time, so there is something consistent about it :-). The question was whether you have any thoughts on:
|
|
The relevant log section from appveyor: ================================== FAILURES ===================================
_________________________ test_custom_socket_factory __________________________
async def test_custom_socket_factory():
class CustomSocketFactory:
def socket(self, family, type, proto):
return ("hi", family, type, proto)
csf = CustomSocketFactory()
assert tsocket.set_custom_socket_factory(csf) is None
assert tsocket.socket() == ("hi", tsocket.AF_INET, tsocket.SOCK_STREAM, 0)
assert tsocket.socket(1, 2, 3) == ("hi", 1, 2, 3)
# socket with fileno= doesn't call our custom method
fd = stdlib_socket.socket().detach()
> wrapped = tsocket.socket(fileno=fd)
..\trio\tests\test_socket.py:840:
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
..\trio\_socket.py:294: in socket
family, type, proto, fileno
..\trio\_socket.py:307: in _sniff_sockopts_for_fileno
sockobj = _stdlib_socket.socket(fileno=fileno)
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
self = <socket.socket fd=-1, family=AddressFamily.AF_UNSPEC, type=0, proto=0>
family = -1, type = -1, proto = -1, fileno = 1324
def __init__(self, family=-1, type=-1, proto=-1, fileno=None):
# For user code address family and type values are IntEnum members, but
# for the underlying _socket.socket they're just integers. The
# constructor of _socket.socket converts the given argument to an
# integer automatically.
if fileno is None:
if family == -1:
family = AF_INET
if type == -1:
type = SOCK_STREAM
if proto == -1:
proto = 0
> _socket.socket.__init__(self, family, type, proto, fileno)
E OSError: [WinError 10022] An invalid argument was supplied: 'family'
C:\Python37\lib\socket.py:151: OSErrorIt seems like it has something to do with the cpython's
Hopefully will work, but somewhat ugly in my opinion... |
|
@njsmith Sorry to pester you, as I'm sure there's more important stuff than this, but is there anything new about this perchance? |
|
Thanks for pestering :-). The explicit check for linux is a bit ugly yeah :-/. It's possible there's no alternative. I still have no idea about this failure... I tried a bit more to track it down, and it seems to be coming directly from some Windows API, not anything inside Python. What do you think about the idea upthread, of changing it so sockobj = _stdlib_socket.socket(family, type, proto, fileno=fileno)i.e., passes in whatever family/type/proto the user gave us? Rationale:
|
|
Looks like this is now passing everywhere except Linux? Frustrating :-( |
simplified test now check against real stdlib_socket
|
Oof, I'm sorry this was so painful! I guess there must be some nicer way to write it, but we've run into so many weird issues here that I'm not even sure how to find it... so maybe we should stick with this version that seems to work :-). I did edit your newsfragment; you can take a look and see if you like the new version. And thanks for your patience with this so-called "easy first issue"... |
|
No need to feel sorry about anything, it's just what it is (bloody annoying at times). |
|
Okay, merged! 🎉 🎂 And, no pressure, but if you'd like to keep contributing then we'd love to have you, so I'm sending you a github invite now. You can read more about this in our contributing documentation. |
WIP as
_socket.fromfddoesn't work properly.Also, shouldn't work on mac...
Fixes: #251