Close UART gracefully on SIGINT/SIGTERM in goTool.
Go exits on Ctrl+C without running defers, leaving the serial port locked; register shutdown hooks for serve and CLI commands. Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
+23
-5
@@ -2,6 +2,7 @@ package main
|
||||
|
||||
import (
|
||||
"embed"
|
||||
"errors"
|
||||
"flag"
|
||||
"fmt"
|
||||
"io/fs"
|
||||
@@ -34,21 +35,29 @@ func runServe(portName string, baud int, args []string) error {
|
||||
|
||||
link := newManagedSerial(portName, baud)
|
||||
link.quiet = true
|
||||
defer link.Close()
|
||||
|
||||
hub := newWSHub()
|
||||
streamCtl := newAccelStreamCtl()
|
||||
tapCtl := newTapNotifyCtl()
|
||||
stop := make(chan struct{})
|
||||
defer close(stop)
|
||||
|
||||
var dashSrv *http.Server
|
||||
var apiSrv *http.Server
|
||||
registerShutdown(func() {
|
||||
close(stop)
|
||||
shutdownHTTPServer(dashSrv)
|
||||
shutdownAPIServer(apiSrv)
|
||||
if err := link.Close(); err != nil {
|
||||
log.Printf("UART close: %v", err)
|
||||
}
|
||||
})
|
||||
|
||||
go runPoller(link, portName, hub, streamCtl, tapCtl, *interval, stop)
|
||||
go runBatteryPoller(link, hub, 5*time.Second, stop)
|
||||
go runCacheStatusDashboardPoller(link, hub, *accelInterval, stop)
|
||||
|
||||
var apiSrv *http.Server
|
||||
if *apiAddr != "" {
|
||||
apiSrv = runAPIServer(portName, link, *apiAddr, *accelInterval, hub, streamCtl, tapCtl, stop)
|
||||
defer shutdownAPIServer(apiSrv)
|
||||
}
|
||||
|
||||
mux := http.NewServeMux()
|
||||
@@ -81,5 +90,14 @@ func runServe(portName string, baud int, args []string) error {
|
||||
if *apiAddr == "" {
|
||||
log.Printf("external API disabled (-api-addr \"\")")
|
||||
}
|
||||
return http.ListenAndServe(*addr, mux)
|
||||
|
||||
dashSrv = &http.Server{Addr: *addr, Handler: mux}
|
||||
go func() {
|
||||
if err := dashSrv.ListenAndServe(); err != nil && !errors.Is(err, http.ErrServerClosed) {
|
||||
log.Printf("dashboard server: %v", err)
|
||||
}
|
||||
}()
|
||||
|
||||
waitForShutdown()
|
||||
return nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user