Skip to content

api: fix panic in conn.NewWatcher() #439

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ Versioning](http://semver.org/spec/v2.0.0.html) except to the first release.

### Fixed

- Fixed panic when calling NewWatcher() during reconnection or after
connection is closed (#438).

## [v2.3.2] - 2025-04-14

This release improves the logic of `Connect` and `pool.Connect` in case of a
Expand Down
2 changes: 1 addition & 1 deletion connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -1461,7 +1461,7 @@ func (conn *Connection) NewWatcher(key string, callback WatchCallback) (Watcher,
// That's why we can't just check the Tarantool response for an unsupported
// request error.
if !isFeatureInSlice(iproto.IPROTO_FEATURE_WATCHERS,
conn.c.ProtocolInfo().Features) {
conn.serverProtocolInfo.Features) {
err := fmt.Errorf("the feature %s must be supported by connection "+
"to create a watcher", iproto.IPROTO_FEATURE_WATCHERS)
return nil, err
Expand Down
70 changes: 68 additions & 2 deletions tarantool_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3550,6 +3550,72 @@ func TestConnection_NewWatcher(t *testing.T) {
}
}

func newWatcherReconnectionPrepareTestConnection(t *testing.T) (*Connection, context.CancelFunc) {
t.Helper()

const server = "127.0.0.1:3015"
testDialer := dialer
testDialer.Address = server

inst, err := test_helpers.StartTarantool(test_helpers.StartOpts{
Dialer: testDialer,
InitScript: "config.lua",
Listen: server,
WaitStart: 100 * time.Millisecond,
ConnectRetry: 10,
RetryTimeout: 500 * time.Millisecond,
})
t.Cleanup(func() { test_helpers.StopTarantoolWithCleanup(inst) })
if err != nil {
t.Fatalf("Unable to start Tarantool: %s", err)
}

ctx, cancel := test_helpers.GetConnectContext()

reconnectOpts := opts
reconnectOpts.Reconnect = 100 * time.Millisecond
reconnectOpts.MaxReconnects = 0
reconnectOpts.Notify = make(chan ConnEvent)
conn, err := Connect(ctx, testDialer, reconnectOpts)
if err != nil {
t.Fatalf("Connection was not established: %v", err)
}

test_helpers.StopTarantool(inst)

// Wait for reconnection process to be started.
for conn.ConnectedNow() {
time.Sleep(100 * time.Millisecond)
}

return conn, cancel
}

func TestNewWatcherDuringReconnect(t *testing.T) {
test_helpers.SkipIfWatchersUnsupported(t)

conn, cancel := newWatcherReconnectionPrepareTestConnection(t)
defer conn.Close()
defer cancel()

_, err := conn.NewWatcher("one", func(event WatchEvent) {})
assert.NotNil(t, err)
assert.ErrorContains(t, err, "client connection is not ready")
}

func TestNewWatcherAfterClose(t *testing.T) {
test_helpers.SkipIfWatchersUnsupported(t)

conn, cancel := newWatcherReconnectionPrepareTestConnection(t)
defer cancel()

_ = conn.Close()

_, err := conn.NewWatcher("one", func(event WatchEvent) {})
assert.NotNil(t, err)
assert.ErrorContains(t, err, "using closed connection")
}

func TestConnection_NewWatcher_noWatchersFeature(t *testing.T) {
test_helpers.SkipIfWatchersSupported(t)

Expand Down Expand Up @@ -4149,13 +4215,13 @@ func runTestMain(m *testing.M) int {
startOpts.MemtxUseMvccEngine = !isStreamUnsupported

inst, err := test_helpers.StartTarantool(startOpts)
defer test_helpers.StopTarantoolWithCleanup(inst)

if err != nil {
log.Printf("Failed to prepare test tarantool: %s", err)
return 1
}

defer test_helpers.StopTarantoolWithCleanup(inst)

return m.Run()
}

Expand Down
Loading