Skip to content

Commit 588ca40

Browse files
committed
fix race condition when creating media connection and setting up playback
1 parent 7a1e71f commit 588ca40

File tree

1 file changed

+18
-16
lines changed

1 file changed

+18
-16
lines changed

LavalinkServer/src/main/java/lavalink/server/player/PlayerRestHandler.kt

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -111,22 +111,24 @@ class PlayerRestHandler(
111111
val player = context.getPlayer(guildId)
112112

113113
playerUpdate.voice.ifPresent {
114-
val oldConn = context.koe.getConnection(guildId)
115-
if (oldConn == null ||
116-
oldConn.gatewayConnection?.isOpen == false ||
117-
oldConn.voiceServerInfo == null ||
118-
oldConn.voiceServerInfo?.endpoint != it.endpoint ||
119-
oldConn.voiceServerInfo?.token != it.token ||
120-
oldConn.voiceServerInfo?.sessionId != it.sessionId
121-
) {
122-
//clear old connection
123-
context.koe.destroyConnection(guildId)
124-
125-
val conn = context.getMediaConnection(player)
126-
conn.connect(VoiceServerInfo(it.sessionId, it.endpoint, it.token)).exceptionally {
127-
throw ResponseStatusException(HttpStatus.INTERNAL_SERVER_ERROR, "Failed to connect to voice server")
128-
}.toCompletableFuture().join()
129-
player.provideTo(conn)
114+
synchronized(player) {
115+
val oldConn = context.koe.getConnection(guildId)
116+
if (oldConn == null ||
117+
oldConn.gatewayConnection?.isOpen == false ||
118+
oldConn.voiceServerInfo == null ||
119+
oldConn.voiceServerInfo?.endpoint != it.endpoint ||
120+
oldConn.voiceServerInfo?.token != it.token ||
121+
oldConn.voiceServerInfo?.sessionId != it.sessionId
122+
) {
123+
//clear old connection
124+
context.koe.destroyConnection(guildId)
125+
126+
val conn = context.getMediaConnection(player)
127+
conn.connect(VoiceServerInfo(it.sessionId, it.endpoint, it.token)).exceptionally {
128+
throw ResponseStatusException(HttpStatus.INTERNAL_SERVER_ERROR, "Failed to connect to voice server")
129+
}.toCompletableFuture().join()
130+
player.provideTo(conn)
131+
}
130132
}
131133
}
132134

0 commit comments

Comments
 (0)