开云体育

ctrl + shift + ? for shortcuts
© 2025 开云体育

Re: C++ preventing EReader reading when socket is closed


 

After much deliberation, I think I figured out the problem.

In my opinion it is a bug in the TWS API for C++.

I would like someone's help to go thru' my logic and confirm it.

The EReader::~EReader() destructor closes the socket by calling eDisconnect() then it waits for the thread to complete by calling WaitForSingleObject(m_hReadThread, INFINITE);

In my opinion this is wrong. Why would you want to disconnect the socket when you have a thread running which is potentially calling recv() on the same socket? In my view, we have to wait for the thread to finish then disconnect the socket.

My fix to the problem is to swap the two steps, i.e.

??? if (m_hReadThread) {
??? ? ? ?? m_isAlive = false;
?? ??? ??? m_pClientSocket->eDisconnect();
? ?? ????? WaitForSingleObject(m_hReadThread, INFINITE);
?? ???? }
becomes

??? if (m_hReadThread) {
????????? m_isAlive = false;
? ? ????? WaitForSingleObject(m_hReadThread, INFINITE);
?? ?????? m_pClientSocket->eDisconnect();
?? ???? }

This has resolved the issue.

Could someone confirm my logic so I can issue a bug report to the IBKR guys?

Thanks

Join [email protected] to automatically receive all group messages.