gc was collecting the lockfile early causing it not to work. fixed with runtime.KeepAlive

This commit is contained in:
sawka 2023-11-09 16:26:44 -08:00
parent 68731f45a2
commit 1a566d06aa
2 changed files with 5 additions and 2 deletions

View File

@ -18,6 +18,7 @@ import (
"os/signal" "os/signal"
"path/filepath" "path/filepath"
"regexp" "regexp"
"runtime"
"runtime/debug" "runtime/debug"
"strconv" "strconv"
"strings" "strings"
@ -793,7 +794,7 @@ func main() {
scLock, err := scbase.AcquireWaveLock() scLock, err := scbase.AcquireWaveLock()
if err != nil || scLock == nil { if err != nil || scLock == nil {
log.Printf("[error] cannot acquire wave lock: %v\n", err) log.Printf("[error] cannot acquire wave lock (another instance of wavesrv is likely running): %v\n", err)
return return
} }
if len(os.Args) >= 2 && strings.HasPrefix(os.Args[1], "--migrate") { if len(os.Args) >= 2 && strings.HasPrefix(os.Args[1], "--migrate") {
@ -886,4 +887,5 @@ func main() {
if err != nil { if err != nil {
log.Printf("ERROR: %v\n", err) log.Printf("ERROR: %v\n", err)
} }
runtime.KeepAlive(scLock)
} }

View File

@ -156,7 +156,8 @@ func AcquireWaveLock() (*os.File, error) {
return nil, fmt.Errorf("cannot find/create WAVETERM_HOME directory %q", homeDir) return nil, fmt.Errorf("cannot find/create WAVETERM_HOME directory %q", homeDir)
} }
lockFileName := path.Join(homeDir, WaveLockFile) lockFileName := path.Join(homeDir, WaveLockFile)
fd, err := os.OpenFile(lockFileName, os.O_WRONLY|os.O_CREATE, 0600) log.Printf("[base] acquiring lock on %s\n", lockFileName)
fd, err := os.OpenFile(lockFileName, os.O_RDWR|os.O_CREATE, 0600)
if err != nil { if err != nil {
return nil, err return nil, err
} }