Repository navigation
Conversation
dc7e429 to
29a6a4b
Compare
|
@retronym review..? |
There was a problem hiding this comment.
One real problem, the rest are nits.
super.close() is a no-op on JDK 8 and 11. In those JDKs ServerSocket.close() starts with if (isClosed()) return;. Both server sockets now override isClosed() and set it to true before calling super.close(), so the TCP socket the base class may have created (via setSoTimeout etc.) is never closed there. JDK 17+ checks a private field instead, which is why it works locally. Fix: call super.close() first, while isClosed() still reports false; the base method is idempotent under its own lock.
Smaller points:
UnixDomainServerSocket.accept(): mapping the wokenaccept()toSocketExceptionrelies onisClosed()beingsynchronized, so the accept thread blocks on the monitor untilclose()has set the flag. Deserves a comment so it survives a future "make it volatile" cleanup. Also, ifprovider.closethrows,isClosedstays false withfdalready -1, and the nextclose()throws again.ReferenceCountedFileDescriptor: a reader woken by the newshutdownreturns -1 (EOF) rather thanSocketException("Socket closed")asjava.net.Socketdoes. The test accepts either; fine, but worth saying in the commit message.- Windows
accept(): there is still a window betweenclose()'sCloseHandleandConnectNamedPipeonceaccept()has passed the secondclosedcheck, where the handle value may already belong to another object.ConnectNamedPipethen fails andconnectFailedturns it intoSocketException, so benign in practice; same class of hazard as the existingAtomicIntegercomment on the Unix side. - Windows read wake-up is untested and works by accident:
Win32NamedPipeSocket.close()closesreaderWaitableunder a blocked read, which fails viaWAIT_FAILEDand thenERROR_INVALID_HANDLE.CloseWakesBlockedTest.closeWakesReadis skipped on Windows. With #102's cancel logic,close()shouldCancelIoExthe pending read and wait before closing the handles. Follow-up. AcceptCloseTest(Windows) andServerSocketStateTest.closeDuringAcceptThrowsSocketException(Unix) test the same thing and could be one test.isBound() == truechangestoString()fromServerSocket[unbound]toaddr=null,localport=0. Harmless.
Approve once the super.close() ordering is fixed. Branch with that fix and the Unix nits, feel free to cherry-pick: review/103-super-close. Compiles with --release 8; the native tests weren't run on my side.
Closing a socket is the usual way to stop another thread that is blocked reading from it. On Unix, that thread stayed blocked until the other side sent data or closed the connection. The same happened on Linux to a thread waiting in accept() when its server socket was closed. Now close() first shuts the socket down, which wakes up blocked threads. A woken read sees end of stream, where java.net.Socket would throw SocketException. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Several ServerSocket methods didn't behave like those of java.net.ServerSocket: - isBound() and isClosed() always returned false. - Calling close() twice threw an exception on Unix. On Windows it closed an internal handle twice, which can close an unrelated handle that Windows has given the same number in between. - close() didn't release the TCP socket that the base class opens for options such as setSoTimeout(). - On Unix, accept() after close() threw IllegalStateException, which code that handles IOException doesn't expect, and accept() interrupted by close() threw a generic IOException. Both now throw SocketException. - On Windows, if close() ran just as accept() was starting, accept() could wait forever. - Binding a Unix server socket through ServerSocketChannels failed, and binding with a null address bound a TCP port on all network interfaces. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Closing a Windows server socket is the usual way to stop a thread waiting in accept(). close() closes the pipe handle that accept() waits on, and then accept() closed the same handle a second time. By then Windows may have given that handle number to something else, which was then closed by mistake. Depending on timing, accept() could also return a socket whose handle was already closed. Now accept() only uses or closes the handle if close() hasn't taken it, and otherwise throws SocketException. Also, the error message now shows the right error code, and the handle no longer leaks when connecting fails. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Closing a server-side Windows named pipe socket waited until the client had read all data sent to it. If the client was still running but not reading, close() blocked indefinitely. Now close() returns right away. The client can still read the data that was sent before the close. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
When creating a named pipe failed, the server socket threw a new exception with an error code that it looked up afterwards, which by then could belong to a different call. The original exception, with the message from Windows, was lost. Now it is kept as the cause. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
29a6a4b to
63ffd34
Compare
Small fixes to what happens when sockets and server sockets are closed, one commit each:
accept().isBound()andisClosed()now work,close()can be called twice and releases everything, andaccept()after or interrupted byclose()throwsSocketException.close()duringaccept()no longer closes a handle twice.The commit messages have the details.
Behavior change: on Unix,
accept()afterclose()now throwsSocketExceptioninstead ofIllegalStateException, and callingclose()a second time no longer throws. Both matchjava.net.ServerSocket.