Skip to content

Commit 650c7de

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

2 files changed

Lines changed: 42 additions & 0 deletions

File tree

internal/corazawaf/transaction.go

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

1645+
tx.transformationCache = map[transformationKey]*transformationValue{}
1646+
16451647
if len(errs) == 0 {
16461648
return nil
16471649
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
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 two transactions
14+
tx1, tx2 := waf.NewTransaction(), waf.NewTransaction()
15+
assert.Empty(t, tx1.transformationCache)
16+
assert.Empty(t, tx2.transformationCache)
17+
// put some value in the transformation cache for tx1
18+
tk1 := transformationKey{transformationsID: 1}
19+
tx1.transformationCache[tk1] = &transformationValue{arg: "bla"}
20+
assert.NotEmpty(t, tx1.transformationCache)
21+
22+
t.Logf("tx1.transformationCache pointer is %p", tx1.transformationCache)
23+
t.Logf("tx2.transformationCache pointer is %p", tx2.transformationCache)
24+
// close both transactions
25+
if err := tx1.Close(); err != nil {
26+
t.Fatalf("Failed to close tx1: %s", err.Error())
27+
}
28+
if err := tx2.Close(); err != nil {
29+
t.Fatalf("Failed to close tx2: %s", err.Error())
30+
}
31+
32+
tx3, tx4 := waf.NewTransaction(), waf.NewTransaction()
33+
// Compare map pointers using reflection to ensure they are different instances
34+
assert.Emptyf(t, tx3.transformationCache,
35+
"tx3.transformationCache should be empty for new transactions, but it is not, pointer is %p",
36+
tx3.transformationCache)
37+
assert.Emptyf(t, tx4.transformationCache,
38+
"tx4.transformationCache should be empty for new transactions, but it is not, pointer is %p",
39+
tx4.transformationCache)
40+
}

0 commit comments

Comments
 (0)