Abort thin client on access denied exception

There are situations in windows where it is possible that a client
attempts to connect to a portfile that it did not have access to. This
can happen if, for example, the server is started in administrator mode
but the client is started as a regular user. When this happens, the
client would try to remove the portfile and start a new server. This new
server would not actually be able to start a server though becuase it
would not be able to open the named point because the other server had
it open. As a result, the client would just hang. The fix is to just
abort the thin client if it gets an access denied exception.
This commit is contained in:
Ethan Atkins 2020-09-21 10:58:36 -07:00
parent 56cca22baf
commit 6247362725
1 changed files with 25 additions and 17 deletions

View File

@ -181,8 +181,13 @@ class NetworkClient(
catch { catch {
// This catches a pipe busy exception which can happen if two windows clients // This catches a pipe busy exception which can happen if two windows clients
// attempt to connect in rapid succession // attempt to connect in rapid succession
case e: IOException if e.getMessage.contains("Couldn't open") && attempt < 10 => None case e: IOException if e.getMessage.contains("Couldn't open") && attempt < 10 =>
case e: IOException => throw new ConnectionRefusedException(e) if (e.getMessage.contains("Access is denied") || e.getMessage.contains("(5)")) {
errorStream.println(s"Access denied for portfile $portfile")
throw new NetworkClient.AccessDeniedException
}
None
case e: IOException => throw new ConnectionRefusedException(e)
} }
res match { res match {
case Some(r) => r case Some(r) => r
@ -1093,7 +1098,7 @@ object NetworkClient {
System.exit(Terminal.withStreams(false) { System.exit(Terminal.withStreams(false) {
val term = Terminal.console val term = Terminal.console
try client(base, restOfArgs, term.inputStream, System.err, term, useJNI) try client(base, restOfArgs, term.inputStream, System.err, term, useJNI)
finally { catch { case _: AccessDeniedException => 1 } finally {
Runtime.getRuntime.removeShutdownHook(hook) Runtime.getRuntime.removeShutdownHook(hook)
hook.run() hook.run()
} }
@ -1124,21 +1129,23 @@ object NetworkClient {
val sbtArgs = args.takeWhile(!_.startsWith(NetworkClient.completions)) val sbtArgs = args.takeWhile(!_.startsWith(NetworkClient.completions))
val arguments = NetworkClient.parseArgs(sbtArgs) val arguments = NetworkClient.parseArgs(sbtArgs)
val noTab = args.contains("--no-tab") val noTab = args.contains("--no-tab")
val client =
simpleClient(
arguments.withBaseDirectory(baseDirectory),
inputStream = in,
errorStream = errorStream,
printStream = errorStream,
useJNI = useJNI,
)
try { try {
val results = val client =
if (client.connect(log = false, promptCompleteUsers = true)) client.getCompletions(cmd) simpleClient(
else Nil arguments.withBaseDirectory(baseDirectory),
out.println(results.sorted.distinct mkString "\n") inputStream = in,
0 errorStream = errorStream,
} catch { case _: Exception => 1 } finally client.close() printStream = errorStream,
useJNI = useJNI,
)
try {
val results =
if (client.connect(log = false, promptCompleteUsers = true)) client.getCompletions(cmd)
else Nil
out.println(results.sorted.distinct mkString "\n")
0
} catch { case _: Exception => 1 } finally client.close()
} catch { case _: AccessDeniedException => 1 }
} }
def run(configuration: xsbti.AppConfiguration, arguments: List[String]): Int = def run(configuration: xsbti.AppConfiguration, arguments: List[String]): Int =
@ -1153,4 +1160,5 @@ object NetworkClient {
e.printStackTrace() e.printStackTrace()
1 1
} }
private class AccessDeniedException extends Throwable
} }