Skip to content

Commit 24fb2e0

Browse files
acln0ianlancetaylor
authored andcommitted
internal/poll: use more fine-grained locking in Splice
The previous code acquired a read lock on src and a write lock on dst for the entire duration of Splice. This resulted in deadlock, in a situation akin to the following: Splice is blocking, waiting to read from src. The caller tries to close dst from another goroutine, but Close on dst blocks in runtime.semacquire, because Splice is still holding a write lock on it, and the Close didn't unblock any I/O. The caller cannot unblock the read side of Splice through other means, because they are stuck waiting in dst.Close(). Use more fine-grained locking instead: acquire the read lock on src just before trying to splice from the source socket to the pipe, and acquire the write lock on dst just before trying to splice from the pipe to the destination socket. Fixes #25985 Change-Id: I264c91c7a69bb6c5e28610e2bd801244804cf86d Reviewed-on: https://go-review.googlesource.com/120317 Run-TryBot: Aram Hăvărneanu <[email protected]> TryBot-Result: Gobot Gobot <[email protected]> Reviewed-by: Ian Lance Taylor <[email protected]>
1 parent 1507502 commit 24fb2e0

File tree

2 files changed

+77
-14
lines changed

2 files changed

+77
-14
lines changed

src/internal/poll/splice_linux.go

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -34,20 +34,6 @@ func Splice(dst, src *FD, remain int64) (written int64, handled bool, sc string,
3434
defer destroyTempPipe(prfd, pwfd)
3535
// From here on, the operation should be considered handled,
3636
// even if Splice doesn't transfer any data.
37-
if err := src.readLock(); err != nil {
38-
return 0, true, "splice", err
39-
}
40-
defer src.readUnlock()
41-
if err := dst.writeLock(); err != nil {
42-
return 0, true, "splice", err
43-
}
44-
defer dst.writeUnlock()
45-
if err := src.pd.prepareRead(src.isFile); err != nil {
46-
return 0, true, "splice", err
47-
}
48-
if err := dst.pd.prepareWrite(dst.isFile); err != nil {
49-
return 0, true, "splice", err
50-
}
5137
var inPipe, n int
5238
for err == nil && remain > 0 {
5339
max := maxSpliceSize
@@ -84,6 +70,13 @@ func Splice(dst, src *FD, remain int64) (written int64, handled bool, sc string,
8470
//
8571
// If spliceDrain returns (0, nil), src is at EOF.
8672
func spliceDrain(pipefd int, sock *FD, max int) (int, error) {
73+
if err := sock.readLock(); err != nil {
74+
return 0, err
75+
}
76+
defer sock.readUnlock()
77+
if err := sock.pd.prepareRead(sock.isFile); err != nil {
78+
return 0, err
79+
}
8780
for {
8881
n, err := splice(pipefd, sock.Sysfd, max, spliceNonblock)
8982
if err != syscall.EAGAIN {
@@ -109,6 +102,13 @@ func spliceDrain(pipefd int, sock *FD, max int) (int, error) {
109102
// all of it to the socket. This behavior is similar to the Write
110103
// step of an io.Copy in userspace.
111104
func splicePump(sock *FD, pipefd int, inPipe int) (int, error) {
105+
if err := sock.writeLock(); err != nil {
106+
return 0, err
107+
}
108+
defer sock.writeUnlock()
109+
if err := sock.pd.prepareWrite(sock.isFile); err != nil {
110+
return 0, err
111+
}
112112
written := 0
113113
for inPipe > 0 {
114114
n, err := splice(sock.Sysfd, pipefd, inPipe, spliceNonblock)

src/net/splice_test.go

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ import (
1010
"bytes"
1111
"fmt"
1212
"io"
13+
"io/ioutil"
14+
"sync"
1315
"testing"
1416
)
1517

@@ -19,6 +21,7 @@ func TestSplice(t *testing.T) {
1921
t.Run("big", testSpliceBig)
2022
t.Run("honorsLimitedReader", testSpliceHonorsLimitedReader)
2123
t.Run("readerAtEOF", testSpliceReaderAtEOF)
24+
t.Run("issue25985", testSpliceIssue25985)
2225
}
2326

2427
func testSpliceSimple(t *testing.T) {
@@ -234,6 +237,66 @@ func testSpliceReaderAtEOF(t *testing.T) {
234237
}
235238
}
236239

240+
func testSpliceIssue25985(t *testing.T) {
241+
front, err := newLocalListener("tcp")
242+
if err != nil {
243+
t.Fatal(err)
244+
}
245+
defer front.Close()
246+
back, err := newLocalListener("tcp")
247+
if err != nil {
248+
t.Fatal(err)
249+
}
250+
defer back.Close()
251+
252+
var wg sync.WaitGroup
253+
wg.Add(2)
254+
255+
proxy := func() {
256+
src, err := front.Accept()
257+
if err != nil {
258+
return
259+
}
260+
dst, err := Dial("tcp", back.Addr().String())
261+
if err != nil {
262+
return
263+
}
264+
defer dst.Close()
265+
defer src.Close()
266+
go func() {
267+
io.Copy(src, dst)
268+
wg.Done()
269+
}()
270+
go func() {
271+
io.Copy(dst, src)
272+
wg.Done()
273+
}()
274+
}
275+
276+
go proxy()
277+
278+
toFront, err := Dial("tcp", front.Addr().String())
279+
if err != nil {
280+
t.Fatal(err)
281+
}
282+
283+
io.WriteString(toFront, "foo")
284+
toFront.Close()
285+
286+
fromProxy, err := back.Accept()
287+
if err != nil {
288+
t.Fatal(err)
289+
}
290+
defer fromProxy.Close()
291+
292+
_, err = ioutil.ReadAll(fromProxy)
293+
if err != nil {
294+
t.Fatal(err)
295+
}
296+
297+
wg.Wait()
298+
}
299+
237300
func BenchmarkTCPReadFrom(b *testing.B) {
238301
testHookUninstaller.Do(uninstallTestHooks)
239302

0 commit comments

Comments
 (0)