Commit fe58f13e authored by Mygod's avatar Mygod

Prevent blocking sleep

parent 3cfc17c2
...@@ -48,7 +48,6 @@ class GuardedProcessPool(private val onFatal: (IOException) -> Unit) : Coroutine ...@@ -48,7 +48,6 @@ class GuardedProcessPool(private val onFatal: (IOException) -> Unit) : Coroutine
private inner class Guard(private val cmd: List<String>) { private inner class Guard(private val cmd: List<String>) {
private val abortChannel = Channel<Unit>() private val abortChannel = Channel<Unit>()
private var startTime: Long = -1
private lateinit var process: Process private lateinit var process: Process
private fun streamLogger(input: InputStream, logger: (String) -> Unit) = try { private fun streamLogger(input: InputStream, logger: (String) -> Unit) = try {
...@@ -56,7 +55,6 @@ class GuardedProcessPool(private val onFatal: (IOException) -> Unit) : Coroutine ...@@ -56,7 +55,6 @@ class GuardedProcessPool(private val onFatal: (IOException) -> Unit) : Coroutine
} catch (_: IOException) { } // ignore } catch (_: IOException) { } // ignore
fun start() { fun start() {
startTime = SystemClock.elapsedRealtime()
process = ProcessBuilder(cmd).directory(Core.deviceStorage.noBackupFilesDir).start() process = ProcessBuilder(cmd).directory(Core.deviceStorage.noBackupFilesDir).start()
} }
...@@ -64,7 +62,7 @@ class GuardedProcessPool(private val onFatal: (IOException) -> Unit) : Coroutine ...@@ -64,7 +62,7 @@ class GuardedProcessPool(private val onFatal: (IOException) -> Unit) : Coroutine
if (!abortChannel.isClosedForSend) abortChannel.send(Unit) if (!abortChannel.isClosedForSend) abortChannel.send(Unit)
} }
suspend fun looper(onRestartCallback: (() -> Unit)?) { suspend fun looper(onRestartCallback: (suspend () -> Unit)?) {
var running = true var running = true
val cmdName = File(cmd.first()).nameWithoutExtension val cmdName = File(cmd.first()).nameWithoutExtension
val exitChannel = Channel<Int>() val exitChannel = Channel<Int>()
...@@ -72,11 +70,11 @@ class GuardedProcessPool(private val onFatal: (IOException) -> Unit) : Coroutine ...@@ -72,11 +70,11 @@ class GuardedProcessPool(private val onFatal: (IOException) -> Unit) : Coroutine
while (true) { while (true) {
thread(name = "stderr-$cmdName") { streamLogger(process.errorStream) { Log.e(cmdName, it) } } thread(name = "stderr-$cmdName") { streamLogger(process.errorStream) { Log.e(cmdName, it) } }
thread(name = "stdout-$cmdName") { thread(name = "stdout-$cmdName") {
runBlocking {
streamLogger(process.inputStream) { Log.i(cmdName, it) } streamLogger(process.inputStream) { Log.i(cmdName, it) }
exitChannel.send(process.waitFor()) // this thread also acts as a daemon thread for waitFor // this thread also acts as a daemon thread for waitFor
} runBlocking { exitChannel.send(process.waitFor()) }
} }
val startTime = SystemClock.elapsedRealtime()
if (select { if (select {
abortChannel.onReceive { true } // prefer abort to save work abortChannel.onReceive { true } // prefer abort to save work
exitChannel.onReceive { false } exitChannel.onReceive { false }
...@@ -118,7 +116,7 @@ class GuardedProcessPool(private val onFatal: (IOException) -> Unit) : Coroutine ...@@ -118,7 +116,7 @@ class GuardedProcessPool(private val onFatal: (IOException) -> Unit) : Coroutine
private val guards = ArrayList<Guard>() private val guards = ArrayList<Guard>()
@MainThread @MainThread
suspend fun start(cmd: List<String>, onRestartCallback: (() -> Unit)? = null) { suspend fun start(cmd: List<String>, onRestartCallback: (suspend () -> Unit)? = null) {
Crashlytics.log(Log.DEBUG, TAG, "start process: " + Commandline.toString(cmd)) Crashlytics.log(Log.DEBUG, TAG, "start process: " + Commandline.toString(cmd))
val guard = Guard(cmd) val guard = Guard(cmd)
guard.start() guard.start()
......
...@@ -267,11 +267,11 @@ class VpnService : BaseVpnService(), LocalDnsService.Interface { ...@@ -267,11 +267,11 @@ class VpnService : BaseVpnService(), LocalDnsService.Interface {
return conn.fileDescriptor return conn.fileDescriptor
} }
private fun sendFd(fd: FileDescriptor) { private suspend fun sendFd(fd: FileDescriptor) {
var tries = 0 var tries = 0
val path = File(Core.deviceStorage.noBackupFilesDir, "sock_path").absolutePath val path = File(Core.deviceStorage.noBackupFilesDir, "sock_path").absolutePath
while (true) try { while (true) try {
Thread.sleep(50L shl tries) delay(50L shl tries)
LocalSocket().use { localSocket -> LocalSocket().use { localSocket ->
localSocket.connect(LocalSocketAddress(path, LocalSocketAddress.Namespace.FILESYSTEM)) localSocket.connect(LocalSocketAddress(path, LocalSocketAddress.Namespace.FILESYSTEM))
localSocket.setFileDescriptorsForSend(arrayOf(fd)) localSocket.setFileDescriptorsForSend(arrayOf(fd))
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment