From 821b89e494e4bbf1bdfdd7cf9af50c68e6e5ef3e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B8rn=20Erik=20Pedersen?= Date: Sun, 20 Oct 2024 11:25:16 +0200 Subject: [PATCH] Make sure that HugoSites is always closed when done Including all the integration tests. --- commands/commandeer.go | 19 +++++++++++++++++++ commands/server.go | 4 ---- deps/deps.go | 7 +++++++ hugolib/integrationtest_builder.go | 6 ++++++ 4 files changed, 32 insertions(+), 4 deletions(-) diff --git a/commands/commandeer.go b/commands/commandeer.go index 06565d45d7a..f535fe7c93f 100644 --- a/commands/commandeer.go +++ b/commands/commandeer.go @@ -42,6 +42,7 @@ import ( "github.com/gohugoio/hugo/common/hugo" "github.com/gohugoio/hugo/common/loggers" "github.com/gohugoio/hugo/common/paths" + "github.com/gohugoio/hugo/common/types" "github.com/gohugoio/hugo/config" "github.com/gohugoio/hugo/config/allconfig" "github.com/gohugoio/hugo/deps" @@ -66,6 +67,12 @@ func Execute(args []string) error { } args = mapLegacyArgs(args) cd, err := x.Execute(context.Background(), args) + if err == nil && cd != nil { + if closer, ok := cd.Root.Command.(types.Closer); ok { + defer closer.Close() + } + } + if err != nil { if err == errHelp { cd.CobraCommand.Help() @@ -149,6 +156,18 @@ func (r *rootCommand) isVerbose() bool { return r.logger.Level() <= logg.LevelInfo } +func (r *rootCommand) Close() error { + if r.hugoSites != nil { + r.hugoSites.DeleteFunc(func(key configKey, value *hugolib.HugoSites) bool { + if value != nil { + value.Close() + } + return false + }) + } + return nil +} + func (r *rootCommand) Build(cd *simplecobra.Commandeer, bcfg hugolib.BuildCfg, cfg config.Provider) (*hugolib.HugoSites, error) { h, err := r.Hugo(cfg) if err != nil { diff --git a/commands/server.go b/commands/server.go index b16bf314883..84d4165f0cc 100644 --- a/commands/server.go +++ b/commands/server.go @@ -1012,10 +1012,6 @@ func (c *serverCommand) serve() error { c.r.Println("Error:", err) } - if h := c.hugoTry(); h != nil { - h.Close() - } - ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) defer cancel() wg2, ctx := errgroup.WithContext(ctx) diff --git a/deps/deps.go b/deps/deps.go index e137aed7b10..4389036cbdf 100644 --- a/deps/deps.go +++ b/deps/deps.go @@ -98,6 +98,8 @@ type Deps struct { // TODO(bep) rethink this re. a plugin setup, but this will have to do for now. WasmDispatchers *warpc.Dispatchers + isClosed bool + *globalErrHandler } @@ -345,6 +347,11 @@ func (d *Deps) TextTmpl() tpl.TemplateParseFinder { } func (d *Deps) Close() error { + if d.isClosed { + return nil + } + d.isClosed = true + if d.MemCache != nil { d.MemCache.Stop() } diff --git a/hugolib/integrationtest_builder.go b/hugolib/integrationtest_builder.go index 551b807dba0..b45defb42d2 100644 --- a/hugolib/integrationtest_builder.go +++ b/hugolib/integrationtest_builder.go @@ -421,6 +421,12 @@ func (s *IntegrationTestBuilder) Build() *IntegrationTestBuilder { s.Assert(err, qt.IsNil) } + s.Cleanup(func() { + if h := s.H; h != nil { + s.Assert(h.Close(), qt.IsNil) + } + }) + return s }