Make socket recover if slimevr crashed (#1604)

This commit is contained in:
lucas lelievre
2025-11-03 18:01:25 +01:00
committed by GitHub
parent 2a55617e3a
commit 7180f4eaa7
3 changed files with 44 additions and 3 deletions

View File

@@ -0,0 +1,20 @@
package dev.slimevr.desktop.platform.linux;
import java.io.IOException;
import java.net.StandardProtocolFamily;
import java.net.UnixDomainSocketAddress;
import java.nio.channels.SocketChannel;
public class SocketUtils {
static boolean isSocketInUse(String socketPath) {
try (SocketChannel testChannel = SocketChannel.open(StandardProtocolFamily.UNIX)) {
testChannel.connect(UnixDomainSocketAddress.of(socketPath));
return true;
} catch (IOException e) {
return false;
}
}
}

View File

@@ -46,7 +46,16 @@ public class UnixSocketBridge extends SteamVRBridge implements AutoCloseable {
File socketFile = new File(socketPath);
if (socketFile.exists()) {
throw new RuntimeException(socketPath + " socket already exists.");
if (SocketUtils.isSocketInUse(socketPath)) {
throw new RuntimeException(
socketPath + " socket is already in use by another process."
);
} else {
LogManager.warning("[" + bridgeName + "] Cleaning up stale socket: " + socketPath);
if (!socketFile.delete()) {
throw new RuntimeException("Failed to delete stale socket: " + socketPath);
}
}
}
socketFile.deleteOnExit();
}

View File

@@ -35,10 +35,22 @@ public class UnixSocketRpcBridge implements dev.slimevr.bridge.Bridge,
) {
this.socketPath = socketPath;
this.protocolAPI = server.protocolAPI;
File socketFile = new File(socketPath);
if (socketFile.exists())
throw new RuntimeException(socketPath + " socket already exists.");
if (socketFile.exists()) {
if (SocketUtils.isSocketInUse(socketPath)) {
throw new RuntimeException(
socketPath + " socket is already in use by another process."
);
} else {
LogManager.warning("[SolarXR Bridge] Cleaning up stale socket: " + socketPath);
if (!socketFile.delete()) {
throw new RuntimeException("Failed to delete stale socket: " + socketPath);
}
}
}
socketFile.deleteOnExit();
try {
socket = ServerSocketChannel.open(StandardProtocolFamily.UNIX);
selector = Selector.open();