Skip to content

Commit

Permalink
removeListener: resolve data races regarding listener access
Browse files Browse the repository at this point in the history
Previously the listener was created by the serveNicks() goroutine in the
background. The goroutine might not have been started before
removeListener() is called.

This also means that the `dir.ch.ln != nil` check in removeListener is
no longer required because if the listener was created it is guaranteed
that the listener exists.

This issue was found using the go race detector.
  • Loading branch information
nmeum committed Jan 6, 2019
1 parent 7a4656b commit acc4296
Showing 1 changed file with 9 additions and 9 deletions.
18 changes: 9 additions & 9 deletions hii.go
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,14 @@ func createListener(client *girc.Client, name string) (*ircDir, error) {
go recvInput(client, name, idir)
if girc.IsValidChannel(name) {
idir.ch = &ircChan{make(chan bool, 1), nil}

nickfp := filepath.Join(dir, nickfn)
idir.ch.ln, err = net.Listen("unix", nickfp)
if err != nil {
os.Remove(infp)
return nil, err
}

go serveNicks(client, name, idir)
} else if girc.IsValidNick(name) {
client.Cmd.Monitor('+', name)
Expand Down Expand Up @@ -368,7 +376,7 @@ func removeListener(name string) error {
fifo.Close()

ch := dir.ch
if ch != nil && ch.ln != nil {
if ch != nil {
ch.done <- true
ch.ln.Close()
}
Expand Down Expand Up @@ -452,14 +460,6 @@ func recvInput(client *girc.Client, name string, dir *ircDir) {
}

func serveNicks(client *girc.Client, name string, dir *ircDir) {
nickfp := filepath.Join(dir.fp, nickfn)

var err error
dir.ch.ln, err = net.Listen("unix", nickfp)
if err != nil {
die(client, err)
}

for {
conn, err := dir.ch.ln.Accept()
select {
Expand Down

0 comments on commit acc4296

Please sign in to comment.