Skip to content

Add gracefulCloseWith, given up on through STM - #658

Merged
kazu-yamamoto merged 4 commits into
haskell:masterfrom
kazu-yamamoto:graceful-close-with
Nov 19, 2026
Merged

kazu-yamamoto merged 4 commits into
haskell:masterfrom
kazu-yamamoto:graceful-close-with

Conversation

@kazu-yamamoto

@kazu-yamamoto kazu-yamamoto commented Nov 17, 2026 •

Copy link
Copy Markdown
Collaborator

For #618: the part a graceful-close manager needs from network, which can then stop pending closes without throwTo.

  • gracefulCloseWith :: Socket -> STM a -> IO (Either a Drained), and unsafeGracefulCloseWith, given up on through an STM action as the other ...With functions are. Left when it gave up; Right PeerClosed when the peer's FIN came (or the connection was already gone), Right DrainLimit when the peer sent 128 KiB without closing. The socket is closed either way.
  • gracefulClose and unsafeGracefulClose are built on them with a registerDelay deadline, unchanged in behaviour; the non-threaded RTS keeps its timeout path.
  • Which close comes at the end is the only difference between the safe and unsafe variants: gracefulClose and gracefulCloseWith end with close, whose closeFdWith wakes any other thread waiting on the socket; unsafeGracefulClose and unsafeGracefulCloseWith end with unsafeClose, for a caller which knows no other thread is waiting on it.
  • The Haddock of gracefulClose mentions the drain limit.
  • The drain reads with recvBufNoWait once the socket is readable, so a thread which loses the data to another reader (as when a close runs twice at once, CLOSE_WAIT #590) waits again under the give-up instead of blocking past it.
  • recvBufNoWait on Windows calls recv directly, as the socket is now non-blocking: it returns 0 at the end of the data rather than -1, as on POSIX.
  • Tests: a peer which does not close makes gracefulClose wait about its timeout; gracefulCloseWith reports PeerClosed, gives up when told to, gives up even if another thread takes the data (this one hangs with the old drain), and cuts off a peer which keeps sending; recvBufNoWait tells -1 from 0.

Tested locally on Linux, macOS and Windows (WinIO).

🤖 Generated with Claude Code

kazu-yamamoto added a commit to kazu-yamamoto/network that referenced this pull request Nov 17, 2026
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
kazu-yamamoto and others added 4 commits November 20, 2026 08:35
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
recvBufNoWait on Windows now tells the end of the data (0) from
nothing to read (-1), as on POSIX.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…wins

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@kazu-yamamoto
kazu-yamamoto merged commit 87851a8 into haskell:master Nov 19, 2026
9 checks passed
@kazu-yamamoto
kazu-yamamoto deleted the graceful-close-with branch November 19, 2026 23:50
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.

1 participant