Skip to content

Commit 352be5b

Browse files
committed
Make sure that HugoSites is always closed when done
Including all the integration tests.
1 parent d37606d commit 352be5b

File tree

4 files changed

+32
-4
lines changed

4 files changed

+32
-4
lines changed

commands/commandeer.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ import (
4242
"github.com/gohugoio/hugo/common/hugo"
4343
"github.com/gohugoio/hugo/common/loggers"
4444
"github.com/gohugoio/hugo/common/paths"
45+
"github.com/gohugoio/hugo/common/types"
4546
"github.com/gohugoio/hugo/config"
4647
"github.com/gohugoio/hugo/config/allconfig"
4748
"github.com/gohugoio/hugo/deps"
@@ -66,6 +67,12 @@ func Execute(args []string) error {
6667
}
6768
args = mapLegacyArgs(args)
6869
cd, err := x.Execute(context.Background(), args)
70+
if cd != nil {
71+
if closer, ok := cd.Root.Command.(types.Closer); ok {
72+
closer.Close()
73+
}
74+
}
75+
6976
if err != nil {
7077
if err == errHelp {
7178
cd.CobraCommand.Help()
@@ -149,6 +156,18 @@ func (r *rootCommand) isVerbose() bool {
149156
return r.logger.Level() <= logg.LevelInfo
150157
}
151158

159+
func (r *rootCommand) Close() error {
160+
if r.hugoSites != nil {
161+
r.hugoSites.DeleteFunc(func(key configKey, value *hugolib.HugoSites) bool {
162+
if value != nil {
163+
value.Close()
164+
}
165+
return false
166+
})
167+
}
168+
return nil
169+
}
170+
152171
func (r *rootCommand) Build(cd *simplecobra.Commandeer, bcfg hugolib.BuildCfg, cfg config.Provider) (*hugolib.HugoSites, error) {
153172
h, err := r.Hugo(cfg)
154173
if err != nil {

commands/server.go

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1012,10 +1012,6 @@ func (c *serverCommand) serve() error {
10121012
c.r.Println("Error:", err)
10131013
}
10141014

1015-
if h := c.hugoTry(); h != nil {
1016-
h.Close()
1017-
}
1018-
10191015
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
10201016
defer cancel()
10211017
wg2, ctx := errgroup.WithContext(ctx)

deps/deps.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,8 @@ type Deps struct {
9898
// TODO(bep) rethink this re. a plugin setup, but this will have to do for now.
9999
WasmDispatchers *warpc.Dispatchers
100100

101+
isClosed bool
102+
101103
*globalErrHandler
102104
}
103105

@@ -345,6 +347,11 @@ func (d *Deps) TextTmpl() tpl.TemplateParseFinder {
345347
}
346348

347349
func (d *Deps) Close() error {
350+
if d.isClosed {
351+
return nil
352+
}
353+
d.isClosed = true
354+
348355
if d.MemCache != nil {
349356
d.MemCache.Stop()
350357
}

hugolib/integrationtest_builder.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -421,6 +421,12 @@ func (s *IntegrationTestBuilder) Build() *IntegrationTestBuilder {
421421
s.Assert(err, qt.IsNil)
422422
}
423423

424+
s.Cleanup(func() {
425+
if h := s.H; h != nil {
426+
s.Assert(h.Close(), qt.IsNil)
427+
}
428+
})
429+
424430
return s
425431
}
426432

0 commit comments

Comments
 (0)