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 1, 2024
1 parent 3ec7cac commit 53e31b4
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 @@ -70,7 +70,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 @@ -49,6 +49,7 @@ import java.io.IOException
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 @@ -71,6 +72,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 @@ -1063,6 +1065,7 @@ class AndroidDriver(
}

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

Expand Down

0 comments on commit 53e31b4

Please sign in to comment.