Remove potential nio selector leak (#27825)

When an ESSelector is created an underlying nio selector is opened. This
selector is closed by the event loop after close has been signalled by
another thread.

However, there is a possibility that an ESSelector is created and some
exception in the startup process prevents it from ever being started
(however, close will still be called). The allows the selector to leak.

This commit addresses this issue by having the signalling thread close
the selector if the event loop is not running when close is signalled.
This commit is contained in:
Tim Brooks 2017-12-14 14:37:41 -07:00 committed by GitHub
parent c541a0c60e
commit f33f9612a7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 10 additions and 1 deletions

View File

@ -177,8 +177,11 @@ public abstract class ESSelector implements Closeable {
try {
exitedLoop.await();
} catch (InterruptedException e) {
eventHandler.uncaughtException(e);
Thread.currentThread().interrupt();
throw new IllegalStateException("Thread was interrupted while waiting for selector to close", e);
}
} else if (selector.isOpen()) {
selector.close();
}
}
}

View File

@ -81,6 +81,12 @@ public class ESSelectorTests extends ESTestCase {
verify(handler).selectException(ioException);
}
public void testSelectorClosedIfOpenAndEventLoopNotRunning() throws IOException {
when(rawSelector.isOpen()).thenReturn(true);
selector.close();
verify(rawSelector).close();
}
private static class TestSelector extends ESSelector {
TestSelector(EventHandler eventHandler, Selector selector) throws IOException {