Merge pull request #5677 from adpi2/fix/exit-bsp-client

Exit BspClient after server socket is closed
This commit is contained in:
eugene yokota 2020-07-27 12:55:31 -04:00 committed by GitHub
commit 2072eba0a9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 34 additions and 11 deletions

View File

@ -18,24 +18,47 @@ import scala.sys.process.Process
import scala.util.control.NonFatal import scala.util.control.NonFatal
class BspClient private (sbtServer: Socket) { class BspClient private (sbtServer: Socket) {
private val lock = new AnyRef
private var terminated = false
private def transferTo(input: InputStream, output: OutputStream): Unit = { private def transferTo(input: InputStream, output: OutputStream): Thread = {
val buffer = Array.ofDim[Byte](1024) val thread = new Thread {
while (true) { override def run(): Unit = {
val size = input.read(buffer) val buffer = Array.ofDim[Byte](1024)
output.write(buffer, 0, size) try {
output.flush() while (!terminated) {
val size = input.read(buffer)
if (size == -1) {
terminated = true
} else {
output.write(buffer, 0, size)
output.flush()
}
}
input.close()
output.close()
} catch {
case NonFatal(_) => ()
} finally {
lock.synchronized {
terminated = true
lock.notify()
}
}
}
} }
thread.setDaemon(true)
thread
} }
private def run(): Exit = { private def run(): Exit = {
try { try {
val redirection = new Thread { transferTo(sbtServer.getInputStream, System.out).start()
override def run(): Unit = transferTo(sbtServer.getInputStream, System.out) transferTo(System.in, sbtServer.getOutputStream).start()
}
redirection.start() lock.synchronized {
transferTo(System.in, sbtServer.getOutputStream) while (!terminated) lock.wait()
}
Exit(0) Exit(0)
} catch { } catch {