Skip to content

Commit

Permalink
fix stupid readLine bug that discards unread lines and breaks libuv g…
Browse files Browse the repository at this point in the history
  • Loading branch information
fiatjaf committed Sep 2, 2022
1 parent fe863e7 commit 6bc0163
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 55 deletions.
27 changes: 22 additions & 5 deletions src/main/scala/CLN.scala
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import java.nio.file.{Files, Path, Paths}
import scala.util.Try
import java.nio.charset.StandardCharsets
import scala.util.{Try, Success, Failure}
import scala.util.control.Breaks._
import scala.util.chaining._
import scala.concurrent.ExecutionContext.Implicits.global
import scala.concurrent.duration.FiniteDuration
Expand Down Expand Up @@ -697,10 +699,25 @@ class CLN(master: ChannelMaster) extends NodeInterface {
def main(onInit: () => Unit): Unit = {
initCallback = onInit

StdinReader.readLoop { _ =>
val line = scala.io.StdIn.readLine().trim
if (line.size > 0) {
handleRPC(line)
Poll(0).startRead { _ =>
breakable {
var current = Array.empty[Byte]

while (true) {
Try(scala.Console.in.read()) match {
case Success(char) if char == 10 =>
// newline, we've got a full line, so handle it
val line = new String(current, StandardCharsets.UTF_8).trim()
if (line.size > 0) handleRPC(line)
current = Array.empty
case Success(char) =>
// normal char, add it to the current
current = current :+ char.toByte
case Failure(err) =>
// EOF
break()
}
}
}
}
}
Expand Down
50 changes: 0 additions & 50 deletions src/main/scala/StdinReader.scala

This file was deleted.

0 comments on commit 6bc0163

Please sign in to comment.