Skip to content

Commit

Permalink
Cleanup Socket and Pidfile on exit
Browse files Browse the repository at this point in the history
These were previously left intact, even when exiting gracefully. As the
daemon also fails if the socket already exists, it became the caller's
responsibilityto check for and cleanup old socket files when performing
graceful / deliberate restarts.

Signed-off-by: Emily Shepherd <[email protected]>
  • Loading branch information
EmilyShepherd committed Sep 6, 2022
1 parent 8c3664b commit f89a005
Showing 1 changed file with 20 additions and 1 deletion.
21 changes: 20 additions & 1 deletion plugins/ipam/dhcp/daemon.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package main

import (
"context"
"encoding/json"
"errors"
"fmt"
Expand All @@ -23,9 +24,11 @@ import (
"net/http"
"net/rpc"
"os"
"os/signal"
"path/filepath"
"runtime"
"sync"
"syscall"
"time"

"github.com/containernetworking/cni/pkg/skel"
Expand Down Expand Up @@ -195,11 +198,27 @@ func runDaemon(
return fmt.Errorf("Error getting listener: %v", err)
}

srv := http.Server{}
exit := make(chan os.Signal, 1)
done := make(chan bool, 1)
signal.Notify(exit, os.Interrupt, syscall.SIGTERM)

go func() {
<-exit
srv.Shutdown(context.TODO())
os.Remove(hostPrefix + socketPath)
os.Remove(pidfilePath)

done <- true
}()

dhcp := newDHCP(dhcpClientTimeout, resendMax)
dhcp.hostNetnsPrefix = hostPrefix
dhcp.broadcast = broadcast
rpc.Register(dhcp)
rpc.HandleHTTP()
http.Serve(l, nil)
srv.Serve(l)

<-done
return nil
}

0 comments on commit f89a005

Please sign in to comment.