Skip to content

Commit 381f841

Browse files
committed
Try this...
1 parent 412e4d3 commit 381f841

File tree

1 file changed

+19
-10
lines changed

1 file changed

+19
-10
lines changed

utils/socket-utils/src/main/java17/datadog/common/socket/TunnelingJdkSocket.java

+19-10
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import java.nio.channels.Selector;
1616
import java.nio.channels.SocketChannel;
1717
import java.nio.file.Path;
18+
import java.util.Iterator;
1819

1920
/**
2021
* Subtype UNIX socket for a higher-fidelity impersonation of TCP sockets. This is named "tunneling"
@@ -128,19 +129,27 @@ public InputStream getInputStream() throws IOException {
128129
ByteBuffer buffer = ByteBuffer.allocate(256);
129130

130131
try {
131-
if (selector.select(timeout) == 0) {
132-
System.out.println("Timeout (" + timeout + "ms) while waiting for data.");
133-
}
134-
for (SelectionKey key : selector.selectedKeys()) {
135-
if (key.isReadable()) {
136-
int r = unixSocketChannel.read(buffer);
137-
if (r == -1) {
138-
unixSocketChannel.close();
139-
System.out.println("Not accepting client messages anymore.");
132+
while (true) {
133+
if (selector.select(timeout) == 0) {
134+
System.out.println("Timeout (" + timeout + "ms) while waiting for data.");
135+
break;
136+
}
137+
Iterator<SelectionKey> keyIterator = selector.selectedKeys().iterator();
138+
while (keyIterator.hasNext()) {
139+
SelectionKey key = keyIterator.next();
140+
keyIterator.remove();
141+
if (key.isReadable()) {
142+
int r = unixSocketChannel.read(buffer);
143+
if (r == -1) {
144+
unixSocketChannel.close();
145+
System.out.println("Not accepting client messages anymore.");
146+
return InputStream.nullInputStream();
147+
}
140148
}
141149
}
150+
buffer.flip();
151+
break;
142152
}
143-
buffer.flip();
144153
} finally {
145154
selector.close();
146155
}

0 commit comments

Comments
 (0)