Skip to content

Commit c8e8eeb

Browse files
authored
fix race when recycling filterManager (#613)
Signed-off-by: spacewander <[email protected]>
1 parent 1d5db16 commit c8e8eeb

File tree

2 files changed

+11
-4
lines changed

2 files changed

+11
-4
lines changed

api/pkg/filtermanager/filtermanager.go

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ type filterManager struct {
4343
encodeIdx int
4444
rspHdr api.ResponseHeaderMap
4545

46-
runningInGoThread atomic.Bool
46+
runningInGoThread atomic.Int32
4747
hdrLock sync.Mutex // FIXME: remove this once we get request headers from the OnLog directly
4848

4949
// use a group of bools instead of map to avoid lookup
@@ -71,6 +71,8 @@ func (m *filterManager) Reset() {
7171
m.encodeIdx = -1
7272
m.rspHdr = nil
7373

74+
m.runningInGoThread.Store(0) // defence in depth
75+
7476
m.canSkipDecodeHeaders = false
7577
m.canSkipDecodeData = false
7678
m.canSkipEncodeHeaders = false
@@ -81,11 +83,15 @@ func (m *filterManager) Reset() {
8183
}
8284

8385
func (m *filterManager) IsRunningInGoThread() bool {
84-
return m.runningInGoThread.Load()
86+
return m.runningInGoThread.Load() != 0
8587
}
8688

8789
func (m *filterManager) MarkRunningInGoThread(flag bool) {
88-
m.runningInGoThread.Store(flag)
90+
if flag {
91+
m.runningInGoThread.Add(1)
92+
} else {
93+
m.runningInGoThread.Add(-1)
94+
}
8995
}
9096

9197
func (m *filterManager) DebugModeEnabled() bool {

api/pkg/filtermanager/filtermanager_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -528,6 +528,7 @@ func (f *accessFieldOnLogFilter) OnLog(_ api.RequestHeaderMap, _ api.RequestTrai
528528
}
529529

530530
func TestDoNotRecycleInUsedFilterManager(t *testing.T) {
531+
envoy.DisableLogInTest() // otherwise, there is too much output
531532
config := initFilterManagerConfig("ns")
532533
config.parsed = []*model.ParsedFilterConfig{
533534
{
@@ -536,7 +537,7 @@ func TestDoNotRecycleInUsedFilterManager(t *testing.T) {
536537
},
537538
}
538539

539-
n := 10
540+
n := 100
540541
var wg sync.WaitGroup
541542

542543
// DecodeHeaders

0 commit comments

Comments
 (0)