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

Concurrent piped I/O hanging indefinitely #812

Open
tdb-alcorn opened this issue Apr 3, 2023 · 0 comments
Open

Concurrent piped I/O hanging indefinitely #812

tdb-alcorn opened this issue Apr 3, 2023 · 0 comments

Comments

@tdb-alcorn
Copy link

In this Stack Overflow question, we found that the problem experienced occurs in the Scastie playground but not locally.

i.e. this code hangs indefinitely:

import java.io._
import scala.io.Source
import scala.concurrent._
import scala.concurrent.ExecutionContext.Implicits.global

def copy(inputStream: InputStream, outputStream: OutputStream): Unit = {
  val buffer = new Array[Byte](1024)
  var bytesRead = inputStream.read(buffer)
  while (bytesRead != -1) {
    outputStream.write(buffer, 0, bytesRead)
    bytesRead = inputStream.read(buffer)
  }
  outputStream.flush()
}

val os0 = new PipedOutputStream
val is0 = new PipedInputStream(os0)

val os1 = new PipedOutputStream
val is1 = new PipedInputStream(os1)

// write to os0
Future {
  os0.write(("foobarbaz"*1000).getBytes("utf-8"))
  os0.close()
}

// copy is0 to os1
Future {
  copy(is0, os1)
  os1.close()
}

// read is1 to output
val result = Source.fromInputStream(is1).mkString
println(result)

Does scastie handle Futures / multiple threads in some unusual way?

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

No branches or pull requests

1 participant