Skip to content

Commit

Permalink
dynacache: Fix potential deadlocks on panics in GetOrCreate
Browse files Browse the repository at this point in the history
  • Loading branch information
bep committed Oct 26, 2024
1 parent ec3890a commit 5e0ee51
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 1 deletion.
23 changes: 23 additions & 0 deletions cache/dynacache/dynacache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package dynacache
import (
"path/filepath"
"testing"
"time"

qt "github.com/frankban/quicktest"
"github.com/gohugoio/hugo/common/loggers"
Expand Down Expand Up @@ -165,6 +166,28 @@ func TestClear(t *testing.T) {
cache.adjustCurrentMaxSize()
}

func TestPanicInCreate(t *testing.T) {
t.Parallel()
c := qt.New(t)
cache := newTestCache(t)

p1 := GetOrCreatePartition[string, testItem](cache, "/aaaa/bbbb", OptionsPartition{Weight: 30, ClearWhen: ClearOnRebuild})

for i := 0; i < 10; i++ {
_, err := p1.GetOrCreate("pani1", func(string) (testItem, error) {
panic("failed")
})

c.Assert(err, qt.Not(qt.IsNil))

_, err = p1.GetOrCreateWitTimeout("panic2", 10*time.Second, func(string) (testItem, error) {
panic("failed")
})

c.Assert(err, qt.Not(qt.IsNil))
}
}

func TestAdjustCurrentMaxSize(t *testing.T) {
t.Parallel()
c := qt.New(t)
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ require (
github.com/bep/gowebp v0.3.0
github.com/bep/helpers v0.5.0
github.com/bep/imagemeta v0.8.1
github.com/bep/lazycache v0.4.0
github.com/bep/lazycache v0.6.0
github.com/bep/logg v0.4.0
github.com/bep/mclib v1.20400.20402
github.com/bep/overlayfs v0.9.2
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,8 @@ github.com/bep/imagemeta v0.8.1 h1:tjZLPRftjxU7PTI87o5e5WKOFQ4S9S0engiP1OTpJTI=
github.com/bep/imagemeta v0.8.1/go.mod h1:5piPAq5Qomh07m/dPPCLN3mDJyFusvUG7VwdRD/vX0s=
github.com/bep/lazycache v0.4.0 h1:X8yVyWNVupPd4e1jV7efi3zb7ZV/qcjKQgIQ5aPbkYI=
github.com/bep/lazycache v0.4.0/go.mod h1:NmRm7Dexh3pmR1EignYR8PjO2cWybFQ68+QgY3VMCSc=
github.com/bep/lazycache v0.6.0 h1:0vCgFo7TBtMQpSx64jnH1sagmw0ZougIFRpsqPHTa5U=
github.com/bep/lazycache v0.6.0/go.mod h1:NmRm7Dexh3pmR1EignYR8PjO2cWybFQ68+QgY3VMCSc=
github.com/bep/logg v0.4.0 h1:luAo5mO4ZkhA5M1iDVDqDqnBBnlHjmtZF6VAyTp+nCQ=
github.com/bep/logg v0.4.0/go.mod h1:Ccp9yP3wbR1mm++Kpxet91hAZBEQgmWgFgnXX3GkIV0=
github.com/bep/mclib v1.20400.20402 h1:olpCE2WSPpOAbFE1R4hnftSEmQ34+xzy2HRzd0m69rA=
Expand Down

0 comments on commit 5e0ee51

Please sign in to comment.