Skip to content

Commit bceb766

Browse files
author
Ruslan Shmelev
committed
DEF-37554 fix transaction data race: recreate transformation cache on tx.Close()
1 parent 7e054cf commit bceb766

File tree

2 files changed

+55
-0
lines changed

2 files changed

+55
-0
lines changed

internal/corazawaf/transaction.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1642,6 +1642,9 @@ func (tx *Transaction) Close() error {
16421642
Msg("Transaction finished")
16431643
}
16441644

1645+
tx.transformationCache = map[transformationKey]*transformationValue{}
1646+
tx.variables = *NewTransactionVariables(tx.WAF.persistenceEngine)
1647+
16451648
if len(errs) == 0 {
16461649
return nil
16471650
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package corazawaf
2+
3+
import (
4+
"testing"
5+
6+
"github.com/stretchr/testify/assert"
7+
)
8+
9+
// TestTransactionPoolCacheIsolation verifies that each transaction gets its own cache
10+
func TestTransactionPoolCacheIsolation(t *testing.T) {
11+
waf := NewWAF()
12+
13+
// Create the first transactions
14+
tx1 := waf.NewTransaction()
15+
assert.Empty(t, tx1.transformationCache)
16+
// put some value in the transformation cache for tx1
17+
tk1 := transformationKey{transformationsID: 1}
18+
tx1.transformationCache[tk1] = &transformationValue{arg: "bla"}
19+
assert.NotEmpty(t, tx1.transformationCache)
20+
21+
t.Logf("tx1.transformationCache pointer is %p", tx1.transformationCache)
22+
// close both transactions
23+
if err := tx1.Close(); err != nil {
24+
t.Fatalf("Failed to close tx1: %s", err.Error())
25+
}
26+
27+
// Create the second transaction and save its pointer to transformationCache
28+
// Create two transactions
29+
tx2 := waf.NewTransaction()
30+
assert.Empty(t, tx2.transformationCache)
31+
// put some value in the transformation cache for tx1
32+
33+
t.Logf("tx2.transformationCache pointer is %p", tx2.transformationCache)
34+
if err := tx2.Close(); err != nil {
35+
t.Fatalf("Failed to close tx2: %s", err.Error())
36+
}
37+
38+
tx3, tx4 := waf.NewTransaction(), waf.NewTransaction()
39+
//tx1.variables
40+
41+
t.Logf("\ntx1.transformationCache pointer is %p\n"+
42+
"tx2.tranformactionCache pointer is %p\n"+
43+
"tx3.tranformactionCache pointer is %p\n"+
44+
"tx4.tranformactionCache pointer is %p\n",
45+
tx1.transformationCache,
46+
tx2.transformationCache,
47+
tx3.transformationCache,
48+
tx4.transformationCache)
49+
50+
assert.Empty(t, tx3.transformationCache)
51+
assert.Empty(t, tx4.transformationCache)
52+
}

0 commit comments

Comments
 (0)