diff --git a/maestro-cli/src/main/java/maestro/cli/session/MaestroSessionManager.kt b/maestro-cli/src/main/java/maestro/cli/session/MaestroSessionManager.kt index 1177f5f390..3afa962bd9 100644 --- a/maestro-cli/src/main/java/maestro/cli/session/MaestroSessionManager.kt +++ b/maestro-cli/src/main/java/maestro/cli/session/MaestroSessionManager.kt @@ -71,7 +71,6 @@ object MaestroSessionManager { val heartbeatFuture = executor.scheduleAtFixedRate( { try { - Thread.sleep(1000) // Add a 1-second delay here for fixing race condition SessionStore.heartbeat(sessionId, selectedDevice.platform) } catch (e: Exception) { logger.error("Failed to record heartbeat", e) diff --git a/maestro-client/src/main/java/maestro/drivers/AndroidDriver.kt b/maestro-client/src/main/java/maestro/drivers/AndroidDriver.kt index d8f0c73239..0486713adf 100644 --- a/maestro-client/src/main/java/maestro/drivers/AndroidDriver.kt +++ b/maestro-client/src/main/java/maestro/drivers/AndroidDriver.kt @@ -50,6 +50,7 @@ import java.net.URI import java.util.UUID import java.util.concurrent.CompletableFuture import java.util.concurrent.Executors +import java.util.concurrent.Semaphore import java.util.concurrent.TimeUnit import java.util.concurrent.TimeoutException import javax.xml.parsers.DocumentBuilderFactory @@ -72,6 +73,7 @@ class AndroidDriver( private val blockingStubWithTimeout get() = blockingStub.withDeadlineAfter(40, TimeUnit.SECONDS) private val asyncStub = MaestroDriverGrpc.newStub(channel) private val documentBuilderFactory = DocumentBuilderFactory.newInstance() + private val deviceCallSemaphore = Semaphore(1) private var instrumentationSession: AdbShellStream? = null private var proxySet = false @@ -1093,6 +1095,7 @@ class AndroidDriver( } private fun runDeviceCall(call: () -> T): T { + deviceCallSemaphore.acquire() return try { call() } catch (throwable: StatusRuntimeException) { @@ -1102,6 +1105,8 @@ class AndroidDriver( throw MaestroException.DriverTimeout("Android driver unreachable") } throw throwable + } finally { + deviceCallSemaphore.release() } }