private synchronized void registerCloseHook(Consumer<Closeable> addCloseHook, Consumer<Closeable> removeCloseHook) { if (closeable == null) { closeable = ar -> { synchronized (CloseHandler.this) { if (closeable == null) { ar.handle(Future.succeededFuture()); return; } closeable = null; } close.accept(0L, ar); }; closeableHookCleanup = () -> { synchronized (CloseHandler.this) { if (closeable != null) { removeCloseHook.accept(closeable); closeable = null; } } }; addCloseHook.accept(closeable); } }