mirror of
https://github.com/gogrlx/nats-server.git
synced 2026-04-17 11:24:44 -07:00
Merge pull request #1385 from nats-io/fix_close_conn
[FIXED] Early closed connection may linger in the server
This commit is contained in:
@@ -2038,3 +2038,34 @@ func TestCloseConnectionLogsReason(t *testing.T) {
|
||||
t.Fatal("Log does not contain closed reason")
|
||||
}
|
||||
}
|
||||
|
||||
func TestCloseConnectionVeryEarly(t *testing.T) {
|
||||
o := DefaultOptions()
|
||||
s := RunServer(o)
|
||||
defer s.Shutdown()
|
||||
|
||||
// The issue was with a connection that would break right when
|
||||
// server was sending the INFO. Creating a bare TCP connection
|
||||
// and closing it right away won't help reproduce the problem.
|
||||
// So testing in 2 steps.
|
||||
|
||||
// Get a normal TCP connection to the server.
|
||||
c, err := net.Dial("tcp", fmt.Sprintf("127.0.0.1:%d", o.Port))
|
||||
if err != nil {
|
||||
s.mu.Unlock()
|
||||
t.Fatalf("Unable to create tcp connection")
|
||||
}
|
||||
// Now close it.
|
||||
c.Close()
|
||||
|
||||
// Wait that num clients falls to 0.
|
||||
checkClientsCount(t, s, 0)
|
||||
|
||||
// Call again with this closed connection. Alternatively, we
|
||||
// would have to call with a fake connection that implements
|
||||
// net.Conn but returns an error on Write.
|
||||
s.createClient(c)
|
||||
|
||||
// This connection should not have been added to the server.
|
||||
checkClientsCount(t, s, 0)
|
||||
}
|
||||
|
||||
@@ -1793,6 +1793,12 @@ func (s *Server) createClient(conn net.Conn) *client {
|
||||
// The connection may have been closed
|
||||
if c.isClosed() {
|
||||
c.mu.Unlock()
|
||||
// If it was due to TLS timeout, teardownConn() has already been called.
|
||||
// Otherwise, if connection was marked as closed while sending the INFO,
|
||||
// we need to call teardownConn() directly here.
|
||||
if !info.TLSRequired {
|
||||
c.teardownConn()
|
||||
}
|
||||
return c
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user