I had a side discussion with Eitan but I think it is useful to explain the change made on the C++ API that fixes the bug reported in this thread: ?
/g/twsapi/message/51775
?
The error that Eitan talks about is caused by the client trying to read or write from/to a closed socket. The socket can be closed by a call to eDisconnect() or it could be by an unexpected TWS shutdown.
?
Without going into all the details explained in the linked thread, the basic problem in the old API versions is that the EReader thread never shut down due to bad thread management. The thread just hung in a never ending loop. The only way to shut it down was to delete the EReader. Also the consequence of this is that the EWrapper callback function "closedConnection()" was never called.
?
The thread management has been fixed a while back and his been in the "LATEST" releases for some time. This new revision will call "closedConnection()" when a socket is closed (either by user or due to TWS shutdown, etc.) so that the client can handle the shutdown gracefully.?
?
This means that the proposed solution by Vadim below is not the correct one as what you are basically doing is trying to write and read from a closed socket which results in the said error. The correct way is to handle the EWrapperImpl callback.
?
Note that this fix is only applicable to the C++ code. I cannot say if there is a similar bug in the Java, Python, etc. APIs. However, the description from IB at the bottom of this documentation page would indicate that this code may be broken in other languages too. ?
?
Note: I did ask for the documentation to be updated to reflect this fix but it has not been done.