Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make sure that HugoSites is always closed when done #12972

Merged
merged 1 commit into from
Oct 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions commands/commandeer.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -66,6 +67,12 @@ func Execute(args []string) error {
}
args = mapLegacyArgs(args)
cd, err := x.Execute(context.Background(), args)
if cd != nil {
if closer, ok := cd.Root.Command.(types.Closer); ok {
closer.Close()
}
}

if err != nil {
if err == errHelp {
cd.CobraCommand.Help()
Expand Down Expand Up @@ -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 {
Expand Down
4 changes: 0 additions & 4 deletions commands/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
7 changes: 7 additions & 0 deletions deps/deps.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down Expand Up @@ -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()
}
Expand Down
6 changes: 6 additions & 0 deletions hugolib/integrationtest_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down