[issue26703] Socket state corrupts when original assignment goes out of scope
New submission from JoshN: Creating a socket in one thread and sharing it with another will cause the socket to corrupt as soon as the thread it was created in exits. Example code: import socket, threading, time, os def start(): a = socket.socket(socket.AF_INET, socket.SOCK_STREAM) a.bind(("", 8080)) a.set_inheritable(True) thread = threading.Thread(target=abc, args=(a.fileno(),)) thread.start() time.sleep(2) print("Main thread exiting, socket is still valid: " + str(a) + "\n") def abc(b): sock = socket.socket(fileno=b) for _ in range(3): print("Passed as an argument:" + str(sock) + "\n=") time.sleep(1.1) start() Note that, as soon as the main thread exits, the socket isn't closed, nor is the fd=-1, etc. Doing anything with this corrupted object throws WinError 10038 ('operation performed on something that is not a socket'). I should note that the main thread exiting doesn't seem to be the cause, it is the original object containing the socket going out of scope that causes the socket to become corrupted. -JoshN -- components: IO messages: 262951 nosy: JoshN priority: normal severity: normal status: open title: Socket state corrupts when original assignment goes out of scope type: crash versions: Python 3.5 ___ Python tracker <http://bugs.python.org/issue26703> ___ ___ Python-bugs-list mailing list Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue26703] Socket state corrupts when original socket object goes out of scope in a different thread
Changes by JoshN : -- title: Socket state corrupts when original assignment goes out of scope -> Socket state corrupts when original socket object goes out of scope in a different thread ___ Python tracker <http://bugs.python.org/issue26703> ___ ___ Python-bugs-list mailing list Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue26703] Socket state corrupts when original socket object goes out of scope in a different thread
JoshN added the comment: I do understand that the docs are a bit strange on the issue. For example, actually testing the line you referenced ("...fileno will return the same socket and not a duplicate.") by creating 2 sockets and testing sameness with the 'is' operator returns false. I tried to trim the example code as much as possible - I did test disabling the garbage collector, playing with inheritance, etc, but trimmed them out as they didn't have any effect on my system. I think my main issue was, when this occurs, the socket 'breaks' as you mentioned instead of closing. Was almost sure it was a bug. Using detach works for this UDP example, but I wasn't sure if detaching the socket actually closes it (e.g. in a stream oriented connection). So this is considered normal behavior then? -- ___ Python tracker <http://bugs.python.org/issue26703> ___ ___ Python-bugs-list mailing list Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue26703] Socket state corrupts when original socket object goes out of scope in a different thread
JoshN added the comment: Josh/Martin/Antoine: Thank you for the tips - I was not aware of the underlying mechanics, especially the separate abstraction layers. I did RTFM up and down before posting this, to be sure. My apologies for the inconvenience. -- ___ Python tracker <http://bugs.python.org/issue26703> ___ ___ Python-bugs-list mailing list Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com