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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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 @@ -19,6 +19,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.0] - 2025-03-11

The release extends box.info responses and ConnectionPool.GetInfo return data.
Expand Down
3 changes: 3 additions & 0 deletions connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -1454,6 +1454,9 @@ func isFeatureInSlice(expected iproto.Feature, actualSlice []iproto.Feature) boo
//
// Since 1.10.0
func (conn *Connection) NewWatcher(key string, callback WatchCallback) (Watcher, error) {
if conn.c == nil {
return nil, ClientError{ErrConnectionNotReady, "client connection is not ready"}
}
// We need to check the feature because the IPROTO_WATCH request is
// asynchronous. We do not expect any response from a Tarantool instance
// That's why we can't just check the Tarantool response for an unsupported
Expand Down
47 changes: 47 additions & 0 deletions tarantool_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3550,6 +3550,53 @@ func TestConnection_NewWatcher(t *testing.T) {
}
}

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

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,
})
defer test_helpers.StopTarantoolWithCleanup(inst)
if err != nil {
t.Fatalf("Unable to start Tarantool: %s", err)
}

ctx, cancel := test_helpers.GetConnectContext()
defer cancel()

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)
}
defer conn.Close()
test_helpers.StopTarantool(inst)

for conn.ConnectedNow() {
time.Sleep(100 * time.Millisecond)
}
_, err = conn.NewWatcher("one", func(event WatchEvent) {})
assert.NotNil(t, err)
assert.ErrorContains(t, err, "client connection is not ready")

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

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

Expand Down
Loading