Skip to content

Commit

Permalink
Fix panic in vttablet when closing topo server twice (#17094)
Browse files Browse the repository at this point in the history
  • Loading branch information
frouioui authored Oct 31, 2024
1 parent 9c9a893 commit ffaeba0
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 4 deletions.
2 changes: 0 additions & 2 deletions go/cmd/vttablet/cli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,6 @@ func run(cmd *cobra.Command, args []string) error {

qsc, err := createTabletServer(ctx, env, config, ts, tabletAlias, srvTopoCounts)
if err != nil {
ts.Close()
return err
}

Expand Down Expand Up @@ -172,7 +171,6 @@ func run(cmd *cobra.Command, args []string) error {
VDiffEngine: vdiff.NewEngine(ts, tablet, env.CollationEnv(), env.Parser()),
}
if err := tm.Start(tablet, config); err != nil {
ts.Close()
return fmt.Errorf("failed to parse --tablet-path or initialize DB credentials: %w", err)
}
servenv.OnClose(func() {
Expand Down
58 changes: 58 additions & 0 deletions go/cmd/vttablet/cli/cli_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/*
Copyright 2024 The Vitess Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package cli

import (
"context"
"os"
"testing"

"github.com/stretchr/testify/require"

"vitess.io/vitess/go/vt/topo"
"vitess.io/vitess/go/vt/topo/memorytopo"
)

// TestRunFailsToStartTabletManager tests the code path in 'run' where we fail to start the TabletManager
// this is done by starting vttablet without a cnf file but requesting it to restore from backup.
// When starting, the TabletManager checks if it needs to restore, in tm.handleRestore but this step will
// fail if we do not provide a cnf file and if the flag --restore_from_backup is provided.
func TestRunFailsToStartTabletManager(t *testing.T) {
ts, factory := memorytopo.NewServerAndFactory(context.Background(), "cell")
topo.RegisterFactory("test", factory)

args := append([]string{}, os.Args...)
t.Cleanup(func() {
ts.Close()
tabletPath = ""
os.Args = append([]string{}, args...)
})

os.Args = []string{"vttablet",
"--topo_implementation", "test", "--topo_global_server_address", "localhost", "--topo_global_root", "cell",
"--db_host", "localhost", "--db_port", "3306",
"--tablet-path", "cell-1", "--init_keyspace", "ks", "--init_shard", "0", "--init_tablet_type", "replica",
"--restore_from_backup",
}

// Creating and canceling the context so that pending tasks in tm_init gets canceled before we close the topo server
ctx, cancel := context.WithCancel(context.Background())
defer cancel()

err := Main.ExecuteContext(ctx)
require.ErrorContains(t, err, "you cannot enable --restore_from_backup without a my.cnf file")
}
6 changes: 4 additions & 2 deletions go/vt/topo/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -343,8 +343,10 @@ func GetAliasByCell(ctx context.Context, ts *Server, cell string) string {
// Close will close all connections to underlying topo Server.
// It will nil all member variables, so any further access will panic.
func (ts *Server) Close() {
ts.globalCell.Close()
if ts.globalReadOnlyCell != ts.globalCell {
if ts.globalCell != nil {
ts.globalCell.Close()
}
if ts.globalReadOnlyCell != nil && ts.globalReadOnlyCell != ts.globalCell {
ts.globalReadOnlyCell.Close()
}
ts.globalCell = nil
Expand Down

0 comments on commit ffaeba0

Please sign in to comment.