[docs] [issue26703] Socket state corrupts when original socket object goes out of scope in a different thread

Martin Panter report at bugs.python.org
Wed Apr 6 22:24:45 EDT 2016


Martin Panter added the comment:

Yes, I think this is the expected behaviour, and I can’t think of any improvements that could be made. If you call fileno(), you have to ensure that you don’t close the file descriptor until you have finished using it. It is a bit like accessing memory after it has been freed. Python doesn’t make raw memory addresses easily accessible, but it does make fileno() accessible without much protection.

Perhaps there is some confusion about the term socket. Normally (without using the fileno=... parameter), Python’s socket() constructor does two things. First, it creates a new OS socket using the socket() system call (or Winsock equivalent), which returns a file descriptor or handle (an integer). Then, it creates a Python socket object, which wraps the file descriptor.

When you use socket(fileno=...), only the second step is taken. You get a _new_ socket object, which wraps the given existing OS socket file descriptor. So when it says “the same socket”, I think it means the same OS-level socket. It still creates a new Python object.

----------

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue26703>
_______________________________________


More information about the docs mailing list