Skip to content

Commit

Permalink
fix race when recycling filterManager (#613)
Browse files Browse the repository at this point in the history
Signed-off-by: spacewander <[email protected]>
  • Loading branch information
spacewander authored Jul 1, 2024
1 parent 1d5db16 commit c8e8eeb
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 4 deletions.
12 changes: 9 additions & 3 deletions api/pkg/filtermanager/filtermanager.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ type filterManager struct {
encodeIdx int
rspHdr api.ResponseHeaderMap

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

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

m.runningInGoThread.Store(0) // defence in depth

m.canSkipDecodeHeaders = false
m.canSkipDecodeData = false
m.canSkipEncodeHeaders = false
Expand All @@ -81,11 +83,15 @@ func (m *filterManager) Reset() {
}

func (m *filterManager) IsRunningInGoThread() bool {
return m.runningInGoThread.Load()
return m.runningInGoThread.Load() != 0
}

func (m *filterManager) MarkRunningInGoThread(flag bool) {
m.runningInGoThread.Store(flag)
if flag {
m.runningInGoThread.Add(1)
} else {
m.runningInGoThread.Add(-1)
}
}

func (m *filterManager) DebugModeEnabled() bool {
Expand Down
3 changes: 2 additions & 1 deletion api/pkg/filtermanager/filtermanager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -528,6 +528,7 @@ func (f *accessFieldOnLogFilter) OnLog(_ api.RequestHeaderMap, _ api.RequestTrai
}

func TestDoNotRecycleInUsedFilterManager(t *testing.T) {
envoy.DisableLogInTest() // otherwise, there is too much output
config := initFilterManagerConfig("ns")
config.parsed = []*model.ParsedFilterConfig{
{
Expand All @@ -536,7 +537,7 @@ func TestDoNotRecycleInUsedFilterManager(t *testing.T) {
},
}

n := 10
n := 100
var wg sync.WaitGroup

// DecodeHeaders
Expand Down

0 comments on commit c8e8eeb

Please sign in to comment.