Skip to content

Commit

Permalink
Fix deadlock when running shards on Android (#1853)
Browse files Browse the repository at this point in the history
  • Loading branch information
tokou committed Sep 4, 2024
1 parent 8cd7387 commit 52126d0
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
5 changes: 5 additions & 0 deletions maestro-client/src/main/java/maestro/drivers/AndroidDriver.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -1093,6 +1095,7 @@ class AndroidDriver(
}

private fun <T> runDeviceCall(call: () -> T): T {
deviceCallSemaphore.acquire()
return try {
call()
} catch (throwable: StatusRuntimeException) {
Expand All @@ -1102,6 +1105,8 @@ class AndroidDriver(
throw MaestroException.DriverTimeout("Android driver unreachable")
}
throw throwable
} finally {
deviceCallSemaphore.release()
}
}

Expand Down

0 comments on commit 52126d0

Please sign in to comment.