atomashpolskiy / bt

BitTorrent library and client with DHT, magnet links, encryption and more
https://atomashpolskiy.github.io/bt/
Apache License 2.0
2.41k stars 382 forks source link

[BUG] Exception upon an attempt to remove the runtime shutdown hook during JVM shutdown #169

Closed atomashpolskiy closed 3 years ago

atomashpolskiy commented 3 years ago

Describe the bug Seems to have been introduced in bf7f3b4e

Expected behaviour No exceptions, smooth shutdown.

To Reproduce Testing this via CliClient.

Additional context Error message and stacktrace:

Exception in thread "6891.bt.runtime.shutdown-manager" java.lang.IllegalStateException: Shutdown in progress
        at java.lang.ApplicationShutdownHooks.remove(ApplicationShutdownHooks.java:82)
        at java.lang.Runtime.removeShutdownHook(Runtime.java:239)
        at bt.runtime.BtRuntime.shutdown(BtRuntime.java:234)
        at bt.runtime.BtRuntime$1.run(BtRuntime.java:101)
atomashpolskiy commented 3 years ago

This one has been addressed by 5cfc4a38 and 61ccf582. But now there is a different (intermittent) error:

bt.BtException: Failed to execute shutdown hook: : Disables port mapping on application shutdown.
    at bt.runtime.BtRuntime.lambda$runHooks$4(BtRuntime.java:276)
    at java.util.HashMap.forEach(HashMap.java:1289)
    at bt.runtime.BtRuntime.runHooks(BtRuntime.java:271)
    at bt.runtime.BtRuntime.shutdown(BtRuntime.java:231)
    at bt.cli.CliClient.lambda$resume$1(CliClient.java:257)
    at java.util.Optional.ifPresent(Optional.java:159)
    at bt.DefaultClient.lambda$notifyListener$0(DefaultClient.java:83)
    at java.util.Optional.ifPresent(Optional.java:159)
    at bt.DefaultClient.notifyListener(DefaultClient.java:82)
    at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
    at java.util.concurrent.FutureTask.runAndReset(FutureTask.java:308)
    at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$301(ScheduledThreadPoolExecutor.java:180)
    at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:294)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
    at java.lang.Thread.run(Thread.java:748)
Caused by: java.lang.InterruptedException
    at java.util.concurrent.CompletableFuture.reportGet(CompletableFuture.java:347)
    at java.util.concurrent.CompletableFuture.get(CompletableFuture.java:1915)
    at bt.runtime.BtRuntime.lambda$runHooks$4(BtRuntime.java:274)
    ... 15 more

The actual shutdown hook may be different, and the error does not happen each time.

atomashpolskiy commented 3 years ago

Closing this one to keep things simple. Further investigation to be done in the scope of #167