SumZeroTrading icon indicating copy to clipboard operation
SumZeroTrading copied to clipboard

IB Disconnection

Open vinceyap88 opened this issue 8 years ago • 5 comments

I have noticed that there are some threads running after calling the InteractiveBrokersClientInterface.unsubscribeLevel1() method as well as InteractiveBrokersClientInterface.disconnect(). Are there any methods which can be used to close the running threads after the connect() method and subscribeLevel1() method?

vinceyap88 avatar Jul 25 '16 06:07 vinceyap88

There is a thread running to process the internal queue of IB data from the level 1&2 subscribers, and order events. Is there an issue with these threads continuing to run even if there are no current level 1 subscribers?

rterp avatar Jul 28 '16 15:07 rterp

The program need to have proper exit. In order to exit properly, we need to stop the running threads. Is there anyway to stop it?

vinceyap88 avatar Jul 28 '16 16:07 vinceyap88

I see. Probably the easiest thing to do will be to convert the threads to daemon threads, so if they are still running when the main thread stops, the application will shut down without needing to stop the daemon threads.

rterp avatar Jul 28 '16 16:07 rterp

Hi Rob,

I think what Vince mentioned was partly caused by blocked threads in IBQuoteProcessor.

    public void run() {
        while( shouldRun ) {
            try {
                processData( quoteBlockingQueue.take() );
            } catch( Exception ex ) {
                //logger.error(ex, ex);
                ex.printStackTrace();
            }
        }
    }

When stopProcessor() method was called, shouldRun is changed to false with intention to exit the while loop, but the thread could be permablocked if no new data is put into the queue. I suggest to also interrupt this thread in stopProcessor()

charlye avatar Oct 12 '16 14:10 charlye

Ok, I see what you are saying now. You're right, if you aren't subscribed to any quotes, or if its after market hours and no quotes are coming in. The other option would be to use the poll(long timeout, TimeUnit unit) on the blockingQueue rather than take(), and give it a 500ms or so timeout value, so if there was nothing in the queue, the operation would timeout, it would see shouldRun is false, and exit the loop.

rterp avatar Oct 23 '16 21:10 rterp