Skip to content

Commit

Permalink
lxc: Handle interupts properly to clean spice socket and avoid accumu…
Browse files Browse the repository at this point in the history
…lation

Signed-off-by: Gabriel Mougard <[email protected]>
  • Loading branch information
gabrielmougard committed Oct 29, 2024
1 parent c1f6a87 commit b721dbc
Showing 1 changed file with 18 additions and 2 deletions.
20 changes: 18 additions & 2 deletions lxc/console.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
package main

import (
"context"
"errors"
"fmt"
"io"
"net"
"os"
"os/exec"
"os/signal"
"runtime"
"strconv"
"sync"
"syscall"

"github.com/gorilla/websocket"
"github.com/spf13/cobra"
Expand Down Expand Up @@ -237,6 +240,12 @@ func (c *cmdConsole) vga(d lxd.InstanceServer, name string) error {
var err error
conf := c.global.conf

// Create a context that is canceled on signal reception.
// This is used to enable the function to execute any cleanup defer statements
// before exiting.
ctx, stop := signal.NotifyContext(context.Background(), syscall.SIGINT, syscall.SIGTERM, syscall.SIGHUP)
defer stop()

// We currently use the control websocket just to abort in case of errors.
controlDone := make(chan struct{}, 1)
handler := func(control *websocket.Conn) {
Expand Down Expand Up @@ -284,7 +293,8 @@ func (c *cmdConsole) vga(d lxd.InstanceServer, name string) error {
}

// Listen on the socket.
listener, err = net.Listen("unix", path.Name())
lc := net.ListenConfig{}
listener, err = lc.Listen(ctx, "unix", path.Name())
if err != nil {
return err
}
Expand Down Expand Up @@ -320,6 +330,12 @@ func (c *cmdConsole) vga(d lxd.InstanceServer, name string) error {
return err
}

// Cancel the operation if the context is canceled.
go func() {
<-ctx.Done()
_ = op.Cancel()
}()

// Handle connections to the socket.
wgConnections := sync.WaitGroup{}
chConnected := make(chan struct{})
Expand Down Expand Up @@ -405,4 +421,4 @@ func (c *cmdConsole) vga(d lxd.InstanceServer, name string) error {
}

return nil
}
}

0 comments on commit b721dbc

Please sign in to comment.