Commit 9aa0a4e3 authored by Max Lv's avatar Max Lv

Fix a potential issue in localSocket

parent aa533184
...@@ -41,22 +41,25 @@ abstract class LocalSocketListener(protected val tag: String) : Thread() { ...@@ -41,22 +41,25 @@ abstract class LocalSocketListener(protected val tag: String) : Thread() {
* Inherited class do not need to close input/output streams as they will be closed automatically. * Inherited class do not need to close input/output streams as they will be closed automatically.
*/ */
protected abstract fun accept(socket: LocalSocket) protected abstract fun accept(socket: LocalSocket)
override final fun run() = LocalSocket().use { localSocket -> override final fun run() {
val serverSocket = try { socketFile.delete() // It's a must-have to close and reuse previous local socket.
localSocket.bind(LocalSocketAddress(socketFile.absolutePath, LocalSocketAddress.Namespace.FILESYSTEM)) LocalSocket().use { localSocket ->
LocalServerSocket(localSocket.fileDescriptor) val serverSocket = try {
} catch (e: IOException) { localSocket.bind(LocalSocketAddress(socketFile.absolutePath, LocalSocketAddress.Namespace.FILESYSTEM))
Log.e(tag, "unable to bind", e) LocalServerSocket(localSocket.fileDescriptor)
return
}
while (running) {
try {
serverSocket.accept()
} catch (e: IOException) { } catch (e: IOException) {
Log.e(tag, "Error when accept socket", e) Log.e(tag, "unable to bind", e)
app.track(e) return
null }
}?.use(this::accept) while (running) {
try {
serverSocket.accept()
} catch (e: IOException) {
Log.e(tag, "Error when accept socket", e)
app.track(e)
null
}?.use(this::accept)
}
} }
} }
......
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