Hi, I have a situation where a thread is started to perform a long running task, but when a certain event occurs I want to interrupt and stop the thread. I use the standard Java technique to use Thread#interrupt on the running thread, and in the thread be prepared to catch InterruptedException and/or check the interrupt status on the thread.
This works fine in some cases, and not in others. The difference has to do with when the interrupt is raised and what code is executing at the time that receives the interrupt. One situation it does not work is if the interrupt is handled when the thread is making a call to BundleContextImpl#getService, as InterruptedException is not thrown nor is the interrupt flag set. Instead, a RuntimeException is thrown that wraps the InterruptedException. While one can catch the RuntimeException and unwrap it looking to see if an InterruptedException is present, that feels awkward and doesn't seem to follow what appears to be the standard approach of throwing the exception and/or setting the interrupt flag. I'm using Karaf 4.2.9 and here is the stack trace: java.lang.RuntimeException: java.lang.InterruptedException at org.apache.felix.framework.ServiceRegistry.getService(ServiceRegistry.java:369) at org.apache.felix.framework.Felix.getService(Felix.java:3737) at org.apache.felix.framework.BundleContextImpl.getService(BundleContextImpl.java:470) ... Caused by: java.lang.InterruptedException at java.util.concurrent.locks.AbstractQueuedSynchronizer.acquireSharedInterruptibly(AbstractQueuedSynchronizer.java:1302) at java.util.concurrent.CountDownLatch.await(CountDownLatch.java:231) at org.apache.felix.framework.ServiceRegistry.getService(ServiceRegistry.java:365) Looking at the code, it does definitely just catch and wrap InterruptedException, and does not set the interrupt flag again. This feels like a bug to me, that InterruptedException should be thrown or the interrupt flag should be set, so callers can be alerted to the interrupt in a standard way and not have to know to catch and unwrap RuntimeException. I have not tried the latest Karaf, but reviewing the code in the latest Felix it has changed and I'm actually not entirely sure what the behavior will be in this scenario, I will try to test it out. If this is a question for the Felix list since this is in that framework code, please let me know and I take the discussion there. Thanks, Kevin