Skip to content

Commit e973d5b

Browse files
committed
intra: do not close the read fd of crash output
1 parent 9f0639b commit e973d5b

2 files changed

Lines changed: 9 additions & 6 deletions

File tree

intra/settings/config.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,4 +75,5 @@ const IntraUa = "Intra"
7575
var PanicAtRandom = atomic.Bool{}
7676

7777
// OwnTunFd is a global flag to indicate that the TUN fd is fully owned by netstack.
78+
// that is, he TUN FD won't be dup'd and will be closed after use.
7879
var OwnTunFd = atomic.Bool{}

intra/tun2socks.go

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -311,15 +311,17 @@ func Crash(afterMs int64) {
311311
}()
312312
}
313313

314+
// global references to keep go's finalizer from cleaning up the FDs
315+
var crashReader, crashWriter, crashRWErr = os.Pipe()
316+
314317
func pipeCrashOutput(c Console) (ok bool) {
315-
r, w, err := os.Pipe()
316-
if err != nil {
317-
log.E("tun: err crash output pipe: %v", err)
318+
if crashRWErr != nil {
319+
log.E("tun: err crash output pipe: %v", crashRWErr)
318320
return false
319321
}
320-
defer core.Close(r) // r isn't dup'd by client code
321-
defer core.Close(w) // always close as w is dup'd by the runtime
322-
if setCrashFd(w) && c.CrashFD(int(r.Fd())) {
322+
// defer core.Close(crashReader) // close iff r is dup'd by client code
323+
defer core.Close(crashWriter) // always close as w is dup'd by the runtime
324+
if setCrashFd(crashWriter) && c.CrashFD(int(crashReader.Fd())) {
323325
return true
324326
}
325327
setCrashFd(nil)

0 commit comments

Comments
 (0)