-
Notifications
You must be signed in to change notification settings - Fork 144
Description
Description:
Hi TLS-Attacker community,
I recently encountered an issue where the server was getting stuck indefinitely during the serverSocket.accept()
call in the initialize()
method of the TransportHandler
. After debugging, I realized that this was because accept()
is a blocking operation, and it waits indefinitely until a connection is established.
To solve this, I added a line to set a timeout on the serverSocket
before the accept()
call. Here’s the updated code snippet:
public void initialize() throws IOException {
if (socketManagement != SocketManagement.EXTERNAL_SOCKET) {
if (serverSocket == null || serverSocket.isClosed()) {
throw new IOException("TransportHandler not preinitialized");
}
serverSocket.setSoTimeout((int) timeout); // Added timeout here
socket = serverSocket.accept();
socket.setSoTimeout((int) timeout); // Ensures the socket has the same timeout
}
dstPort = socket.getPort();
cachedSocketState = null;
LOGGER.info("Connection established from ports {} -> {}", srcPort, dstPort);
setStreams(new PushbackInputStream(socket.getInputStream()), socket.getOutputStream());
}
Root Cause:
The serverSocket.accept()
method blocks until a connection is established, which caused the program to hang indefinitely when no client connected.
Solution:
By setting a timeout using:
serverSocket.setSoTimeout((int) timeout);
an IOException
will now be thrown if a connection is not established within the specified timeout period. This ensures that the server does not get stuck in the accept()
method indefinitely.
Impact:
This fix ensures the TransportHandler
is more robust and prevents unexpected hangs during connection establishment, especially in scenarios where no client connects or there are network issues.
Let me know if you need additional details or if this solution could be improved further!