diff --git a/internal/vm/io_close_half_flock_test.go b/internal/vm/io_close_half_flock_test.go new file mode 100644 index 00000000..1ae18b2e --- /dev/null +++ b/internal/vm/io_close_half_flock_test.go @@ -0,0 +1,37 @@ +// Copyright (c) the go-embedded-ruby/ruby authors +// +// SPDX-License-Identifier: BSD-3-Clause + +package vm_test + +import ( + "fmt" + "path/filepath" + "runtime" + "testing" +) + +// TestCloseHalfReleasesTheFlock: #close_read on a read-only File and +// #close_write on a write-only one close the whole stream, and with it the +// descriptor File#flock holds, as close(2) does in MRI (#979). Before, the +// lock outlived the stream: every LOCK_NB below answered false, and a blocking +// flock on the file hung. The want is MRI 4.0.7's output for the same program +// (arm64-darwin25, rubydiff, 2026-11-22). Every lock is LOCK_NB so that a +// regression fails here rather than hangs. +func TestCloseHalfReleasesTheFlock(t *testing.T) { + if runtime.GOOS == "windows" || runtime.GOARCH == "wasm" || runtime.GOOS == "plan9" { + t.Skip("no flock(2) on this platform") + } + d := filepath.ToSlash(t.TempDir()) + src := fmt.Sprintf(`path = File.join(%q, "f") +File.write(path, "abc") +r = File.open(path, "r"); r.flock(File::LOCK_EX); r.close_read +a = File.open(path, "r") { |g| g.flock(File::LOCK_EX | File::LOCK_NB) } +w = File.open(path, "w"); c = w.flock(File::LOCK_EX | File::LOCK_NB); w.close_write +b = File.open(path, "r") { |g| g.flock(File::LOCK_EX | File::LOCK_NB) } +p [a, c, b, r.closed?, w.closed?] +`, d) + if got, want := eval(t, src), "[0, 0, 0, true, true]\n"; got != want { + t.Errorf("got=%q\nwant=%q", got, want) + } +} diff --git a/internal/vm/io_descriptors.go b/internal/vm/io_descriptors.go index 0a7a0866..b1e664f8 100644 --- a/internal/vm/io_descriptors.go +++ b/internal/vm/io_descriptors.go @@ -224,12 +224,14 @@ func defIOReadExtra(cls *RClass) { o.rdClosed, o.closed = true, true o.pipeEndClosed() o.unpin() + o.releaseLockFd() // close(2) drops any flock this stream still holds (#979) return object.NilV } o.rdClosed = true if o.wrClosed { // both halves shut ⇒ the stream is fully closed o.closed = true o.unpin() + o.releaseLockFd() } return object.NilV }) @@ -249,6 +251,7 @@ func defIOReadExtra(cls *RClass) { o.wrClosed, o.closed = true, true o.pipeEndClosed() o.unpin() + o.releaseLockFd() // close(2) drops any flock this stream still holds (#979) return object.NilV } ioFlush(o) @@ -256,6 +259,7 @@ func defIOReadExtra(cls *RClass) { if o.rdClosed { o.closed = true o.unpin() + o.releaseLockFd() } return object.NilV })