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

Use better method to find free port #7083

Draft
wants to merge 1 commit into
base: 1.8.x
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,7 @@ jobs:
shell: bash
run: |
# test building sbtn on Windows
sbt "commandProj/testOnly xsbt.IPCSpec"
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is to make sure the added test runs on Windows

sbt "-Dsbt.io.virtual=false" nativeImage
# test launcher script
echo build using JDK 8, test using JDK 8, on Windows
Expand Down
16 changes: 6 additions & 10 deletions main-command/src/main/scala/xsbt/IPC.scala
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,7 @@ import scala.annotation.tailrec
import scala.util.control.NonFatal

object IPC {
private val portMin = 1025
private val portMax = 65536
private val loopback = InetAddress.getByName(null)
private[xsbt] val loopback = InetAddress.getByName(null)

def client[T](port: Int)(f: IPC => T): T = ipc(new Socket(loopback, port))(f)

Expand All @@ -29,12 +27,10 @@ object IPC {
def unmanagedServer: Server = new Server(makeServer)

def makeServer: ServerSocket = {
val random = new java.util.Random
def nextPort = random.nextInt(portMax - portMin + 1) + portMin

def createServer(attempts: Int): ServerSocket =
if (attempts > 0) {
try new ServerSocket(nextPort, 1, loopback)
try new ServerSocket(0, 1, loopback)
catch { case NonFatal(_) => createServer(attempts - 1) }
} else sys.error("Could not connect to socket: maximum attempts exceeded")

Expand Down Expand Up @@ -63,18 +59,18 @@ object IPC {
finally s.close()

final class Server private[IPC] (s: ServerSocket) {
def port = s.getLocalPort
def close() = s.close()
def port: Int = s.getLocalPort
def close(): Unit = s.close()
def isClosed: Boolean = s.isClosed
def connection[T](f: IPC => T): T = IPC.ipc(s.accept())(f)
}
}

final class IPC private (s: Socket) {
def port = s.getLocalPort
def port: Int = s.getLocalPort
private val in = new BufferedReader(new InputStreamReader(s.getInputStream))
private val out = new BufferedWriter(new OutputStreamWriter(s.getOutputStream))

def send(s: String) = { out.write(s); out.newLine(); out.flush() }
def send(s: String): Unit = { out.write(s); out.newLine(); out.flush() }
def receive: String = in.readLine()
}
20 changes: 20 additions & 0 deletions main-command/src/test/scala/xsbt/IPCSpec.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/*
* sbt
* Copyright 2011 - 2018, Lightbend, Inc.
* Copyright 2008 - 2010, Mark Harrah
* Licensed under Apache License 2.0 (see LICENSE)
*/
package xsbt

import org.scalatest.flatspec.AnyFlatSpec
import org.scalatest.matchers.should.Matchers._

class IPCSpec extends AnyFlatSpec {
"server" should "find free open ports and close them" in {
noException should be thrownBy {
(1 until 100).foreach { _ =>
IPC.unmanagedServer.close()
}
}
}
}