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

2016-04-08 Thread Antoine Pitrou
Antoine Pitrou added the comment: No need to apologize :) Perhaps we need to make the docs a bit clearer. -- ___ Python tracker ___

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

2016-04-08 Thread JoshN
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. --

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

2016-04-07 Thread Antoine Pitrou
Antoine Pitrou added the comment: The general answer here is you should avoid mixing calls to different abstraction layers. Either use only the file descriptor or only the socket object. This is not limited to lifetime issues, other issues can occur. For example, setting a timeout on a

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

2016-04-06 Thread Martin Panter
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

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

2016-04-06 Thread JoshN
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

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

2016-04-06 Thread Martin Panter
Martin Panter added the comment: Also, if you enable warnings (e.g. python -Wall), you should see that the socket is being closed: -c:22: ResourceWarning: unclosed -- ___ Python tracker

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

2016-04-06 Thread Martin Panter
Martin Panter added the comment: The documentation already says “Sockets are automatically closed when they are garbage-collected”. If for some reason you want to release a socket object but keep the file descriptor open, I suggest socket.detach(). Otherwise, pass the original socket, not the

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

2016-04-06 Thread Josh Rosenberg
Josh Rosenberg added the comment: For source reference, the behavior for this case is to just copy out the file descriptor and stick it in a new socket object ( https://hg.python.org/cpython/file/3.5/Modules/socketmodule.c#l4289 ); no work is being done to somehow collaboratively manage the

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

2016-04-06 Thread Josh Rosenberg
Josh Rosenberg added the comment: You used the `fileno` based initialization in the child, which creates a wrapper around the same file descriptor without duplicating it, so when the first socket disappears, that file descriptor becomes invalid. I think this is a doc bug more than a behavior

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

2016-04-06 Thread SilentGhost
Changes by SilentGhost : -- nosy: +pitrou ___ Python tracker ___ ___ Python-bugs-list

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

2016-04-06 Thread JoshN
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