Skip to content

Commit

Permalink
Attempt to fix sporadic ctrl-c bug
Browse files Browse the repository at this point in the history
Issue #120 reports that if you hit ctrl-c shortly after startup,
occasionally termshark will not quit correctly, and subsequent ctrl-c
invocations make no difference.

This occurs only if reading from an interface. In that case, the
PSML-reading process starts with stdin set to the read-end of a
pipe. That pipe is supplied with pcap data via a tail command, reading
from the gradually accumulating pcap on disk.

The goroutine that tracks changes to PSML process state needs to ensure
that if the controlling context is cancelled, this pipe is
closed. Failing to do this can lead to Go's cmd.Exec never responding to
Wait() because internally, the Wait() calls itself waits for a private
goroutine - that goroutine is running io.Copy() from the user's process
IO reader (our pipe) to the real process's stdin. Closing the pipe will
cause io.Copy() to terminate, then cmd.Exec's private goroutine to
terminate, and so allows Wait() to return a value.
  • Loading branch information
gcla committed Apr 18, 2021
1 parent 88f0374 commit 2461481
Showing 1 changed file with 14 additions and 2 deletions.
16 changes: 14 additions & 2 deletions pcap/loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -1359,6 +1359,15 @@ func (p *PsmlLoader) loadPsmlSync(iloader *InterfaceLoader, e iPsmlLoaderEnv, cb

//======================================================================

closedPipe := false
closePipe := func() {
if !closedPipe {
fifoPipeWriter.Close()
fifoPipeReader.Close()
closedPipe = true
}
}

if p.ReadingFromFifo() {
// PcapPsml will be nil if here

Expand All @@ -1378,8 +1387,7 @@ func (p *PsmlLoader) loadPsmlSync(iloader *InterfaceLoader, e iPsmlLoaderEnv, cb
// is used as stdin for the psml command, which also runs in this
// goroutine.
defer func() {
fifoPipeWriter.Close()
fifoPipeReader.Close()
closePipe()
}()

// wrap the read end of the pipe with a Read() function that counts
Expand Down Expand Up @@ -1425,6 +1433,10 @@ func (p *PsmlLoader) loadPsmlSync(iloader *InterfaceLoader, e iPsmlLoaderEnv, cb
if err != nil {
log.Infof("Did not kill tshark psml process: %v", err)
}

if p.ReadingFromFifo() {
closePipe()
}
}

loop:
Expand Down

0 comments on commit 2461481

Please sign in to comment.