fix: ensure closeAsync joins I/O thread to prevent libusb crash

This commit is contained in:
Davide Depau committed 2026-06-30 22:17:06 +02:00
1 parent e203b46563
commit 2c8326f016
3 files changed
+128 -2

No files matched your search

@@ -4,10 +4,12 @@ import android.util.Log
import eu.depau.etchdroid.utils.AsyncInputStream
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job
import kotlinx.coroutines.channels.Channel
import kotlinx.coroutines.channels.ClosedReceiveChannelException
import kotlinx.coroutines.channels.ClosedSendChannelException
import kotlinx.coroutines.launch
import kotlinx.coroutines.withTimeoutOrNull
import me.jahnen.libaums.core.driver.BlockDeviceDriver
import java.io.IOException
import java.nio.ByteBuffer
@@ -18,6 +20,14 @@ private const val TAG = "BlockDeviceInputStream"
private const val TRACE_IO = false
/**
* Upper bound on how long [BlockDeviceInputStream.closeAsync] waits for the I/O worker to
* terminate. On a healthy device the worker exits in milliseconds once the channel is closed; the
* timeout only trips if a native libusb transfer is genuinely stuck, in which case the job is
* failing anyway and we must not block close() forever.
*/
private const val IO_THREAD_JOIN_TIMEOUT_MS = 10_000L
/**
* Helper function to trace I/O operations.
*
@@ -110,6 +120,12 @@ class BlockDeviceInputStream(
*/
private val mIoThreadRunning: AtomicBoolean = AtomicBoolean(false)
/**
* The coroutine running the I/O thread, so [closeAsync] can wait for it to fully terminate
* (i.e. no native block-device transfer is still in flight) before returning.
*/
private var mIoThreadJob: Job? = null
/**
* Waits until the wanted block is available in the block channel and returns it. Note that the
* returned data may not necessarily *start* at the wanted block, but it will contain it.
@@ -165,7 +181,7 @@ class BlockDeviceInputStream(
val rendezvous = Channel<Unit>()
coroutineScope.launch(Dispatchers.IO) {
mIoThreadJob = coroutineScope.launch(Dispatchers.IO) {
Thread.currentThread().name = "BlockDeviceInputStream I/O thread"
try {
@@ -442,6 +458,12 @@ class BlockDeviceInputStream(
} catch (e: ClosedReceiveChannelException) {
// Ignore
}
// Wait for the I/O worker to fully terminate so no native block-device read is still in
// flight when the caller moves on: the underlying libusb handle is not safe against
// concurrent transfers and would crash natively.
withTimeoutOrNull(IO_THREAD_JOIN_TIMEOUT_MS) { mIoThreadJob?.join() }
?: Log.w(TAG, "I/O worker did not terminate before close")
}
/**
@@ -6,12 +6,14 @@ import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.DelicateCoroutinesApi
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.Job
import kotlinx.coroutines.channels.Channel
import kotlinx.coroutines.channels.ClosedReceiveChannelException
import kotlinx.coroutines.launch
import kotlinx.coroutines.sync.Mutex
import kotlinx.coroutines.sync.withLock
import kotlinx.coroutines.withContext
import kotlinx.coroutines.withTimeoutOrNull
import me.jahnen.libaums.core.driver.BlockDeviceDriver
import java.io.IOException
import java.nio.ByteBuffer
@@ -21,6 +23,14 @@ private const val TAG = "BlockDeviceOutputStream"
private const val BARRIER_TAG = -0xBA881E8L
/**
* Upper bound on how long [BlockDeviceOutputStream.closeAsync] waits for the I/O worker to
* terminate. On a healthy device the worker exits in milliseconds once the channel is closed; the
* timeout only trips if a native libusb transfer is genuinely stuck, in which case the job is
* failing anyway and we must not block close() forever.
*/
private const val IO_THREAD_JOIN_TIMEOUT_MS = 10_000L
private const val TRACE_IO = false
/**
@@ -116,6 +126,12 @@ class BlockDeviceOutputStream(
*/
private val mIoThreadRunning: AtomicBoolean = AtomicBoolean(false)
/**
* The coroutine running the I/O thread, so [closeAsync] can wait for it to fully terminate
* (i.e. no native block-device transfer is still in flight) before returning.
*/
private var mIoThreadJob: Job? = null
/**
* Atomic flag that indicates whether the stream has been closed.
*/
@@ -144,7 +160,7 @@ class BlockDeviceOutputStream(
}
}
coroutineScope.launch(Dispatchers.IO) {
mIoThreadJob = coroutineScope.launch(Dispatchers.IO) {
Thread.currentThread().name = "BlockDeviceOutputStream I/O thread"
if (!mIoThreadRunning.compareAndSet(false, true)) {
@@ -383,5 +399,11 @@ class BlockDeviceOutputStream(
flushAsync()
val channel = mChannelMutex.withLock { mBlockChannel }
channel.close()
// Wait for the I/O worker to fully terminate so no native block-device transfer is still
// in flight when the caller moves on (e.g. the write->verify handoff): the underlying
// libusb handle is not safe against concurrent transfers and would crash natively.
withTimeoutOrNull(IO_THREAD_JOIN_TIMEOUT_MS) { mIoThreadJob?.join() }
?: Log.w(TAG, "I/O worker did not terminate before close")
}
}
@@ -1,4 +1,5 @@
@file:OptIn(ExperimentalCoroutinesApi::class)
@file:Suppress("RunBlockingInSuspendFunction")
package eu.depau.etchdroid
@@ -8,10 +9,15 @@ import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.debug.DebugProbes
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import kotlinx.coroutines.runBlocking
import me.jahnen.libaums.core.driver.BlockDeviceDriver
import org.junit.jupiter.api.AfterAll
import org.junit.jupiter.api.Assertions.assertArrayEquals
import org.junit.jupiter.api.Assertions.assertEquals
import org.junit.jupiter.api.Assertions.assertFalse
import org.junit.jupiter.api.Assertions.assertTrue
import org.junit.jupiter.api.Assertions.fail
import org.junit.jupiter.api.BeforeAll
import org.junit.jupiter.api.Test
@@ -22,6 +28,45 @@ import tech.apter.junit.jupiter.robolectric.RobolectricExtension
import java.io.IOException
import java.lang.Math.min
import java.nio.ByteBuffer
import java.util.concurrent.CountDownLatch
import java.util.concurrent.atomic.AtomicInteger
import kotlin.time.Duration.Companion.milliseconds
/**
* A block device that parks inside its first [read] until [releaseRead] is counted down, signaling
* via [readStarted] once it has entered the call. Used to prove that closing a stream waits for an
* in-flight native transfer to finish (the libusb concurrency crash, ETCHDROID write->verify).
*/
private class ParkingBlockDeviceDriver(
sizeBytes: Long,
override val blockSize: Int,
) : BlockDeviceDriver {
private val backing = ByteArray(sizeBytes.toInt())
override val blocks: Long get() = (backing.size / blockSize).toLong()
val activeReads = AtomicInteger(0)
val readStarted = CountDownLatch(1)
val releaseRead = CountDownLatch(1)
override fun init() {}
override fun read(deviceOffset: Long, buffer: ByteBuffer) {
activeReads.incrementAndGet()
try {
readStarted.countDown()
releaseRead.await()
val start = (deviceOffset * blockSize).toInt()
buffer.put(backing, start, buffer.remaining())
} finally {
activeReads.decrementAndGet()
}
}
override fun write(deviceOffset: Long, buffer: ByteBuffer) {
val start = (deviceOffset * blockSize).toInt()
buffer.get(backing, start, buffer.remaining())
}
}
@ExperimentalCoroutinesApi
@ExtendWith(RobolectricExtension::class)
@@ -54,6 +99,43 @@ class BlockDeviceInputStreamTest {
assertDoesNotThrow { runBlocking { inputStream.closeAsync() } }
}
@Test
fun closeWaitsForInFlightRead() = runBlocking {
// Regression: closeAsync() used to return while the I/O worker was still inside a native
// blockDev.read(). At the write->verify handoff that let two coroutines submit transfers on
// the same libusb handle concurrently, crashing the app natively in libusb_submit_transfer.
val testDev = ParkingBlockDeviceDriver(1L * 1024 * 1024, 512)
val inputStream =
BlockDeviceInputStream(testDev, coroutineScope, bufferBlocks = 1, prefetchBuffers = 1)
// Trigger the worker; it enters read() and parks. readAsync won't return until the worker
// delivers a block, so run it detached (and swallow the close-induced cancellation).
val readJob = coroutineScope.launch {
try {
inputStream.readAsync(ByteArray(4))
} catch (_: Exception) {
// Expected: the channel is closed under us by closeAsync().
}
}
assertTrue(testDev.readStarted.await(5, java.util.concurrent.TimeUnit.SECONDS)) {
"Worker never entered read()"
}
val closeJob = coroutineScope.launch { inputStream.closeAsync() }
// Give closeAsync ample time to run; it must NOT complete while a read is in flight.
delay(500.milliseconds)
assertFalse(closeJob.isCompleted) { "closeAsync returned while a read was still in flight" }
assertTrue(testDev.activeReads.get() > 0) { "Expected a read to be in flight" }
// Let the read finish; close must now complete and no transfer may remain in flight.
testDev.releaseRead.countDown()
closeJob.join()
readJob.join()
assertEquals(0, testDev.activeReads.get())
}
@Test
fun testWithWeirdBlockSize() {
runReadTest(