Skip to content

Commit 634541e

Browse files
authored
Add shutdown hook to close clients properly (#1102)
1 parent 2af0559 commit 634541e

File tree

2 files changed

+20
-0
lines changed

2 files changed

+20
-0
lines changed
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package lavalink.server.io
2+
3+
import org.springframework.web.socket.CloseStatus
4+
5+
class ShutdownHandler(private val socketServer: SocketServer) : Thread("lavalink-shutdown-handler") {
6+
init {
7+
isDaemon = false // we want this thread to block shutdown until it has finished running
8+
}
9+
10+
override fun run() {
11+
socketServer.contexts.forEach {
12+
// don't care about exceptions here, the JVM's shutting down anyway.
13+
it.runCatching { closeWebSocket(CloseStatus.GOING_AWAY.code) }
14+
}
15+
}
16+
}

LavalinkServer/src/main/java/lavalink/server/io/SocketServer.kt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,10 @@ final class SocketServer(
5656
private val statsCollector = StatsCollector(this)
5757
private val charPool = ('a'..'z') + ('0'..'9')
5858

59+
init {
60+
Runtime.getRuntime().addShutdownHook(ShutdownHandler(this))
61+
}
62+
5963
companion object {
6064
private val log = LoggerFactory.getLogger(SocketServer::class.java)
6165

0 commit comments

Comments
 (0)