Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix 2378; Allow to read from stdin when using run command #2384

Merged

Conversation

WojciechMazur
Copy link
Contributor

This PR fixes long-standing issues with problems when trying to read from stdin in created executable when running it directly from sbt using run task.
I've observed that after changing Scala Proces Builder argument connectInput to true application managed to run correctly and read from stdin, however Thread wrapping this process was failing with exception each time. Because of that I've changed the implementation to use Java ProcessBuilder which seems to be more stable as it's more simple (it does not wrap process in a dedicated thread).

Here's an example stack trace of exceptions from Scala Process (click to expand)
Exception in thread "Thread-969" java.lang.InterruptedException
        at java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject.reportInterruptAfterWait(AbstractQueuedSynchronizer.java:2014)
        at java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject.await(AbstractQueuedSynchronizer.java:2048)
        at java.util.concurrent.LinkedBlockingQueue.take(LinkedBlockingQueue.java:442)
        at sbt.internal.util.Terminal$WriteableInputStream.read(Terminal.scala:514)
        at sbt.internal.util.Terminal$WriteableInputStream.read(Terminal.scala:521)
        at sbt.internal.util.Terminal$proxyInputStream$.read(Terminal.scala:663)
        at sbt.internal.util.Terminal$SimpleInputStream.read(Terminal.scala:610)
        at sbt.internal.util.Terminal$SimpleInputStream.read$(Terminal.scala:609)
        at sbt.internal.util.Terminal$proxyInputStream$.read(Terminal.scala:618)
        at java.io.FilterInputStream.read(FilterInputStream.java:133)
        at java.io.FilterInputStream.read(FilterInputStream.java:107)
        at scala.sys.process.BasicIO$.loop$1(BasicIO.scala:238)
        at scala.sys.process.BasicIO$.transferFullyImpl(BasicIO.scala:246)
        at scala.sys.process.BasicIO$.transferFully(BasicIO.scala:227)
        at scala.sys.process.BasicIO$.connectToIn(BasicIO.scala:196)
        at scala.sys.process.BasicIO$.$anonfun$input$1(BasicIO.scala:203)
        at scala.sys.process.BasicIO$.$anonfun$input$1$adapted(BasicIO.scala:202)
        at scala.sys.process.ProcessBuilderImpl$Simple.$anonfun$run$2(ProcessBuilderImpl.scala:79)
        at scala.sys.process.ProcessImpl$Spawn$$anon$1.run(ProcessImpl.scala:27)

@WojciechMazur WojciechMazur linked an issue Sep 23, 2021 that may be closed by this pull request
Copy link
Member

@ekrich ekrich left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM - it didn't occur to me how to fix this.

@WojciechMazur
Copy link
Contributor Author

WojciechMazur commented Sep 23, 2021

@ekrich can you confirm if it does work on MacOS? I only could ensure that it works correctly on Windows and Linux. I was wondering if the issue might be platform specific

@ekrich
Copy link
Member

ekrich commented Sep 24, 2021

No macOS in CI? I can try tomorrow.

@WojciechMazur WojciechMazur added this to the 0.4.1 milestone Sep 24, 2021
@ekrich
Copy link
Member

ekrich commented Sep 24, 2021

@WojciechMazur I tested with macOS and it worked. In sandbox:

object Test {
  def main(args: Array[String]): Unit = {
    println("Hello, World!")
    println("Please enter an integer:")
    val i = scala.io.StdIn.readInt()
    println(s"You entered a $i")
    println("Done")
  }
}

Result:

[info] Compiling to native code (4589 ms)
[info] Linking native code (immix gc, none lto) (137 ms)
[info] Total (11580 ms)
Hello, World!
Please enter an integer:
9
You entered a 9
Done
[success] Total time: 138 s (02:18), completed Sep 24, 2021 8:10:10 AM

@WojciechMazur WojciechMazur merged commit 48962cd into scala-native:master Sep 27, 2021
WojciechMazur added a commit to WojciechMazur/scala-native that referenced this pull request Sep 27, 2021
* Use java.lang.ProcessBuilder to run compiled executable in sbt
@WojciechMazur WojciechMazur deleted the sbtPlugin/fix-io-inheritence branch December 24, 2021 05:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Cannot read from stdin via the plugin/sbt
3 participants