From 9dc2bdaffb36358fc3c32ad70aebfccdb91d62a8 Mon Sep 17 00:00:00 2001 From: Arthur Schreiber Date: Fri, 16 Jan 2026 14:15:32 +0000 Subject: [PATCH] Fix sporadic TestServingKeyspaces panic on context cancellation The test was failing intermittently with "sandboxTopo SaveVSchema returned an error: context canceled" due to a race condition where background goroutines spawned by KeyspaceEventWatcher would call WatchSrvVSchema after the test context was canceled. Handle context cancellation gracefully by checking ctx.Err() and returning early instead of panicking. Also add proper error handling for UpdateSrvVSchema which was previously ignoring errors. Co-Authored-By: Claude Opus 4.5 Signed-off-by: Arthur Schreiber --- go/vt/vtgate/sandbox_test.go | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/go/vt/vtgate/sandbox_test.go b/go/vt/vtgate/sandbox_test.go index 41d5f8c104b..ccd82b29e7c 100644 --- a/go/vt/vtgate/sandbox_test.go +++ b/go/vt/vtgate/sandbox_test.go @@ -298,6 +298,11 @@ func (sct *sandboxTopo) WatchSrvVSchema(ctx context.Context, cell string, callba return } + // Context may already be canceled during test cleanup - exit gracefully. + if ctx.Err() != nil { + return + } + // Update the backing topo server with the current sandbox vschemas. for ks := range ksToSandbox { ksvs := &topo.KeyspaceVSchemaInfo{ @@ -305,12 +310,23 @@ func (sct *sandboxTopo) WatchSrvVSchema(ctx context.Context, cell string, callba Keyspace: srvVSchema.Keyspaces[ks], } if err := sct.topoServer.SaveVSchema(ctx, ksvs); err != nil { + if ctx.Err() != nil { + return + } panic(fmt.Sprintf("sandboxTopo SaveVSchema returned an error: %v", err)) } } - sct.topoServer.UpdateSrvVSchema(ctx, cell, srvVSchema) + if err := sct.topoServer.UpdateSrvVSchema(ctx, cell, srvVSchema); err != nil { + if ctx.Err() != nil { + return + } + panic(fmt.Sprintf("sandboxTopo UpdateSrvVSchema returned an error: %v", err)) + } current, updateChan, err := sct.topoServer.WatchSrvVSchema(ctx, cell) if err != nil { + if ctx.Err() != nil { + return + } panic(fmt.Sprintf("sandboxTopo WatchSrvVSchema returned an error: %v", err)) } if !callback(current.Value, nil) {