Skip to content

Commit

Permalink
Better shutdown
Browse files Browse the repository at this point in the history
Signed-off-by: Jimmy Moore <[email protected]>
  • Loading branch information
jimmyaxod committed Dec 17, 2024
1 parent 65f19cd commit d06050a
Showing 1 changed file with 19 additions and 13 deletions.
32 changes: 19 additions & 13 deletions pkg/storage/expose/nbd.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ type ExposedStorageNBDNL struct {
provLock sync.RWMutex
deviceIndex int
dispatchers []*Dispatch
handlersWg sync.WaitGroup
}

func NewExposedStorageNBDNL(prov storage.Provider, conf *Config) *ExposedStorageNBDNL {
Expand Down Expand Up @@ -210,6 +211,7 @@ func (n *ExposedStorageNBDNL) Init() error {
d.asyncReads = n.config.AsyncReads
d.asyncWrites = n.config.AsyncWrites
// Start reading commands on the socket and dispatching them to our provider
n.handlersWg.Add(1)
go func() {
err := d.Handle()
if n.config.Logger != nil {
Expand All @@ -218,6 +220,7 @@ func (n *ExposedStorageNBDNL) Init() error {
Err(err).
Msg("nbd dispatch completed")
}
n.handlersWg.Done()
}()
n.socks = append(n.socks, serverc)
socks = append(socks, client)
Expand Down Expand Up @@ -293,20 +296,29 @@ func (n *ExposedStorageNBDNL) Shutdown() error {
// First cancel the context, which will stop waiting on pending readAt/writeAt...
n.cancelfn()

// Now ask to disconnect
err := nbdnl.Disconnect(uint32(n.deviceIndex))
if err != nil {
return err
}

// Close all the socket pairs...
for _, v := range n.socks {
err = v.Close()
err := v.Close()
if err != nil {
return err
}
}

// Now wait until the handlers return
n.handlersWg.Wait()

// Now wait for any pending responses to be sent. There should not be any new
// Requests received since we have Disconnected.
for _, d := range n.dispatchers {
d.Wait()
}

// Now ask to disconnect
err := nbdnl.Disconnect(uint32(n.deviceIndex))
if err != nil {
return err
}

// Wait until it's completely disconnected...
for {
s, err := nbdnl.Status(uint32(n.deviceIndex))
Expand All @@ -316,12 +328,6 @@ func (n *ExposedStorageNBDNL) Shutdown() error {
time.Sleep(100 * time.Nanosecond)
}

// Now wait for any pending responses to be sent. There should not be any new
// Requests received since we have Disconnected.
for _, d := range n.dispatchers {
d.Wait()
}

if n.config.Logger != nil {
n.config.Logger.Trace().
Str("uuid", n.uuid.String()).
Expand Down

0 comments on commit d06050a

Please sign in to comment.