Opened 4 years ago

Last modified 3 years ago

#5599 enhancement new

Add address family detection support to systemd endpoint / adopted socket endpoint / IReactorSocket.adoptStreamPort

Reported by: exarkun Owned by: rwall
Priority: normal Milestone:
Component: core Keywords:
Cc: Branch: branches/socket-family-from-fd-5599
(github, patch, buildbot, log)
Author: rwall Launchpad Bug:

Description

It is actually possible to detect the address family of an arbitrary socket, it's just hard in Python.

Rather than forcing applications to specify this all over the place, we should let it be optional and do the detection ourselves.

Attachments (1)

socket-family-from-fd-5599.patch (9.8 KB) - added by rwall 3 years ago.
Socket family and type detection functions plus tests

Download all attachments as: .zip

Change History (5)

Changed 3 years ago by rwall

Socket family and type detection functions plus tests

comment:2 Changed 3 years ago by rwall

  • Author set to rwall
  • Branch set to branches/socket-family-from-fd-5599

(In [39053]) Branching to 'socket-family-from-fd-5599'

comment:3 Changed 3 years ago by rwall

  • Keywords review added

Ready for review in log:branches/socket-family-from-fd-5599

  1. This link shows how to detect socket family and type can be detected in python: http://utcc.utoronto.ca/~cks/space/blog/python/SocketFromFdMistake
  2. I wrote some functions and they seem to work.
  3. I wrote test builders (copying the ReactorTestBuilder) to try all combinations of family and type.
  4. But in fact many of the combinations were invalid or couldn't be accurately detected. So I should probably limit the tests to INET INET6 UNIX DGRAM and STREAM.
  5. It will be interesting to see if the tests pass on non-Linux platforms
    1. http://buildbot.twistedmatrix.com/boxes-supported?branch=/branches/socket-family-from-fd-5599
  6. I'm not sure where these functions should be used in the existing APIs. eg
    1. IReactorSocket.adoptXXXEndpoint
    2. t.i.endpoints.AdoptedStreamServerEndpoint
    3. t.i.endpoints.serverFromString (for type detection)
    4. t.a.internet.strports.service (for type detection)

So I'd like to know whether this is a valid approach and where family (and especially type) autodetection will be used.

-RichardW

comment:4 Changed 3 years ago by therve

  • Keywords review removed
  • Owner set to rwall

A couple of comments:

  • This ticket only talks about family. It looks like you could split the type detection in another branch easily.
  • That said, there is some duplication between the 2 calls, namely the fromfd call. I don't know how expensive it is, but it might be interesting to think of an API where it could be shared.
  • You need to skip the tests on windows indeed, because socket.fromfd doesn't exist there.
  • Regarding where family detection should be used, I'm thinking:
    • Change IReactorSocket.adoptStreamPort and adoptStreamConnection so that None can be passed for addressFamily.
    • Change AdoptedStreamServerEndpoint so that addressFamily is optional and defaults to None.
    • Make it so that _SystemdParser._parseServer doesn't require a domain argument anymore.

This should be done in a different ticket, maybe even 3 different ones. For type detection, I don't think we already have an API that would benefit from it. We definitely should, though. Maybe it would be even interesting to have a generic adoptFD mechanism, if we find a nice way to detect if a socket is a server or not (getpeername maybe?)

Thanks!

Note: See TracTickets for help on using tickets.