@@ -13,6 +13,12 @@ import (
13
13
)
14
14
15
15
const (
16
+ // spliceNonblock doesn't make the splice itself necessarily nonblocking
17
+ // (because the actual file descriptors that are spliced from/to may block
18
+ // unless they have the O_NONBLOCK flag set), but it makes the splice pipe
19
+ // operations nonblocking.
20
+ spliceNonblock = 0x2
21
+
16
22
// maxSpliceSize is the maximum amount of data Splice asks
17
23
// the kernel to move in a single call to splice(2).
18
24
// We use 1MB as Splice writes data through a pipe, and 1MB is the default maximum pipe buffer size,
@@ -89,7 +95,11 @@ func spliceDrain(pipefd int, sock *FD, max int) (int, error) {
89
95
return 0 , err
90
96
}
91
97
for {
92
- n , err := splice (pipefd , sock .Sysfd , max , 0 )
98
+ // In theory calling splice(2) with SPLICE_F_NONBLOCK could end up an infinite loop here,
99
+ // because it could return EAGAIN ceaselessly when the write end of the pipe is full,
100
+ // but this shouldn't be a concern here, since the pipe buffer must be sufficient for
101
+ // this data transmission on the basis of the workflow in Splice.
102
+ n , err := splice (pipefd , sock .Sysfd , max , spliceNonblock )
93
103
if err == syscall .EINTR {
94
104
continue
95
105
}
@@ -127,7 +137,14 @@ func splicePump(sock *FD, pipefd int, inPipe int) (int, error) {
127
137
}
128
138
written := 0
129
139
for inPipe > 0 {
130
- n , err := splice (sock .Sysfd , pipefd , inPipe , 0 )
140
+ // In theory calling splice(2) with SPLICE_F_NONBLOCK could end up an infinite loop here,
141
+ // because it could return EAGAIN ceaselessly when the read end of the pipe is empty,
142
+ // but this shouldn't be a concern here, since the pipe buffer must contain inPipe size of
143
+ // data on the basis of the workflow in Splice.
144
+ n , err := splice (sock .Sysfd , pipefd , inPipe , spliceNonblock )
145
+ if err == syscall .EINTR {
146
+ continue
147
+ }
131
148
// Here, the condition n == 0 && err == nil should never be
132
149
// observed, since Splice controls the write side of the pipe.
133
150
if n > 0 {
0 commit comments