Skip to content

Commit

Permalink
cleanup: close connection before performing cleanup
Browse files Browse the repository at this point in the history
To ensure that new listeners / files aren't created after / during the
cleanup (e.g. through RPL_MONONLINE messages).

Discussion: Sending a QUIT command and blocking until the client quit
would be preferable, unfortunately such a command is not available.

See: lrstanley/girc#16
  • Loading branch information
nmeum committed Dec 24, 2018
1 parent c7a4022 commit ddba26e
Showing 1 changed file with 8 additions and 7 deletions.
15 changes: 8 additions & 7 deletions hii.go
Expand Up @@ -94,7 +94,8 @@ func usage() {
os.Exit(2)
}

func cleanup() {
func cleanup(client *girc.Client) {
client.Close()
for _, dir := range ircDirs {
err := removeListener(dir.name)
if err != nil {
Expand All @@ -103,8 +104,8 @@ func cleanup() {
}
}

func die(err error) {
cleanup()
func die(client *girc.Client, err error) {
cleanup(client)
log.Fatal(err)
}

Expand Down Expand Up @@ -460,7 +461,7 @@ func serveNicks(client *girc.Client, name string, dir *ircDir) {
var err error
dir.ch.ln, err = net.Listen("unix", nickfp)
if err != nil {
die(err)
die(client, err)
}

for {
Expand Down Expand Up @@ -622,7 +623,7 @@ func handleMsg(client *girc.Client, event girc.Event) {

names, err := getEventDirs(client, &event)
if err != nil {
die(err)
die(client, err)
}

for _, name := range names {
Expand Down Expand Up @@ -718,12 +719,12 @@ func main() {
signal.Notify(sig, syscall.SIGINT, syscall.SIGTERM, syscall.SIGQUIT, syscall.SIGHUP)
go func() {
<-sig
cleanup()
cleanup(client)
os.Exit(1)
}()

err = client.Connect()
cleanup()
cleanup(client)
if err != nil {
log.Fatal(err)
}
Expand Down

0 comments on commit ddba26e

Please sign in to comment.