Skip to content

socket: Thread#kill can interrupt a blocked accept — the unblock function - #935

Merged
tannevaled merged 2 commits into
mainfrom
socket-unblock
Nov 14, 2026
Merged

tannevaled merged 2 commits into
mainfrom
socket-unblock

Conversation

@tannevaled

Copy link
Copy Markdown
Contributor

socket: Thread#kill can interrupt a blocked accept — the unblock function

threadRelease runs its fn to completion, so a Thread#kill arriving while a
thread is parked in accept(2) is only noticed after fn returns — which,
for an accept nobody will connect to, is never.

MRI 4.0.6 rbgo before rbgo after
alive? after t.kill false at 0.1 s still true at 3.0 s false at 0.1 s

Same program both sides, the one in #933.

The mechanism

MRI's answer is the unblock function of rb_thread_call_without_gvl, whose
job is to make the call return. rbgo already had the other half:
interruptibleWait selects on the thread's interrupt channel for every wait
rbgo owns — join, Queue#pop, Mutex#lock. A wait the kernel owns cannot
go into a select, so ioBlockCancel puts a watcher on that same channel and
calls a cancel function instead: SetDeadline(now), which makes the call return
without closing anything.

Two details the design had to get right, both written down before any code:

  • net.Listener does not declare SetDeadline — the interface is
    Accept/Close/Addr. The capability is asked for (socketDeadliner)
    rather than assumed, and the deadline is cleared afterwards so a listener that
    outlives the interrupted thread still accepts.
  • ⛔ An interrupted wait is not a failed one. The error after cancelling is
    the deadline we set ourselves; raising it would hand a killed thread an
    IOError MRI never raises. accept returns instead, and the thread unwinds at
    its next safepoint — which is what safepoints are for.

The answer travels on a channel, not a shared bool: a flag written by the
watcher and read by the caller is a data race, and -race would say so.

⛔ My own GVL gate refused this first

ioBlockCancel is a new releaser, so the Accept inside it read as unwrapped
until the wrapper was named in the one map where that exemption lives.
Inferring it would have defeated the point of the map. That is the gate from
#924 catching the author of the gate, one day later.

Measured, and one number deliberately not quoted

library/socket/tcpserver/accept_spec.rb — the exact file whose site this
changes — is gone from the kill list: nine files killed by the clock across
library/socket + library/timeout before, eight after.

⛔ The passing-example delta from that sweep is not quoted, because the
control is broken there: MRI scores 7 on those 190 files. The cause is now
known — library/socket/spec_helper.rb calls MSpec.enable_feature, and the
conformance shim defines no MSpec, so the reference stops at load. A control
that scores 7 measures nothing, and +217 against it would mean nothing.

Why one site and not twelve

Twelve ioBlock sites remain (socket.go ×6, socket_raw.go ×6). Each needs
its own canceller — listener, conn, packet conn — and its own decision about the
error it returns. Converting them all in one change, each with its own
concurrency semantics, is how a race gets shipped; this lands the primitive with
one site proven end to end against the reference, which is the same ratchet
argument this repository already uses for the GVL gate itself.

The remaining sites are listed in #933 as the follow-up, and the seven
library/socket files still killed name them precisely: recv, send,
recvfrom, connect_nonblock, tcp_server_loop, udp_server_loop,
unixserver/accept.

112 tests pass under -race, no data race, gate green at 33 calls.

Refs #933, #916, #924

(#933 stays open: one site of thirteen is converted. #916 stays open: eight
files still hang. #924 is the merged pull request whose gate refused this one
until the new wrapper was named.)

🤖 Generated with Claude Code

tannevaled and others added 2 commits November 14, 2026 08:37
…ion)

