Skip to content

vm: release the flock when #close_read or #close_write closes the stream - #981

Merged
tannevaled merged 1 commit into
mainfrom
flock-close-half
Nov 22, 2026
Merged

tannevaled merged 1 commit into
mainfrom
flock-close-half

Conversation

@tannevaled

Copy link
Copy Markdown
Contributor

What

#close_read on a read-only File and #close_write on a write-only one close the whole stream, as rb_io_close_read / rb_io_close_write do. But they never released the descriptor File#flock holds, which #close does. The lock outlived the stream, and a later blocking flock on the same file waited forever.

Each place these two methods set closed now calls releaseLockFd, as close(2) on MRI's descriptor drops its flock. That covers the non-duplex path and the duplex path once both halves are shut.

Measured

  • Oracle: rubydiff against MRI 4.0.7 (arm64-darwin25).
    • The issue's program: rbgo gave no answer within 30 s; MRI answers [[:after_close_read, 0], [:after_close_write, 0]] in 95 ms. They agree now.
    • The same program with every lock LOCK_NB: rbgo answered [false, false, false, true, true], MRI [0, 0, 0, true, true]. They agree now.
  • Test: TestCloseHalfReleasesTheFlock holds the LOCK_NB program with MRI's output as its expected value, so a regression fails instead of hanging. It skips Windows, wasm and plan9, which have no flock(2).
  • Suite: before the rebase onto vm: flush only what a stream wrote, and only into the file it opened #978, go test ./... passed (internal/vm 249 s). After the rebase, the flock, pin and opened-file tests pass. The full suite was not re-run locally at a load average of ~180; CI runs it.
  • Vet: buildscan --vet passed under all 19 CI configurations.

Fixes #979
Refs #978 (merged; this branch is rebased onto it, which also releases its inode pin at these four places)

🤖 Generated with Claude Code

close the whole stream, as rb_io_close_read and rb_io_close_write do,
but never released the descriptor File#flock holds, which #close does.
The lock outlived the stream, so a later blocking flock on the same
file waited forever.

Each place these two methods set `closed` now calls releaseLockFd, as
close(2) on MRI's descriptor drops its flock.

Measured with rubydiff against MRI 4.0.7 (arm64-darwin25): the
issue's program timed out after 30 s on rbgo where MRI answers
[[:after_close_read, 0], [:after_close_write, 0]] in 95 ms; with every
lock LOCK_NB, rbgo answered [false, false, false, true, true] where MRI
answers [0, 0, 0, true, true]. Both agree now.
TestCloseHalfReleasesTheFlock holds the LOCK_NB program, so a
regression fails instead of hanging.

Fixes #979

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@tannevaled
tannevaled merged commit f17e6c6 into main Nov 22, 2026
26 checks passed
@tannevaled
tannevaled deleted the flock-close-half branch November 22, 2026 23:54
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.

File#close_read / #close_write leave the flock held: a later blocking flock on the file hangs

1 participant