Skip to content

Commit 5829c74

Browse files
unix: change Dup,Dup2,Dup3 to use Syscall, not RawSyscall
This avoids hanging when a Go program uses a FUSE filesystem and the dup system call has to close a file descriptor. When dup uses RawSyscall then the goroutine calling dup will occupy a scheduler slot (a p structure) during the call, and may block waiting for some other goroutine to respond to the close call on the FUSE filesystem. Changing to Syscall avoids the problem. This makes Dup a tiny bit slower but is quite unlikely to make a difference for any real programs. Update golang/go#10202. Change-Id: I590c5c9a04e0a1281a85dc553c7592fa83949ac7 Reviewed-on: https://go-review.googlesource.com/8056 Reviewed-by: Rob Pike <[email protected]>
1 parent 2cb975b commit 5829c74

21 files changed

+126
-48
lines changed

unix/syscall_darwin.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -222,8 +222,8 @@ func Kill(pid int, signum syscall.Signal) (err error) { return kill(pid, int(sig
222222
//sys Chown(path string, uid int, gid int) (err error)
223223
//sys Chroot(path string) (err error)
224224
//sys Close(fd int) (err error)
225-
//sysnb Dup(fd int) (nfd int, err error)
226-
//sysnb Dup2(from int, to int) (err error)
225+
//sys Dup(fd int) (nfd int, err error)
226+
//sys Dup2(from int, to int) (err error)
227227
//sys Exchangedata(path1 string, path2 string, options int) (err error)
228228
//sys Exit(code int)
229229
//sys Fchdir(fd int) (err error)

unix/syscall_dragonfly.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -127,8 +127,8 @@ func Getfsstat(buf []Statfs_t, flags int) (n int, err error) {
127127
//sys Chown(path string, uid int, gid int) (err error)
128128
//sys Chroot(path string) (err error)
129129
//sys Close(fd int) (err error)
130-
//sysnb Dup(fd int) (nfd int, err error)
131-
//sysnb Dup2(from int, to int) (err error)
130+
//sys Dup(fd int) (nfd int, err error)
131+
//sys Dup2(from int, to int) (err error)
132132
//sys Exit(code int)
133133
//sys Fchdir(fd int) (err error)
134134
//sys Fchflags(fd int, flags int) (err error)

unix/syscall_freebsd.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -379,8 +379,8 @@ func Llistxattr(link string, dest []byte) (sz int, err error) {
379379
//sys Chown(path string, uid int, gid int) (err error)
380380
//sys Chroot(path string) (err error)
381381
//sys Close(fd int) (err error)
382-
//sysnb Dup(fd int) (nfd int, err error)
383-
//sysnb Dup2(from int, to int) (err error)
382+
//sys Dup(fd int) (nfd int, err error)
383+
//sys Dup2(from int, to int) (err error)
384384
//sys Exit(code int)
385385
//sys ExtattrGetFd(fd int, attrnamespace int, attrname string, data uintptr, nbytes int) (ret int, err error)
386386
//sys ExtattrSetFd(fd int, attrnamespace int, attrname string, data uintptr, nbytes int) (ret int, err error)

unix/syscall_linux.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -790,9 +790,9 @@ func Mount(source string, target string, fstype string, flags uintptr, data stri
790790
//sys Chroot(path string) (err error)
791791
//sys Close(fd int) (err error)
792792
//sys Creat(path string, mode uint32) (fd int, err error)
793-
//sysnb Dup(oldfd int) (fd int, err error)
794-
//sysnb Dup2(oldfd int, newfd int) (err error)
795-
//sysnb Dup3(oldfd int, newfd int, flags int) (err error)
793+
//sys Dup(oldfd int) (fd int, err error)
794+
//sys Dup2(oldfd int, newfd int) (err error)
795+
//sys Dup3(oldfd int, newfd int, flags int) (err error)
796796
//sysnb EpollCreate(size int) (fd int, err error)
797797
//sysnb EpollCreate1(flag int) (fd int, err error)
798798
//sysnb EpollCtl(epfd int, op int, fd int, event *EpollEvent) (err error)

unix/syscall_netbsd.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -151,8 +151,8 @@ func sendfile(outfd int, infd int, offset *int64, count int) (written int, err e
151151
//sys Chown(path string, uid int, gid int) (err error)
152152
//sys Chroot(path string) (err error)
153153
//sys Close(fd int) (err error)
154-
//sysnb Dup(fd int) (nfd int, err error)
155-
//sysnb Dup2(from int, to int) (err error)
154+
//sys Dup(fd int) (nfd int, err error)
155+
//sys Dup2(from int, to int) (err error)
156156
//sys Exit(code int)
157157
//sys Fchdir(fd int) (err error)
158158
//sys Fchflags(fd int, flags int) (err error)

unix/syscall_openbsd.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -129,8 +129,8 @@ func Getfsstat(buf []Statfs_t, flags int) (n int, err error) {
129129
//sys Chown(path string, uid int, gid int) (err error)
130130
//sys Chroot(path string) (err error)
131131
//sys Close(fd int) (err error)
132-
//sysnb Dup(fd int) (nfd int, err error)
133-
//sysnb Dup2(from int, to int) (err error)
132+
//sys Dup(fd int) (nfd int, err error)
133+
//sys Dup2(from int, to int) (err error)
134134
//sys Exit(code int)
135135
//sys Fchdir(fd int) (err error)
136136
//sys Fchflags(fd int, flags int) (err error)

unix/zsyscall_darwin_386.go

+8-3
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,12 @@
33

44
package unix
55

6-
import "unsafe"
6+
import (
7+
"syscall"
8+
"unsafe"
9+
)
10+
11+
var _ syscall.Errno
712

813
// THIS FILE IS GENERATED BY THE COMMAND AT THE TOP; DO NOT EDIT
914

@@ -409,7 +414,7 @@ func Close(fd int) (err error) {
409414
// THIS FILE IS GENERATED BY THE COMMAND AT THE TOP; DO NOT EDIT
410415

411416
func Dup(fd int) (nfd int, err error) {
412-
r0, _, e1 := RawSyscall(SYS_DUP, uintptr(fd), 0, 0)
417+
r0, _, e1 := Syscall(SYS_DUP, uintptr(fd), 0, 0)
413418
nfd = int(r0)
414419
if e1 != 0 {
415420
err = e1
@@ -420,7 +425,7 @@ func Dup(fd int) (nfd int, err error) {
420425
// THIS FILE IS GENERATED BY THE COMMAND AT THE TOP; DO NOT EDIT
421426

422427
func Dup2(from int, to int) (err error) {
423-
_, _, e1 := RawSyscall(SYS_DUP2, uintptr(from), uintptr(to), 0)
428+
_, _, e1 := Syscall(SYS_DUP2, uintptr(from), uintptr(to), 0)
424429
if e1 != 0 {
425430
err = e1
426431
}

unix/zsyscall_darwin_amd64.go

+8-3
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,12 @@
33

44
package unix
55

6-
import "unsafe"
6+
import (
7+
"syscall"
8+
"unsafe"
9+
)
10+
11+
var _ syscall.Errno
712

813
// THIS FILE IS GENERATED BY THE COMMAND AT THE TOP; DO NOT EDIT
914

@@ -409,7 +414,7 @@ func Close(fd int) (err error) {
409414
// THIS FILE IS GENERATED BY THE COMMAND AT THE TOP; DO NOT EDIT
410415

411416
func Dup(fd int) (nfd int, err error) {
412-
r0, _, e1 := RawSyscall(SYS_DUP, uintptr(fd), 0, 0)
417+
r0, _, e1 := Syscall(SYS_DUP, uintptr(fd), 0, 0)
413418
nfd = int(r0)
414419
if e1 != 0 {
415420
err = e1
@@ -420,7 +425,7 @@ func Dup(fd int) (nfd int, err error) {
420425
// THIS FILE IS GENERATED BY THE COMMAND AT THE TOP; DO NOT EDIT
421426

422427
func Dup2(from int, to int) (err error) {
423-
_, _, e1 := RawSyscall(SYS_DUP2, uintptr(from), uintptr(to), 0)
428+
_, _, e1 := Syscall(SYS_DUP2, uintptr(from), uintptr(to), 0)
424429
if e1 != 0 {
425430
err = e1
426431
}

unix/zsyscall_dragonfly_386.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -428,7 +428,7 @@ func Close(fd int) (err error) {
428428
// THIS FILE IS GENERATED BY THE COMMAND AT THE TOP; DO NOT EDIT
429429

430430
func Dup(fd int) (nfd int, err error) {
431-
r0, _, e1 := RawSyscall(SYS_DUP, uintptr(fd), 0, 0)
431+
r0, _, e1 := Syscall(SYS_DUP, uintptr(fd), 0, 0)
432432
nfd = int(r0)
433433
if e1 != 0 {
434434
err = e1
@@ -439,7 +439,7 @@ func Dup(fd int) (nfd int, err error) {
439439
// THIS FILE IS GENERATED BY THE COMMAND AT THE TOP; DO NOT EDIT
440440

441441
func Dup2(from int, to int) (err error) {
442-
_, _, e1 := RawSyscall(SYS_DUP2, uintptr(from), uintptr(to), 0)
442+
_, _, e1 := Syscall(SYS_DUP2, uintptr(from), uintptr(to), 0)
443443
if e1 != 0 {
444444
err = e1
445445
}

unix/zsyscall_dragonfly_amd64.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -428,7 +428,7 @@ func Close(fd int) (err error) {
428428
// THIS FILE IS GENERATED BY THE COMMAND AT THE TOP; DO NOT EDIT
429429

430430
func Dup(fd int) (nfd int, err error) {
431-
r0, _, e1 := RawSyscall(SYS_DUP, uintptr(fd), 0, 0)
431+
r0, _, e1 := Syscall(SYS_DUP, uintptr(fd), 0, 0)
432432
nfd = int(r0)
433433
if e1 != 0 {
434434
err = e1
@@ -439,7 +439,7 @@ func Dup(fd int) (nfd int, err error) {
439439
// THIS FILE IS GENERATED BY THE COMMAND AT THE TOP; DO NOT EDIT
440440

441441
func Dup2(from int, to int) (err error) {
442-
_, _, e1 := RawSyscall(SYS_DUP2, uintptr(from), uintptr(to), 0)
442+
_, _, e1 := Syscall(SYS_DUP2, uintptr(from), uintptr(to), 0)
443443
if e1 != 0 {
444444
err = e1
445445
}

unix/zsyscall_freebsd_386.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -394,7 +394,7 @@ func Close(fd int) (err error) {
394394
// THIS FILE IS GENERATED BY THE COMMAND AT THE TOP; DO NOT EDIT
395395

396396
func Dup(fd int) (nfd int, err error) {
397-
r0, _, e1 := RawSyscall(SYS_DUP, uintptr(fd), 0, 0)
397+
r0, _, e1 := Syscall(SYS_DUP, uintptr(fd), 0, 0)
398398
nfd = int(r0)
399399
if e1 != 0 {
400400
err = e1
@@ -405,7 +405,7 @@ func Dup(fd int) (nfd int, err error) {
405405
// THIS FILE IS GENERATED BY THE COMMAND AT THE TOP; DO NOT EDIT
406406

407407
func Dup2(from int, to int) (err error) {
408-
_, _, e1 := RawSyscall(SYS_DUP2, uintptr(from), uintptr(to), 0)
408+
_, _, e1 := Syscall(SYS_DUP2, uintptr(from), uintptr(to), 0)
409409
if e1 != 0 {
410410
err = e1
411411
}

unix/zsyscall_freebsd_amd64.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -394,7 +394,7 @@ func Close(fd int) (err error) {
394394
// THIS FILE IS GENERATED BY THE COMMAND AT THE TOP; DO NOT EDIT
395395

396396
func Dup(fd int) (nfd int, err error) {
397-
r0, _, e1 := RawSyscall(SYS_DUP, uintptr(fd), 0, 0)
397+
r0, _, e1 := Syscall(SYS_DUP, uintptr(fd), 0, 0)
398398
nfd = int(r0)
399399
if e1 != 0 {
400400
err = e1
@@ -405,7 +405,7 @@ func Dup(fd int) (nfd int, err error) {
405405
// THIS FILE IS GENERATED BY THE COMMAND AT THE TOP; DO NOT EDIT
406406

407407
func Dup2(from int, to int) (err error) {
408-
_, _, e1 := RawSyscall(SYS_DUP2, uintptr(from), uintptr(to), 0)
408+
_, _, e1 := Syscall(SYS_DUP2, uintptr(from), uintptr(to), 0)
409409
if e1 != 0 {
410410
err = e1
411411
}

unix/zsyscall_freebsd_arm.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -394,7 +394,7 @@ func Close(fd int) (err error) {
394394
// THIS FILE IS GENERATED BY THE COMMAND AT THE TOP; DO NOT EDIT
395395

396396
func Dup(fd int) (nfd int, err error) {
397-
r0, _, e1 := RawSyscall(SYS_DUP, uintptr(fd), 0, 0)
397+
r0, _, e1 := Syscall(SYS_DUP, uintptr(fd), 0, 0)
398398
nfd = int(r0)
399399
if e1 != 0 {
400400
err = e1
@@ -405,7 +405,7 @@ func Dup(fd int) (nfd int, err error) {
405405
// THIS FILE IS GENERATED BY THE COMMAND AT THE TOP; DO NOT EDIT
406406

407407
func Dup2(from int, to int) (err error) {
408-
_, _, e1 := RawSyscall(SYS_DUP2, uintptr(from), uintptr(to), 0)
408+
_, _, e1 := Syscall(SYS_DUP2, uintptr(from), uintptr(to), 0)
409409
if e1 != 0 {
410410
err = e1
411411
}

unix/zsyscall_linux_386.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -309,7 +309,7 @@ func Creat(path string, mode uint32) (fd int, err error) {
309309
// THIS FILE IS GENERATED BY THE COMMAND AT THE TOP; DO NOT EDIT
310310

311311
func Dup(oldfd int) (fd int, err error) {
312-
r0, _, e1 := RawSyscall(SYS_DUP, uintptr(oldfd), 0, 0)
312+
r0, _, e1 := Syscall(SYS_DUP, uintptr(oldfd), 0, 0)
313313
fd = int(r0)
314314
if e1 != 0 {
315315
err = e1
@@ -320,7 +320,7 @@ func Dup(oldfd int) (fd int, err error) {
320320
// THIS FILE IS GENERATED BY THE COMMAND AT THE TOP; DO NOT EDIT
321321

322322
func Dup2(oldfd int, newfd int) (err error) {
323-
_, _, e1 := RawSyscall(SYS_DUP2, uintptr(oldfd), uintptr(newfd), 0)
323+
_, _, e1 := Syscall(SYS_DUP2, uintptr(oldfd), uintptr(newfd), 0)
324324
if e1 != 0 {
325325
err = e1
326326
}
@@ -330,7 +330,7 @@ func Dup2(oldfd int, newfd int) (err error) {
330330
// THIS FILE IS GENERATED BY THE COMMAND AT THE TOP; DO NOT EDIT
331331

332332
func Dup3(oldfd int, newfd int, flags int) (err error) {
333-
_, _, e1 := RawSyscall(SYS_DUP3, uintptr(oldfd), uintptr(newfd), uintptr(flags))
333+
_, _, e1 := Syscall(SYS_DUP3, uintptr(oldfd), uintptr(newfd), uintptr(flags))
334334
if e1 != 0 {
335335
err = e1
336336
}

unix/zsyscall_linux_amd64.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -309,7 +309,7 @@ func Creat(path string, mode uint32) (fd int, err error) {
309309
// THIS FILE IS GENERATED BY THE COMMAND AT THE TOP; DO NOT EDIT
310310

311311
func Dup(oldfd int) (fd int, err error) {
312-
r0, _, e1 := RawSyscall(SYS_DUP, uintptr(oldfd), 0, 0)
312+
r0, _, e1 := Syscall(SYS_DUP, uintptr(oldfd), 0, 0)
313313
fd = int(r0)
314314
if e1 != 0 {
315315
err = e1
@@ -320,7 +320,7 @@ func Dup(oldfd int) (fd int, err error) {
320320
// THIS FILE IS GENERATED BY THE COMMAND AT THE TOP; DO NOT EDIT
321321

322322
func Dup2(oldfd int, newfd int) (err error) {
323-
_, _, e1 := RawSyscall(SYS_DUP2, uintptr(oldfd), uintptr(newfd), 0)
323+
_, _, e1 := Syscall(SYS_DUP2, uintptr(oldfd), uintptr(newfd), 0)
324324
if e1 != 0 {
325325
err = e1
326326
}
@@ -330,7 +330,7 @@ func Dup2(oldfd int, newfd int) (err error) {
330330
// THIS FILE IS GENERATED BY THE COMMAND AT THE TOP; DO NOT EDIT
331331

332332
func Dup3(oldfd int, newfd int, flags int) (err error) {
333-
_, _, e1 := RawSyscall(SYS_DUP3, uintptr(oldfd), uintptr(newfd), uintptr(flags))
333+
_, _, e1 := Syscall(SYS_DUP3, uintptr(oldfd), uintptr(newfd), uintptr(flags))
334334
if e1 != 0 {
335335
err = e1
336336
}

unix/zsyscall_linux_arm.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -309,7 +309,7 @@ func Creat(path string, mode uint32) (fd int, err error) {
309309
// THIS FILE IS GENERATED BY THE COMMAND AT THE TOP; DO NOT EDIT
310310

311311
func Dup(oldfd int) (fd int, err error) {
312-
r0, _, e1 := RawSyscall(SYS_DUP, uintptr(oldfd), 0, 0)
312+
r0, _, e1 := Syscall(SYS_DUP, uintptr(oldfd), 0, 0)
313313
fd = int(r0)
314314
if e1 != 0 {
315315
err = e1
@@ -320,7 +320,7 @@ func Dup(oldfd int) (fd int, err error) {
320320
// THIS FILE IS GENERATED BY THE COMMAND AT THE TOP; DO NOT EDIT
321321

322322
func Dup2(oldfd int, newfd int) (err error) {
323-
_, _, e1 := RawSyscall(SYS_DUP2, uintptr(oldfd), uintptr(newfd), 0)
323+
_, _, e1 := Syscall(SYS_DUP2, uintptr(oldfd), uintptr(newfd), 0)
324324
if e1 != 0 {
325325
err = e1
326326
}
@@ -330,7 +330,7 @@ func Dup2(oldfd int, newfd int) (err error) {
330330
// THIS FILE IS GENERATED BY THE COMMAND AT THE TOP; DO NOT EDIT
331331

332332
func Dup3(oldfd int, newfd int, flags int) (err error) {
333-
_, _, e1 := RawSyscall(SYS_DUP3, uintptr(oldfd), uintptr(newfd), uintptr(flags))
333+
_, _, e1 := Syscall(SYS_DUP3, uintptr(oldfd), uintptr(newfd), uintptr(flags))
334334
if e1 != 0 {
335335
err = e1
336336
}

unix/zsyscall_netbsd_386.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -411,7 +411,7 @@ func Close(fd int) (err error) {
411411
// THIS FILE IS GENERATED BY THE COMMAND AT THE TOP; DO NOT EDIT
412412

413413
func Dup(fd int) (nfd int, err error) {
414-
r0, _, e1 := RawSyscall(SYS_DUP, uintptr(fd), 0, 0)
414+
r0, _, e1 := Syscall(SYS_DUP, uintptr(fd), 0, 0)
415415
nfd = int(r0)
416416
if e1 != 0 {
417417
err = e1
@@ -422,7 +422,7 @@ func Dup(fd int) (nfd int, err error) {
422422
// THIS FILE IS GENERATED BY THE COMMAND AT THE TOP; DO NOT EDIT
423423

424424
func Dup2(from int, to int) (err error) {
425-
_, _, e1 := RawSyscall(SYS_DUP2, uintptr(from), uintptr(to), 0)
425+
_, _, e1 := Syscall(SYS_DUP2, uintptr(from), uintptr(to), 0)
426426
if e1 != 0 {
427427
err = e1
428428
}

unix/zsyscall_netbsd_amd64.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -411,7 +411,7 @@ func Close(fd int) (err error) {
411411
// THIS FILE IS GENERATED BY THE COMMAND AT THE TOP; DO NOT EDIT
412412

413413
func Dup(fd int) (nfd int, err error) {
414-
r0, _, e1 := RawSyscall(SYS_DUP, uintptr(fd), 0, 0)
414+
r0, _, e1 := Syscall(SYS_DUP, uintptr(fd), 0, 0)
415415
nfd = int(r0)
416416
if e1 != 0 {
417417
err = e1
@@ -422,7 +422,7 @@ func Dup(fd int) (nfd int, err error) {
422422
// THIS FILE IS GENERATED BY THE COMMAND AT THE TOP; DO NOT EDIT
423423

424424
func Dup2(from int, to int) (err error) {
425-
_, _, e1 := RawSyscall(SYS_DUP2, uintptr(from), uintptr(to), 0)
425+
_, _, e1 := Syscall(SYS_DUP2, uintptr(from), uintptr(to), 0)
426426
if e1 != 0 {
427427
err = e1
428428
}

0 commit comments

Comments
 (0)