threadRelease runs fn to completion, so a kill arriving while a thread is
parked in accept(2) is only noticed after fn returns -- which, for an accept
nobody will connect to, is never. Measured: MRI kills such a thread within
200 ms, rbgo left it alive after 3 full seconds (#933).

MRI's answer is the unblock function of rb_thread_call_without_gvl, whose job
is to make the call RETURN. rbgo already had the other half: interruptibleWait
selects on the thread's interrupt channel for every wait rbgo owns (join,
Queue#pop, Mutex#lock). A wait the KERNEL owns cannot go in a select, so
ioBlockCancel puts a watcher on the SAME channel and calls a cancel function
instead -- here SetDeadline(now), which makes the call return without closing
anything.

Two things the design had to get right, both written down before coding:

  - net.Listener does NOT declare SetDeadline; the interface is
    Accept/Close/Addr. The capability is asked for (socketDeadliner) rather
    than assumed, and the deadline is cleared afterwards so a listener that
    outlives the interrupted thread still accepts.
  - ⛔ AN INTERRUPTED WAIT IS NOT A FAILED ONE. The error after cancelling is
    the deadline we ourselves set; raising it would hand a killed thread an
    IOError MRI never raises. accept returns instead, and the thread unwinds
    at its next safepoint, which is what safepoints are for.

The answer travels on a channel, not a shared bool: a flag written by the
watcher and read by the caller is a data race, and -race would say so.

⛔ MY OWN GVL GATE REFUSED THIS FIRST, which is the gate working. ioBlockCancel
is a new releaser, so the Accept inside it read as unwrapped until the wrapper
was NAMED in the one map where that exemption lives. Inferring it would have
defeated the point.

Verified against the oracle, same program both sides:

  rbgo   ["alive after kill", false, "waited", 0.1]
  MRI    ["alive after kill", false, "waited", 0.1]

112 tests pass under -race, no data race, gate green at 33 calls.

Refs #933, #916

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
The ratchet reported unblockNow at 0.0%, newly below 100%, and it was right
in the most useful way: NOTHING IN THE SUITE EVER INTERRUPTS A BLOCKING CALL.
I had proved the fix with a script run by hand, so nothing would have noticed
if the cancel stopped working.

The test kills a thread parked in accept and asserts it dies, and then asserts
something I had claimed in a comment and never checked: that the LISTENER
SURVIVES. The cancel works by putting the deadline in the past, so without
clearing it afterwards every later accept would fail at once — a fix that
trades a hang for a broken server. The second half of the test accepts a real
connection after the interrupted one.

It waits on status == "sleep" rather than on a sleep of its own, which is what
ruby/spec does here, so a slow machine makes it slower and not flaky.

Measured before pushing with covratchet -require: ioBlockCancel, unblockNow,
clearDeadline and registerTCPServer all at 100%. Passes under -race in 0.09s.

Refs #933

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
@tannevaled
tannevaled merged commit 72351e8 into main Nov 14, 2026
26 checks passed
@tannevaled
tannevaled deleted the socket-unblock branch November 14, 2026 20:07
tannevaled added a commit that referenced this pull request Nov 15, 2026
…938)

Every figure here was already measured and merged; none of it needed a new run.
What it needed was someone to ask which published sentences the merges had made
false, which is a different question from "what did we fix".

## The four

1. `library/socket` **8 killed -> 7**, and the headline **75 -> 9** is **75 -> 8**.
   #935 measured it in the sentence "nine files killed by the clock across
   `library/socket` + `library/timeout` before, eight after".

2. A withdrawn figure. The README said the remaining hangs were "275 passing
   against MRI's 371". That control scored **7** on those 190 files, because
   `library/socket/spec_helper.rb` calls `MSpec.enable_feature` and the shim
   defined no `MSpec` (#936). A number counted against a broken control is not a
   smaller number, it is not a number; it is removed rather than adjusted.

3. The corpus map's "the rest is concentrated: matrix 233, bigdecimal 180, ..."
   list was compiled while `library/socket` contributed a flat **zero on both
   sides**. Repaired, socket is MRI 1790 / rbgo 224 -- about **-1560**, larger
   than every module on that list put together (#937). The two totals are
   deliberately **not summed**: different runs, different MRI patch releases
   (4.0.7 and 4.0.6), and a sum across two measurements is how two errors cancel.
   The published gap is marked a **floor** instead.

4. `docs/SHARED-ENGINES.md` still concluded, at the top of its `net-ftp` section,
   that **"#916 is not closed, because the measurement did not move ... still 39
   killed"**, and told the reader `net-ftp` was not available for extraction.
   That was true on 2026-11-13 and is the opposite of the outcome: #927 released
   the GVL on the **data** connection and #928 added `Net::FTP.default_passive`,
   and net-ftp is **0 killed, -128**. The section now records why the inference
   was wrong -- "the fix did not move the measurement" was read as "the fix is
   incomplete" when the mechanism was wired at only some of its doors -- and
   lifts the extraction blocker while naming the -128 the new home inherits.

## Why a doc gate did not catch any of them

Seven README drift gates run in this repository's root package and all seven are
green both before and after this change: they check the conformance total
against `BASELINE`, the Ruby version against the runtime, the Go floor, the
numeric surface, the wasm lane, and the shim line numbers the prose cites. Not
one of them looks at a figure in a dated prose table, because there is nothing
to compare such a figure against -- the run that produced it is gone.

That is the honest limit, and it is stated in the README rather than papered
over: the dated map is explicitly "not a gate", and the only number CI enforces
is per-file. The gates caught the thing they can catch -- when a 22-line
insertion moved two cited line numbers, every lane went red.

Refs #916, #933, #935, #936, #937

Co-authored-by: Claude Opus 5 <noreply@anthropic.com>
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