-
Notifications
You must be signed in to change notification settings - Fork 101
Bound socket writes with a timeout #99
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,161 @@ | ||
| /* | ||
| * Copyright (c) 2021 mobile.dev inc. | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| * | ||
| */ | ||
|
|
||
| package dadb | ||
|
|
||
| import com.google.common.truth.Truth.assertThat | ||
| import org.junit.jupiter.api.Assertions.assertTimeoutPreemptively | ||
| import org.junit.jupiter.api.Test | ||
| import org.junit.jupiter.api.assertThrows | ||
| import java.io.Closeable | ||
| import java.net.InetSocketAddress | ||
| import java.net.ServerSocket | ||
| import java.net.Socket | ||
| import java.net.SocketTimeoutException | ||
| import java.time.Duration | ||
| import java.util.Collections | ||
| import kotlin.concurrent.thread | ||
|
|
||
| /** | ||
| * Tests that a wedged adb connection fails fast instead of hanging forever. | ||
| * | ||
| * `Socket.setSoTimeout` (Dadb's `socketTimeout`) bounds READS only. Writes — the OPEN that begins | ||
| * every shell/pull/push, plus streamed WRITE payloads — are bounded separately by okio's socket | ||
| * sink timeout (see [AdbConnection.WRITE_TIMEOUT_MILLIS]). | ||
| * | ||
| * Both directions are exercised against a REAL emulator (adb port 5555); the wedge is injected | ||
| * deterministically and portably, with no process freezing: | ||
| * - WRITE: route dadb through a transparent TCP [Relay] to real adbd, then stop draining the client | ||
| * side. Real adbd would buffer hundreds of MB, but a relay that stops reading lets only the OS | ||
| * socket buffers absorb the write before it blocks — so the write deadline fires deterministically. | ||
| * - READ: run a command that produces no output (`sleep`); the read blocks until SO_TIMEOUT fires. | ||
| * | ||
| * Requires a booted emulator on adb port 5555. | ||
| */ | ||
| class WriteTimeoutTest { | ||
|
|
||
| /** | ||
| * Transparent byte relay: dadb <-> [Relay] <-> real adbd. Forwards both directions untouched | ||
| * (so the real ADB handshake/protocol run against real adbd); pausing the client->target pump | ||
| * wedges writes. Accepts repeatedly so a connection can be rebuilt after a timeout closes it. | ||
| */ | ||
| private class Relay(private val targetPort: Int) : Closeable { | ||
| private val server = ServerSocket().apply { bind(InetSocketAddress("localhost", 0)) } | ||
| val port: Int get() = server.localPort | ||
|
|
||
| @Volatile private var forwardClientToTarget = true | ||
| @Volatile private var forwardTargetToClient = true | ||
| private val openSockets = Collections.synchronizedList(mutableListOf<Socket>()) | ||
|
|
||
| init { | ||
| thread(isDaemon = true, name = "relay-accept") { | ||
| while (!server.isClosed) { | ||
| val client = try { server.accept() } catch (e: Throwable) { break } | ||
| val target = try { Socket("localhost", targetPort) } catch (e: Throwable) { | ||
| runCatching { client.close() }; continue | ||
| } | ||
| openSockets.add(client); openSockets.add(target) | ||
| // client -> target: pausing it wedges dadb's writes (the peer stops draining). | ||
| pump(client, target) { forwardClientToTarget } | ||
| // target -> client: pausing it wedges dadb's reads (responses never arrive, but | ||
| // the connection stays open, so SO_TIMEOUT fires rather than hitting EOF). | ||
| pump(target, client) { forwardTargetToClient } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| private fun pump(from: Socket, to: Socket, enabled: () -> Boolean) { | ||
| thread(isDaemon = true, name = "relay-pump") { | ||
| val buf = ByteArray(16 * 1024) | ||
| try { | ||
| val input = from.getInputStream() | ||
| val output = to.getOutputStream() | ||
| while (true) { | ||
| while (!enabled()) Thread.sleep(20) // don't start a read while paused | ||
| val n = input.read(buf) | ||
| if (n < 0) break | ||
| while (!enabled()) Thread.sleep(20) // don't forward a read that landed during a pause | ||
| output.write(buf, 0, n) | ||
| output.flush() | ||
| } | ||
| } catch (ignore: Throwable) { | ||
| } | ||
| } | ||
| } | ||
|
|
||
| fun wedgeWrites() { forwardClientToTarget = false } | ||
| fun unwedgeWrites() { forwardClientToTarget = true } | ||
| fun wedgeReads() { forwardTargetToClient = false } | ||
|
|
||
| override fun close() { | ||
| runCatching { server.close() } | ||
| synchronized(openSockets) { openSockets.forEach { runCatching { it.close() } } } | ||
| } | ||
| } | ||
|
|
||
| @Test | ||
| fun `a wedged write fails fast with SocketTimeoutException and the connection recovers`() { | ||
| Relay(targetPort = 5555).use { relay -> | ||
| // writeTimeoutMillis is the internal test seam; production always uses WRITE_TIMEOUT_MILLIS. | ||
| val dadb = DadbImpl( | ||
| host = "localhost", | ||
| port = relay.port, | ||
| keyPair = AdbKeyPair.readDefault(), | ||
| connectTimeout = 5000, | ||
| socketTimeout = 5000, | ||
| writeTimeoutMillis = 1000, | ||
| ) | ||
| try { | ||
| // Real handshake + op, through the relay, against real adbd. | ||
| assertThat(dadb.shell("echo warmup").allOutput.trim()).isEqualTo("warmup") | ||
|
|
||
| // Wedge: relay stops draining the client side, so a large write stalls. | ||
| relay.wedgeWrites() | ||
| val bigCommand = "x".repeat(32 * 1024 * 1024) // overflows the OS socket buffers (incl. larger Linux defaults) | ||
| assertThrows<SocketTimeoutException> { | ||
| assertTimeoutPreemptively(Duration.ofSeconds(8)) { dadb.shell(bigCommand) } | ||
| } | ||
|
|
||
| // The timed-out write closed the socket; once forwarding resumes, the next op rebuilds. | ||
| relay.unwedgeWrites() | ||
| assertThat(dadb.shell("echo recovered").allOutput.trim()).isEqualTo("recovered") | ||
| } finally { | ||
| runCatching { dadb.close() } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| @Test | ||
| fun `a wedged read is bounded by socketTimeout`() { | ||
| Relay(targetPort = 5555).use { relay -> | ||
| val dadb = Dadb.create("localhost", relay.port, connectTimeout = 5000, socketTimeout = 1000) | ||
| try { | ||
| // Real handshake + op, through the relay, against real adbd. | ||
| assertThat(dadb.shell("echo warmup").allOutput.trim()).isEqualTo("warmup") | ||
|
|
||
| // Wedge: adbd's responses stop reaching dadb, so the next op's read blocks until | ||
| // SO_TIMEOUT fires (the connection stays open, so this is a timeout, not an EOF). | ||
| relay.wedgeReads() | ||
| assertThrows<SocketTimeoutException> { | ||
| assertTimeoutPreemptively(Duration.ofSeconds(8)) { dadb.shell("echo hi") } | ||
| } | ||
| } finally { | ||
| runCatching { dadb.close() } | ||
| } | ||
| } | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I like the approach overall, but I'm not sure socket writes are the only place dadb can get stuck, this specifically guards the socket write syscall, right? My review angle was "is there anything else inside dadb that can hang?", and two things stood out:
I dug into the dadb + maestro code with Claude and found it comes from two threads sharing one dadb connection. The driver tunnels its gRPC channel over the same dadb connection, so besides the consumer (test-runner) thread there's a gRPC reader thread doing keep-alive reads:
The potential hang, in MessageQueue.take():
T is never in read() or write(), so neither timeout fires. This points to needing a timeout on the await() here.