Skip to content

Commit 5e0f944

Browse files
committed
Ensure the VPN shuts down cleanly now that read() blocks
1 parent a48b89d commit 5e0f944

File tree

1 file changed

+20
-18
lines changed

1 file changed

+20
-18
lines changed

app/src/main/java/tech/httptoolkit/android/ProxyVpnRunnable.kt

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import tech.httptoolkit.android.vpn.socket.SocketNIODataService
1010
import io.sentry.Sentry
1111
import java.io.FileInputStream
1212
import java.io.FileOutputStream
13+
import java.io.InterruptedIOException
1314
import java.net.InetSocketAddress
1415
import java.nio.ByteBuffer
1516

@@ -68,26 +69,27 @@ class ProxyVpnRunnable(
6869

6970
running = true
7071
while (running) {
71-
data = packet.array()
72-
73-
length = vpnReadStream.read(data)
74-
if (length > 0) {
75-
try {
76-
packet.limit(length)
77-
handler.handlePacket(packet)
78-
} catch (e: Exception) {
79-
Sentry.capture(e)
80-
Log.e(TAG, e.message)
81-
}
82-
83-
packet.clear()
84-
} else {
85-
try {
72+
try {
73+
data = packet.array()
74+
75+
length = vpnReadStream.read(data)
76+
if (length > 0) {
77+
try {
78+
packet.limit(length)
79+
handler.handlePacket(packet)
80+
} catch (e: Exception) {
81+
Sentry.capture(e)
82+
Log.e(TAG, e.message)
83+
}
84+
85+
packet.clear()
86+
} else {
8687
Thread.sleep(10)
87-
} catch (e: InterruptedException) {
88-
Log.d(TAG, "Failed to sleep: " + e.message)
8988
}
90-
89+
} catch (e: InterruptedException) {
90+
Log.i(TAG, "Sleep interrupted: " + e.message)
91+
} catch (e: InterruptedIOException) {
92+
Log.i(TAG, "Read interrupted: " + e.message)
9193
}
9294
}
9395

0 commit comments

Comments
 (0)