From fe7ca9fc80850d3ae5221e67e809bbf8a0744c65 Mon Sep 17 00:00:00 2001 From: loucass003 Date: Tue, 27 Oct 2026 21:07:36 +0100 Subject: [PATCH] Properly handle HID errors --- .../main/java/dev/slimevr/desktop/hid/hid.kt | 24 +++++++++++++++---- 1 file changed, 19 insertions(+), 5 deletions(-) diff --git a/server/desktop/src/main/java/dev/slimevr/desktop/hid/hid.kt b/server/desktop/src/main/java/dev/slimevr/desktop/hid/hid.kt index bf7452b1b..366bf26fa 100644 --- a/server/desktop/src/main/java/dev/slimevr/desktop/hid/hid.kt +++ b/server/desktop/src/main/java/dev/slimevr/desktop/hid/hid.kt @@ -43,6 +43,18 @@ private fun shutdownHidApi() { } } +/** + * JNA reports a native access violation as a bare java.lang.Error. Left as is it escapes the manager's + * exception handling and cancels the whole server scope, so it becomes a regular failure to retry. + * hidapi's Windows enumeration can fault this way while devices are in flux, like on resume from sleep + */ +private inline fun nativeHidCall(block: () -> T): T = try { + block() +} catch (e: Error) { + if (e::class != Error::class) throw e + throw IllegalStateException("Native HID call failed", e) +} + private fun enumerateHidDevices(directTrackersEnabled: Boolean): Map { ensureHidApi() @@ -94,7 +106,7 @@ private class DesktopHidConnection(private val struct: HidDeviceStructure) : Hid var result = -1 while (!closed) { - result = HidApi.read(struct, buffer, budgetMs) + result = nativeHidCall { HidApi.read(struct, buffer, budgetMs) } if (result >= 0 || !wasInterrupted(struct)) break // hidapi's poll() has no EINTR retry, so any signal delivered to this thread, like a @@ -114,7 +126,7 @@ private class DesktopHidConnection(private val struct: HidDeviceStructure) : Hid override suspend fun write(data: ByteArray): Int = writeLock.withLock { if (closed) return@withLock -1 withContext(Dispatchers.IO) { - val written = HidApi.write(struct, data, data.size, 0) + val written = nativeHidCall { HidApi.write(struct, data, data.size, 0) } if (written > 0) written - 1 else written } } @@ -132,12 +144,14 @@ private class DesktopHidTransport : HidTransport { override val wakeSignal: ReceiveChannel? = null override suspend fun enumerate(directTrackersEnabled: Boolean): Map = withContext(Dispatchers.IO) { - enumerateHidDevices(directTrackersEnabled) + nativeHidCall { enumerateHidDevices(directTrackersEnabled) } } override suspend fun open(descriptor: HidDeviceDescriptor): HidConnection? = withContext(Dispatchers.IO) { - ensureHidApi() - HidApi.open(descriptor.key)?.let { struct -> DesktopHidConnection(struct) } + nativeHidCall { + ensureHidApi() + HidApi.open(descriptor.key)?.let { struct -> DesktopHidConnection(struct) } + } } override suspend fun shutdown() = withContext(Dispatchers.IO) { shutdownHidApi() }