Skip to content

Fix closing sockets and server sockets - #103

Open
lrytz wants to merge 5 commits into
sbt:2.xfrom
lrytz:pr/server-socket-close
Open

lrytz wants to merge 5 commits into
sbt:2.xfrom
lrytz:pr/server-socket-close

Conversation

@lrytz

@lrytz lrytz commented Nov 4, 2026

Copy link
Copy Markdown
Contributor

Small fixes to what happens when sockets and server sockets are closed, one commit each:

  • On Unix, closing a socket now wakes up threads blocked reading from it or waiting in accept().
  • isBound() and isClosed() now work, close() can be called twice and releases everything, and accept() after or interrupted by close() throws SocketException.
  • On Windows, close() during accept() no longer closes a handle twice.
  • On Windows, closing a server-side socket no longer waits for the client to read.
  • Errors from creating a named pipe keep their original cause.

The commit messages have the details.

Behavior change: on Unix, accept() after close() now throws SocketException instead of IllegalStateException, and calling close() a second time no longer throws. Both match java.net.ServerSocket.

@lrytz lrytz mentioned this pull request Nov 4, 2026
@lrytz
lrytz force-pushed the pr/server-socket-close branch 2 times, most recently from dc7e429 to 29a6a4b Compare November 4, 2026 19:07
@lrytz

lrytz commented Nov 11, 2026

Copy link
Copy Markdown
Contributor Author

@retronym review..?

@retronym retronym left a comment •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 woken accept() to SocketException relies on isClosed() being synchronized, so the accept thread blocks on the monitor until close() has set the flag. Deserves a comment so it survives a future "make it volatile" cleanup. Also, if provider.close throws, isClosed stays false with fd already -1, and the next close() throws again.
  • ReferenceCountedFileDescriptor: a reader woken by the new shutdown returns -1 (EOF) rather than SocketException("Socket closed") as java.net.Socket does. The test accepts either; fine, but worth saying in the commit message.
  • Windows accept(): there is still a window between close()'s CloseHandle and ConnectNamedPipe once accept() has passed the second closed check, where the handle value may already belong to another object. ConnectNamedPipe then fails and connectFailed turns it into SocketException, so benign in practice; same class of hazard as the existing AtomicInteger comment on the Unix side.
  • Windows read wake-up is untested and works by accident: Win32NamedPipeSocket.close() closes readerWaitable under a blocked read, which fails via WAIT_FAILED and then ERROR_INVALID_HANDLE. CloseWakesBlockedTest.closeWakesRead is skipped on Windows. With #102's cancel logic, close() should CancelIoEx the pending read and wait before closing the handles. Follow-up.
  • AcceptCloseTest (Windows) and ServerSocketStateTest.closeDuringAcceptThrowsSocketException (Unix) test the same thing and could be one test.
  • isBound() == true changes toString() from ServerSocket[unbound] to addr=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.

lrytz and others added 5 commits November 11, 2026 16:24
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>
@lrytz
lrytz force-pushed the pr/server-socket-close branch from 29a6a4b to 63ffd34 Compare November 11, 2026 15:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants