Skip to content
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

Verify the Pool connection is not nil before attempting to close it #480

Merged
merged 1 commit into from
Mar 12, 2020

Conversation

cquon
Copy link
Contributor

@cquon cquon commented Mar 12, 2020

This checks to make sure the Pool's connection is not nil before attempting to close it (previously it was not checking so could get a panic for a nil deference. If looking at the Pool connection initialization code in pool.go:

// NewPool creates a new connection pool for the given host
func NewPool(host Host, opts *ConnectOpts) (*Pool, error) {
	initialCap := opts.InitialCap
	if initialCap <= 0 {
		// Fallback to MaxIdle if InitialCap is zero, this should be removed
		// when MaxIdle is removed
		initialCap = opts.MaxIdle
	}

	maxOpen := opts.MaxOpen
	if maxOpen <= 0 {
		maxOpen = 1
	}

	conns := make([]*Connection, maxOpen)
	var err error
	for i := 0; i < opts.InitialCap; i++ {
		conns[i], err = NewConnection(host.String(), opts)
		if err != nil {
			return nil, err
		}
	}

	return &Pool{
		conns:   conns,
		pointer: -1,
		host:    host,
		opts:    opts,
		closed:  poolIsNotClosed,
	}, nil
}

It can be seen that if the opts.InitialCap < opts.MaxOpen, then we allocate a connection slice of size opts.MaxOpen where only opts.InitialCap are initially initialized. If we then close it later, it will cause the panic referred to above (can see stack trace in issue).

Addresses: #479

@CMogilko CMogilko merged commit c38c1ce into rethinkdb:master Mar 12, 2020
@CMogilko
Copy link
Member

thanks for your contribution

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants