Skip to content

Commit 50f94bf

Browse files
committed
fix(eventlistener): remove goroutine from updateSnapshot
Rename updateSnapshotAsync to updateSnapshot and remove the go func() wrapper. The event loop is already serial, and the mutex on SnapshotManager now protects concurrent callers from other paths (certificates, control plane sync).
1 parent 641d92f commit 50f94bf

4 files changed

Lines changed: 18 additions & 20 deletions

File tree

gateway/gateway-controller/pkg/eventlistener/api_processor.go

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -43,21 +43,19 @@ func (l *EventListener) processAPIEvent(event eventhub.Event) {
4343
}
4444
}
4545

46-
func (l *EventListener) updateSnapshotAsync(entityID, correlationID, failureMessage string) {
46+
func (l *EventListener) updateSnapshot(entityID, correlationID, failureMessage string) {
4747
if l.snapshotManager == nil {
4848
return
4949
}
5050

51-
go func() {
52-
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
53-
defer cancel()
51+
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
52+
defer cancel()
5453

55-
if err := l.snapshotManager.UpdateSnapshot(ctx, correlationID); err != nil {
56-
l.logger.Error(failureMessage,
57-
slog.String("entity_id", entityID),
58-
slog.Any("error", err))
59-
}
60-
}()
54+
if err := l.snapshotManager.UpdateSnapshot(ctx, correlationID); err != nil {
55+
l.logger.Error(failureMessage,
56+
slog.String("entity_id", entityID),
57+
slog.Any("error", err))
58+
}
6159
}
6260

6361
// handleAPICreateOrUpdate handles API create or update events
@@ -109,7 +107,7 @@ func (l *EventListener) handleAPICreateOrUpdate(event eventhub.Event) {
109107

110108
// Update xDS snapshot for REST APIs only (WebSubApi and WebBrokerApi use Policy xDS)
111109
if storedConfig.Kind != models.KindWebSubApi && storedConfig.Kind != models.KindWebBrokerApi {
112-
l.updateSnapshotAsync(entityID, event.EventID, "Failed to update xDS snapshot after replica sync")
110+
l.updateSnapshot(entityID, event.EventID, "Failed to update xDS snapshot after replica sync")
113111
}
114112

115113
// Update policies
@@ -188,7 +186,7 @@ func (l *EventListener) handleAPIDelete(event eventhub.Event) {
188186

189187
// Update xDS snapshot for REST APIs only (WebSubApi and WebBrokerApi use Policy xDS)
190188
if existingConfig == nil || (existingConfig.Kind != models.KindWebSubApi && existingConfig.Kind != models.KindWebBrokerApi) {
191-
l.updateSnapshotAsync(entityID, event.EventID, "Failed to update xDS snapshot after API deletion")
189+
l.updateSnapshot(entityID, event.EventID, "Failed to update xDS snapshot after API deletion")
192190
}
193191

194192
// Remove runtime config for the deleted API

gateway/gateway-controller/pkg/eventlistener/llm_provider_processor.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ func (l *EventListener) handleLLMProviderCreateOrUpdate(event eventhub.Event) {
122122
slog.Any("error", err))
123123
}
124124

125-
l.updateSnapshotAsync(entityID, event.EventID, "Failed to update xDS snapshot after LLM provider replica sync")
125+
l.updateSnapshot(entityID, event.EventID, "Failed to update xDS snapshot after LLM provider replica sync")
126126
l.updatePoliciesForAPI(storedConfig, event.EventID)
127127

128128
l.logger.Info("Successfully processed LLM provider create/update event",
@@ -184,7 +184,7 @@ func (l *EventListener) handleLLMProxyCreateOrUpdate(event eventhub.Event) {
184184
}
185185
}
186186

187-
l.updateSnapshotAsync(entityID, event.EventID, "Failed to update xDS snapshot after LLM proxy replica sync")
187+
l.updateSnapshot(entityID, event.EventID, "Failed to update xDS snapshot after LLM proxy replica sync")
188188
l.updatePoliciesForAPI(storedConfig, event.EventID)
189189

190190
l.logger.Info("Successfully processed LLM proxy create/update event",
@@ -243,7 +243,7 @@ func (l *EventListener) handleLLMProviderDelete(event eventhub.Event) {
243243
}
244244
}
245245

246-
l.updateSnapshotAsync(entityID, event.EventID, "Failed to update xDS snapshot after LLM provider deletion")
246+
l.updateSnapshot(entityID, event.EventID, "Failed to update xDS snapshot after LLM provider deletion")
247247

248248
if l.policyManager != nil && existingConfig != nil {
249249
if err := l.policyManager.DeleteAPIConfig(existingConfig.Kind, existingConfig.Handle); err != nil {
@@ -300,7 +300,7 @@ func (l *EventListener) handleLLMProxyDelete(event eventhub.Event) {
300300
}
301301
}
302302

303-
l.updateSnapshotAsync(entityID, event.EventID, "Failed to update xDS snapshot after LLM proxy deletion")
303+
l.updateSnapshot(entityID, event.EventID, "Failed to update xDS snapshot after LLM proxy deletion")
304304

305305
if l.policyManager != nil && existingConfig != nil {
306306
if err := l.policyManager.DeleteAPIConfig(existingConfig.Kind, existingConfig.Handle); err != nil {

gateway/gateway-controller/pkg/eventlistener/mcp_processor.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ func (l *EventListener) handleMCPProxyCreateOrUpdate(event eventhub.Event) {
9595
}
9696
}
9797

98-
l.updateSnapshotAsync(entityID, event.EventID, "Failed to update xDS snapshot after MCP proxy replica sync")
98+
l.updateSnapshot(entityID, event.EventID, "Failed to update xDS snapshot after MCP proxy replica sync")
9999
l.updatePoliciesForAPI(storedConfig, event.EventID)
100100

101101
l.logger.Info("Successfully processed MCP proxy create/update event",
@@ -119,7 +119,7 @@ func (l *EventListener) handleMCPProxyDelete(event eventhub.Event) {
119119
return
120120
}
121121

122-
l.updateSnapshotAsync(entityID, event.EventID, "Failed to update xDS snapshot after MCP proxy deletion")
122+
l.updateSnapshot(entityID, event.EventID, "Failed to update xDS snapshot after MCP proxy deletion")
123123

124124
if l.policyManager != nil && existingConfig != nil {
125125
if err := l.policyManager.DeleteAPIConfig(existingConfig.Kind, existingConfig.Handle); err != nil {

gateway/gateway-controller/pkg/xds/snapshot_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,9 +107,9 @@ func TestConcurrentUpdateSnapshot(t *testing.T) {
107107
close(aGotAll)
108108
select {
109109
case <-bDone:
110-
// no mutex — B finished first, A will overwrite with stale data
110+
// pre-fix path: B raced past A → stale overwrite will occur
111111
case <-time.After(200 * time.Millisecond):
112-
// mutex heldB is waiting for the lock, A continues
112+
// post-fix path: mutex held, B is queued behind A
113113
}
114114
}
115115

0 commit comments

Comments
 (0)