Stop async tasks when server stops?

Hello, I am having some troubles getting an async task to stop when the server does.

The thread the task is running on appears to never stop, even when the server does. I know this is how Java threads work, but is it how Sponge tasks work? If so, how can I prevent this from happening?

The thread is daemon which means it will die when the main thread dies
https://github.com/SpongePowered/SpongeCommon/blob/master/src/main/java/org/spongepowered/common/service/scheduler/AsyncScheduler.java#L54

Not sure why it’s not stopping for you. You could try listening to GameStoppingServerEvent and cancelling the task
https://github.com/SpongePowered/SpongeAPI/blob/master/src/main/java/org/spongepowered/api/event/game/state/GameStoppingServerEvent.java

1 Like

I see, thanks for the help!

Why didn’t you use an interruption to stop the threads to allow the tasks to exit and perform cleanup logic instead of setting to daemon? I guess it’s more of a question directed at @sibomots, perhaps he would be better suited to answer this.