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

Capture when mongoSocket.conn is nil to prevent panics #38

Merged
merged 2 commits into from
May 14, 2019
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
9 changes: 9 additions & 0 deletions server.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ package mgo

import (
"errors"
"fmt"
"net"
"sort"
"sync"
Expand Down Expand Up @@ -198,6 +199,14 @@ func (server *mongoServer) Connect(timeout time.Duration, socket *mongoSocket) e
logf("Connection to %s failed: %v", server.Addr, err.Error())
return err
}

if conn == nil {
// if a dialer is incorrectly implemented, it may return (nil, nil)
// which will cause a panic downstream
logf("Received nil connection to %s", server.Addr)
return fmt.Errorf("received nil connection to %s", server.Addr)
}

logf("Connection to %s established.", server.Addr)

stats.conn(+1, master)
Expand Down
4 changes: 3 additions & 1 deletion socket.go
Original file line number Diff line number Diff line change
Expand Up @@ -350,7 +350,9 @@ func (socket *mongoSocket) kill(err error, abend bool) {

logf("Socket %p to %s: closing: %s (abend=%v)", socket, socket.addr, err.Error(), abend)
socket.dead = err
socket.conn.Close()
if socket.conn != nil {
socket.conn.Close()
}
stats.socketsAlive(-1)
replyFuncs := socket.replyFuncs
socket.replyFuncs = make(map[uint32]replyFunc)
Expand Down