From 045d1270a7c299c6e40cccf3776860749abfe18e Mon Sep 17 00:00:00 2001 From: qmuntal Date: Fri, 30 Jan 2026 14:26:57 +0100 Subject: [PATCH] internal/poll: unlock read lock if write lock fails in readWriteLock If the write lock acquisition fails, the read lock acquired earlier is not released, leading to a potential deadlock. The deadlock shouldn't occur because when the write lock fails, it indicates that the FD is closing, and no other goroutine should be holding the read lock. However, better to be safe and release the read lock in such cases. Change-Id: If593c36040a97357f835b42bb3133ff1dc55a638 Reviewed-on: https://go-review.googlesource.com/c/go/+/740560 Reviewed-by: Damien Neil LUCI-TryBot-Result: Go LUCI Reviewed-by: Roland Shoemaker --- src/internal/poll/fd_mutex.go | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/internal/poll/fd_mutex.go b/src/internal/poll/fd_mutex.go index f71c7739a1..aa8f425235 100644 --- a/src/internal/poll/fd_mutex.go +++ b/src/internal/poll/fd_mutex.go @@ -254,7 +254,11 @@ func (fd *FD) writeUnlock() { // readWriteLock adds a reference to fd and locks fd for reading and writing. // It returns an error when fd cannot be used for reading and writing. func (fd *FD) readWriteLock() error { - if !fd.fdmu.rwlock(true) || !fd.fdmu.rwlock(false) { + if !fd.fdmu.rwlock(true) { + return errClosing(fd.isFile) + } + if !fd.fdmu.rwlock(false) { + fd.fdmu.rwunlock(true) // unlock read lock acquired above return errClosing(fd.isFile) } return nil -- 2.52.0