8000 Replicate failing to open port on Windows by mdedetrich · Pull Request #7084 · sbt/sbt · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Replicate failing to open port on Windows #7084

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

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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
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"
sbt "-Dsbt.io.virtual=false" nativeImage
# test launcher script
echo build using JDK 8, test using JDK 8, on Windows
Expand Down
9 changes: 7 additions & 2 deletions main-command/src/main/scala/xsbt/IPC.scala
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ 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 @@ -34,7 +34,12 @@ object IPC {

def createServer(attempts: Int): ServerSocket =
if (attempts > 0) {
try new ServerSocket(nextPort, 1, loopback)
val backlog =
if (sys.props("os.name").toLowerCase(java.util.Locale.ROOT).contains("windows"))
2
else
1
try new ServerSocket(nextPort, backlog, loopback)
catch { case NonFatal(_) => createServer(attempts - 1) }
} else sys.error("Could not connect to socket: maximum attempts exceeded")

Expand Down
30 changes: 30 additions & 0 deletions main-command/src/test/scala/xsbt/IPCSpec.scala
94FA
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
* 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 {
val server = IPC.unmanagedServer
(1 until 500).foreach { index =>
try {
IPC.client(server.port)(identity)
} catch {
case e: Throwable =>
val message = s"failure index is $index"
info(message)
throw e
}
}
server.close()
}
}
}
0