Commit 63b736d1 authored by Andy Wilkinson's avatar Andy Wilkinson

Fix NPE when waiting for named socket path to become available

Fixes gh-19941
parent ebcb2ac9
...@@ -36,6 +36,8 @@ import com.sun.jna.platform.win32.Kernel32; ...@@ -36,6 +36,8 @@ import com.sun.jna.platform.win32.Kernel32;
*/ */
public class NamedPipeSocket extends Socket { public class NamedPipeSocket extends Socket {
private static final int WAIT_INTERVAL = 100;
private static final long TIMEOUT = TimeUnit.MILLISECONDS.toNanos(1000); private static final long TIMEOUT = TimeUnit.MILLISECONDS.toNanos(1000);
private final RandomAccessFile file; private final RandomAccessFile file;
...@@ -44,16 +46,14 @@ public class NamedPipeSocket extends Socket { ...@@ -44,16 +46,14 @@ public class NamedPipeSocket extends Socket {
private final OutputStream outputStream; private final OutputStream outputStream;
private final Consumer<String> awaiter;
NamedPipeSocket(String path) throws IOException { NamedPipeSocket(String path) throws IOException {
this.file = open(path); this.file = open(path);
this.inputStream = new NamedPipeInputStream(); this.inputStream = new NamedPipeInputStream();
this.outputStream = new NamedPipeOutputStream(); this.outputStream = new NamedPipeOutputStream();
this.awaiter = Platform.isWindows() ? new WindowsAwaiter() : new SleepAwaiter();
} }
private RandomAccessFile open(String path) throws IOException { private static RandomAccessFile open(String path) throws IOException {
Consumer<String> awaiter = Platform.isWindows() ? new WindowsAwaiter() : new SleepAwaiter();
long startTime = System.nanoTime(); long startTime = System.nanoTime();
while (true) { while (true) {
try { try {
...@@ -63,7 +63,7 @@ public class NamedPipeSocket extends Socket { ...@@ -63,7 +63,7 @@ public class NamedPipeSocket extends Socket {
if (System.nanoTime() - startTime > TIMEOUT) { if (System.nanoTime() - startTime > TIMEOUT) {
throw ex; throw ex;
} }
this.awaiter.accept(path); awaiter.accept(path);
} }
} }
} }
...@@ -139,9 +139,10 @@ public class NamedPipeSocket extends Socket { ...@@ -139,9 +139,10 @@ public class NamedPipeSocket extends Socket {
@Override @Override
public void accept(String path) { public void accept(String path) {
try { try {
Thread.sleep(100); Thread.sleep(WAIT_INTERVAL);
} }
catch (InterruptedException ex) { catch (InterruptedException ex) {
Thread.currentThread().interrupt();
} }
} }
...@@ -154,7 +155,7 @@ public class NamedPipeSocket extends Socket { ...@@ -154,7 +155,7 @@ public class NamedPipeSocket extends Socket {
@Override @Override
public void accept(String path) { public void accept(String path) {
Kernel32.INSTANCE.WaitNamedPipe(path, 100); Kernel32.INSTANCE.WaitNamedPipe(path, WAIT_INTERVAL);
} }
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment