Commit 33d866ae authored by Mygod's avatar Mygod

Ignore IOException for selector.select

parent 02e10607
...@@ -20,12 +20,11 @@ ...@@ -20,12 +20,11 @@
package com.github.shadowsocks.net package com.github.shadowsocks.net
import com.github.shadowsocks.utils.printLog
import kotlinx.coroutines.suspendCancellableCoroutine import kotlinx.coroutines.suspendCancellableCoroutine
import java.io.IOException
import java.nio.ByteBuffer import java.nio.ByteBuffer
import java.nio.channels.Pipe import java.nio.channels.*
import java.nio.channels.SelectableChannel
import java.nio.channels.SelectionKey
import java.nio.channels.Selector
import java.util.concurrent.ConcurrentLinkedQueue import java.util.concurrent.ConcurrentLinkedQueue
import kotlin.coroutines.resume import kotlin.coroutines.resume
...@@ -57,7 +56,7 @@ class ChannelMonitor : Thread("ChannelMonitor"), AutoCloseable { ...@@ -57,7 +56,7 @@ class ChannelMonitor : Thread("ChannelMonitor"), AutoCloseable {
fun register(channel: SelectableChannel, ops: Int, block: (SelectionKey) -> Unit) { fun register(channel: SelectableChannel, ops: Int, block: (SelectionKey) -> Unit) {
pendingRegistrations.add(Triple(channel, ops, block)) pendingRegistrations.add(Triple(channel, ops, block))
val junk = ByteBuffer.allocateDirect(1) val junk = ByteBuffer.allocateDirect(1)
while (registrationPipe.sink().write(junk) == 0); while (running && registrationPipe.sink().write(junk) == 0);
} }
suspend fun wait(channel: SelectableChannel, ops: Int) = suspendCancellableCoroutine<Unit> { cont -> suspend fun wait(channel: SelectableChannel, ops: Int) = suspendCancellableCoroutine<Unit> { cont ->
...@@ -69,7 +68,13 @@ class ChannelMonitor : Thread("ChannelMonitor"), AutoCloseable { ...@@ -69,7 +68,13 @@ class ChannelMonitor : Thread("ChannelMonitor"), AutoCloseable {
override fun run() { override fun run() {
while (running) { while (running) {
if (selector.select() <= 0) continue val num = try {
selector.select()
} catch (e: IOException) {
printLog(e)
continue
}
if (num <= 0) continue
val iterator = selector.selectedKeys().iterator() val iterator = selector.selectedKeys().iterator()
while (iterator.hasNext()) { while (iterator.hasNext()) {
val key = iterator.next() val key = iterator.next()
...@@ -84,5 +89,6 @@ class ChannelMonitor : Thread("ChannelMonitor"), AutoCloseable { ...@@ -84,5 +89,6 @@ class ChannelMonitor : Thread("ChannelMonitor"), AutoCloseable {
selector.wakeup() selector.wakeup()
join() join()
selector.keys().forEach { it.channel().close() } selector.keys().forEach { it.channel().close() }
selector.close()
} }
} }
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