From 230d8f6028c279645cd4bece84916b958e75c02d Mon Sep 17 00:00:00 2001 From: WithoutPants <53250216+WithoutPants@users.noreply.github.com> Date: Tue, 5 Apr 2022 14:58:08 +1000 Subject: [PATCH] Make desktop.Start run on main thread (#2475) --- cmd/stash/main.go | 18 +++++++++++------- internal/desktop/desktop.go | 1 + internal/manager/favicon.go | 6 +++--- internal/manager/manager.go | 7 +++++-- 4 files changed, 20 insertions(+), 12 deletions(-) diff --git a/cmd/stash/main.go b/cmd/stash/main.go index b85bbfe98..c653150b9 100644 --- a/cmd/stash/main.go +++ b/cmd/stash/main.go @@ -5,12 +5,13 @@ import ( "fmt" "os" "os/signal" - "runtime/pprof" "syscall" "github.com/apenwarr/fixconsole" "github.com/stashapp/stash/internal/api" + "github.com/stashapp/stash/internal/desktop" "github.com/stashapp/stash/internal/manager" + "github.com/stashapp/stash/ui" _ "github.com/golang-migrate/migrate/v4/database/sqlite3" _ "github.com/golang-migrate/migrate/v4/source/file" @@ -28,18 +29,21 @@ func main() { manager.Initialize() api.Start() + go handleSignals() + desktop.Start(manager.GetInstance(), &manager.FaviconProvider{UIBox: ui.UIBox}) + blockForever() - - // stop any profiling at exit - pprof.StopCPUProfile() - - manager.GetInstance().Shutdown(0) } -func blockForever() { +func handleSignals() { // handle signals signals := make(chan os.Signal, 1) signal.Notify(signals, syscall.SIGINT, syscall.SIGTERM) <-signals + manager.GetInstance().Shutdown(0) +} + +func blockForever() { + select {} } diff --git a/internal/desktop/desktop.go b/internal/desktop/desktop.go index 7ff70a684..b2b44bf45 100644 --- a/internal/desktop/desktop.go +++ b/internal/desktop/desktop.go @@ -26,6 +26,7 @@ type FaviconProvider interface { } // Start starts the desktop icon process. It blocks until the process exits. +// MUST be run on the main goroutine or will have no effect on macOS func Start(shutdownHandler ShutdownHandler, faviconProvider FaviconProvider) { if IsDesktop() { c := config.GetInstance() diff --git a/internal/manager/favicon.go b/internal/manager/favicon.go index 9be34e3c0..6267cfec0 100644 --- a/internal/manager/favicon.go +++ b/internal/manager/favicon.go @@ -8,13 +8,13 @@ import ( const faviconDir = "v2.5/build/" type FaviconProvider struct { - uiBox embed.FS + UIBox embed.FS } func (p *FaviconProvider) GetFavicon() []byte { if runtime.GOOS == "windows" { faviconPath := faviconDir + "favicon.ico" - ret, _ := p.uiBox.ReadFile(faviconPath) + ret, _ := p.UIBox.ReadFile(faviconPath) return ret } @@ -23,6 +23,6 @@ func (p *FaviconProvider) GetFavicon() []byte { func (p *FaviconProvider) GetFaviconPng() []byte { faviconPath := faviconDir + "favicon.png" - ret, _ := p.uiBox.ReadFile(faviconPath) + ret, _ := p.UIBox.ReadFile(faviconPath) return ret } diff --git a/internal/manager/manager.go b/internal/manager/manager.go index 83be48067..3314e20ed 100644 --- a/internal/manager/manager.go +++ b/internal/manager/manager.go @@ -255,7 +255,6 @@ func (s *singleton) PostInit(ctx context.Context) error { s.ScraperCache = instance.initScraperCache() writeStashIcon() - go desktop.Start(instance, &FaviconProvider{uiBox: ui.UIBox}) // clear the downloads and tmp directories // #1021 - only clear these directories if the generated folder is non-empty @@ -289,7 +288,7 @@ func (s *singleton) PostInit(ctx context.Context) error { func writeStashIcon() { p := FaviconProvider{ - uiBox: ui.UIBox, + UIBox: ui.UIBox, } iconPath := filepath.Join(instance.Config.GetConfigPath(), "icon.png") @@ -484,6 +483,9 @@ func (s *singleton) GetSystemStatus() *models.SystemStatus { // Shutdown gracefully stops the manager func (s *singleton) Shutdown(code int) { + // stop any profiling at exit + pprof.StopCPUProfile() + // TODO: Each part of the manager needs to gracefully stop at some point // for now, we just close the database. err := database.Close() @@ -493,5 +495,6 @@ func (s *singleton) Shutdown(code int) { os.Exit(1) } } + os.Exit(code) }