Skip to content

fromfd() and dup() for _socket on WIndows #45719

Closed

Description

@roudkerk
mannequin
BPO 1378
Nosy @gvanrossum, @theller, @orsenthil, @tiran, @Preston-Landers
Files
  • socket_fromfd.patch
  • trunk_socket_fromfd.patch
  • py3k_socket_fromfd.patch
  • socket.diff
  • py3k_another_socket.patch
  • socket2.diff
  • socket3.diff
  • set_error.patch
  • 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 2007-11-16.01:25:50.005>
    created_at = <Date 2007-11-03.15:24:15.366>
    labels = ['type-bug']
    title = 'fromfd() and dup() for _socket on WIndows'
    updated_at = <Date 2012-05-09.08:34:16.531>
    user = 'https://bugs.python.org/roudkerk'

    bugs.python.org fields:

    activity = <Date 2012-05-09.08:34:16.531>
    actor = 'Arthur.Kantor'
    assignee = 'christian.heimes'
    closed = True
    closed_date = <Date 2007-11-16.01:25:50.005>
    closer = 'gvanrossum'
    components = []
    creation = <Date 2007-11-03.15:24:15.366>
    creator = 'roudkerk'
    dependencies = []
    files = ['8682', '8723', '8746', '8748', '8757', '8758', '8759', '8761']
    hgrepos = []
    issue_num = 1378
    keywords = ['patch']
    message_count = 32.0
    messages = ['57084', '57336', '57492', '57494', '57498', '57501', '57504', '57505', '57506', '57508', '57511', '57513', '57514', '57515', '57516', '57541', '57548', '57551', '57552', '57554', '57555', '57556', '57557', '57566', '57567', '57576', '57577', '57580', '93551', '94160', '94257', '160260']
    nosy_count = 8.0
    nosy_names = ['gvanrossum', 'nnorwitz', 'theller', 'orsenthil', 'christian.heimes', 'roudkerk', 'planders', 'Arthur.Kantor']
    pr_nums = []
    priority = 'normal'
    resolution = 'accepted'
    stage = None
    status = 'closed'
    superseder = None
    type = 'behavior'
    url = 'https://bugs.python.org/issue1378'
    versions = ['Python 2.7']

    Activity

    1. roudkerk commented on Nov 3, 2007

      roudkerkmannequin
      MannequinAuthor

      The patch adds support for _socket.fromfd() and _socket.socket.dup() on
      Windows. It uses the Win32 DuplicateHandle() function.

      The patch is to socketmodule.c an test_socket.py.

    2. added
      type-bug
      on Nov 3, 2007
    3. tiran commented on Nov 9, 2007

      @tiran
      Member

      I've created a clean patch and tested it. It works as promised, great
      work! Somebody with more Windows Fu than me should verify it before it's
      applied.

    4. gvanrossum commented on Nov 14, 2007

      @gvanrossum
      Member

      I'd like to see this applied, HOWEVER I'm confused. I don't see any
      mention of fromfd in the patch except in a comment. Now, in 3.0,
      fromfd() is implemented in socket.py using os.dup(); but in 2.6 i don't
      see it there either. What's going in?

      I am hoping that this will help bpo-1439 along as well.

    5. gvanrossum commented on Nov 14, 2007

      @gvanrossum
      Member

      Never mind. I must've searched for fromdf. Crys, I think this is good
      to go into 2.6 and be merged into 3.0. Then in 3.0 we can simplify Bill
      Janssen's patch further.

    6. tiran commented on Nov 14, 2007

      @tiran
      Member

      Neal, Thomas, what do you think about the patch? Your knowledge of the
      Windows API is greater than mine. Is the duplicate_socket() function ok?
      I don't want to apply a patch I don't fully understand.

      +#ifdef MS_WINDOWS
      +/* On Windows a socket is really a handle not an fd */
      +static SOCKET
      +duplicate_socket(SOCKET handle)
      +{
      + HANDLE newhandle;
      +
      + if (!DuplicateHandle(GetCurrentProcess(), (HANDLE)handle,
      + GetCurrentProcess(), &newhandle,
      + 0, FALSE, DUPLICATE_SAME_ACCESS))
      + {
      + WSASetLastError(WSAEBADF);
      + return INVALID_SOCKET;
      + }
      + return (SOCKET)newhandle;
      +}
      +#define dup(fd) duplicate_socket(fd)
      +#define SOCKETCLOSE closesocket
      +#define NO_MAKEFILE /* socket handles can't be treated like file handles */
      +#endif

    7. tiran commented on Nov 14, 2007

      @tiran
      Member

      Port of the socket_fromfd.patch to py3k

    8. theller commented on Nov 14, 2007

      @theller

      Christian Heimes schrieb:

      Neal, Thomas, what do you think about the patch? Your knowledge of the
      Windows API is greater than mine. Is the duplicate_socket() function ok?
      I don't want to apply a patch I don't fully understand.

      +#ifdef MS_WINDOWS
      +/* On Windows a socket is really a handle not an fd */
      +static SOCKET
      +duplicate_socket(SOCKET handle)
      +{

      •   HANDLE newhandle;
        
      •   if (!DuplicateHandle(GetCurrentProcess(), (HANDLE)handle,
        
      •                        GetCurrentProcess(), &newhandle,
        
      •                        0, FALSE, DUPLICATE_SAME_ACCESS))
        
      •   {
        
      •           WSASetLastError(WSAEBADF);
        
      •           return INVALID_SOCKET;
        
      •   }
        
      •   return (SOCKET)newhandle;
        

      +}
      +#define dup(fd) duplicate_socket(fd)
      +#define SOCKETCLOSE closesocket
      +#define NO_MAKEFILE /* socket handles can't be treated like file handles */
      +#endif

      Not much time, so only one comment:
      Is it wise to call WSASetLastError(WSAEBADF) instead of calling GetLastError()?

      Thomas

    9. gvanrossum commented on Nov 14, 2007

      @gvanrossum
      Member

      I'm disappointed that this doesn't implement socket.fromfd() properly.
      Perhaps fromfd() needs to be implemented in socketmodule.c?

      Also, the whole mess with the _base slot can be gotten rid of;
      accept() can use the dup() method instead.

    10. tiran commented on Nov 14, 2007

      @tiran
      Member

      I'm disappointed that this doesn't implement socket.fromfd() properly.
      Perhaps fromfd() needs to be implemented in socketmodule.c?

      On Windows socket.fromfd() is not possible. Socket handlers and file
      descriptors are different types using different API methods. Other
      examples for the discrepancy are select.select() and select.poll(). On
      Windows they don't work on file.

    11. gvanrossum commented on Nov 14, 2007

      @gvanrossum
      Member

      On Windows socket.fromfd() is not possible. Socket handlers and file
      descriptors are different types using different API methods. Other
      examples for the discrepancy are select.select() and select.poll(). On
      Windows they don't work on file.

      I know, but, I was thinking of it taking the fileno() of some other
      socket object as argument.

      In any case I think the test should not be based on whether os.name ==
      'nt' but on the presence of something in the _socket module.

    12. gvanrossum commented on Nov 14, 2007

      @gvanrossum
      Member

      Crys, did you actually run the tests for the 3.0 port of the patch? I
      think accept() is broken, as it checks for _can_dup_socket and will
      attempt to call os.dup().

      I'm thinking that we should change the C class to not create new objects
      on accept() and dup() -- instead, there should be methods _accept() and
      _dup() that return new file descriptors. Then the subclass can
      implement accept() and dup() properly without needing to call dup()
      again, and the _base slot will no longer be needed. Tell you what, I'll
      try this and submit a patch for testing on Windows later.

    13. gvanrossum commented on Nov 15, 2007

      @gvanrossum
      Member

      Here's a new version along the lines I described. Please test on
      Windows. It works on Linux and OSX. What this includes:

      • the dup-on-windows implementation by R Oudkerk, with the
        GetLastError() suggestion from Thomas Heller.

      • Replace the C implementations of dup() and accept() with _dup() and
        _accept() that return file descriptors instead of socket objects.

      • Implement accept() and dup() in Python using _accept() and _dup().

      • Get rid of socket.fromfd(). You can use socket.socket(..., fileno=...)
        instead (it doesn't dup though).

    14. gvanrossum commented on Nov 15, 2007

      @gvanrossum
      Member

      New version of socket.diff. The only change is better docstrings for
      accept() and dup().

    15. 6 remaining items

    16. gvanrossum commented on Nov 15, 2007

      @gvanrossum
      Member
      • I'm hoping that Bill can submit his SSL changes first.

      • If we make _dup() a module-level function, we can implement fromfd()
        in Python. I'll do this now.

    17. gvanrossum commented on Nov 15, 2007

      @gvanrossum
      Member

      socket2.diff makes dup() a module-level function in _socket (and hence
      in socket.py) and uses that to implement fromfd() in socket.py.

      No changes otherwise compared to py3k_another_socket.patch.

    18. gvanrossum commented on Nov 15, 2007

      @gvanrossum
      Member

      socket3.diff builds on socket2.diff:

      • Rename socket class to Socket; add socket() as a factory function.

      • Implement roudkerk's suggestion of using a duplicate socket in
        makefile() to get rid of the manual reference counting. Yay!

      Note: this dinterferes with Bill Janssen's bpo-1451. Whichever is
      checked in last must fix issues with the other.

    19. gvanrossum commented on Nov 15, 2007

      @gvanrossum
      Member

      Forgot to upload socket3.diff.

    20. tiran commented on Nov 15, 2007

      @tiran
      Member

      I've yet another idea for a tiny improvement. Instead of doing

         newfd = _socket.dup(socket_instance.fileno())

      I prefer

         newfd = _socket.dup(socket_instance)

      It removes some confusing magic and makes error checking on Windows
      slightly easier.

    21. gvanrossum commented on Nov 15, 2007

      @gvanrossum
      Member

      Hold on, socket3.diff breaks four unit tests:

      test_ftplib test_poplib test_smtplib test_urllib2net

      newfd = _socket.dup(socket_instance)

      But that doesn't allow fromfd to work. I found some real use cases for
      fromfd where the fd is passed in through some other means:

      http://www.google.com/codesearch?as_q=socket%5C.fromfd&btnG=Search+Code&as_lang=python

    22. roudkerk commented on Nov 16, 2007

      roudkerkmannequin
      MannequinAuthor

      Currently on Windows set_error() make use of a large array which maps
      socket error numbers to error messages.

      This patch (against socketmodule.c from Python 2.6) removes that
      array and just lets PyErr_SetExcFromWindowsErr()
      generate the message by using the Win32 function FormatMessage().

      It is orthogonal from the other patches discussed here.

    23. gvanrossum commented on Nov 16, 2007

      @gvanrossum
      Member

      Thanks! roudkerk's patch is submitted as revision 59004.

      BTW I need to go back to the drawing board for the rest of the socket
      patch here. Using dup() in makefile() doesn't work for the
      ssl.SSLSocket class. Maybe the explicit reference counting is the best
      we can get.

    24. gvanrossum commented on Nov 16, 2007

      @gvanrossum
      Member

      I've submitted socket2.diff plus small changes to ssl.py.
      This seems the best I can do given that I don't think I can make dup()
      work on ssl sockets.

    25. Preston-Landers commented on Oct 4, 2009

      Preston-Landersmannequin
      Mannequin

      I'm curious what happened with this issue. It says closed+accepted but
      it doesn't appear to be checked in. Was there a fatal problem
      implementing this feature on Windows? Is it hung up on the inability
      to dup SSL sockets?

      I'm highly interested in deploying Python web servers using FastCGI,
      SCGI, WSGI, etc on the Windows server platform. I wrote a simple
      Windows fromfd() patch for Python 2.1 which was successfully used by my
      organization for many years. Now we are trying to move to a newer
      version of Python and still facing the need to patch this in. Just
      wondering what happened with this feature and perhaps there is something
      I can do to help move it along again. Many Python projects like flup,
      python-scgi, etc would be able to support Windows via this feature.

      (PS if this question is more appropriately raised on a mailing list
      rather than the issue tracker, I apologize in advance.)

    26. orsenthil commented on Oct 17, 2009

      @orsenthil
      Member

      On Sun, Oct 04, 2009 at 05:39:26PM +0000, Preston Landers wrote:

      I'm curious what happened with this issue. It says closed+accepted but
      it doesn't appear to be checked in.

      If you see the report, the last message indicates that it is checked in revision 59004.

    27. Preston-Landers commented on Oct 19, 2009

      Preston-Landersmannequin
      Mannequin

      Hmm... revision 59004 appears to be unrelated to the main issue at hand.
      As far as I can tell now the status is this: the dup() and fromfd()
      support appears to be present in Python 3000 for Windows - at least the
      py3k branch. I didn't check the releases. But it's not present in 2.6
      or trunk. I guess as far as I'm concerned that's fine. I'm going to use
      a patch to 2.6.3 to implement it for my purposes anyway. Thanks.

    28. ArthurKantor commented on May 9, 2012

      ArthurKantormannequin
      Mannequin

      Hi guys

      It appears, that this patch was meant to go into both the 2.x and 3.x series, but it never made into 2.x

      Are there still plans to apply it to 2.7?

      (apologies if I'm asking this in the wrong forum)

    29. transferred this issue fromon Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

    Metadata

    Metadata

    Assignees

    Labels

    type-bug

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions