Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions internal/vm/io_close_half_flock_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
}
4 changes: 4 additions & 0 deletions internal/vm/io_descriptors.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
})
Expand All @@ -249,13 +251,15 @@ 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)
o.wrClosed = true
if o.rdClosed {
o.closed = true
o.unpin()
o.releaseLockFd()
}
return object.NilV
})
Expand Down
Loading