Skip to content

Commit

Permalink
[FIXED] Possible data race in routez when route disconnects (nats-io#540
Browse files Browse the repository at this point in the history
)

* [FIXED] Possible data race in routez when route disconnects

Resolves nats-io#539
  • Loading branch information
kozlovic authored Jul 11, 2017
1 parent 1ca5e57 commit 56649b3
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 4 deletions.
8 changes: 4 additions & 4 deletions server/monitor.go
Original file line number Diff line number Diff line change
Expand Up @@ -320,13 +320,13 @@ func (s *Server) HandleRoutez(w http.ResponseWriter, r *http.Request) {
}
ri.Subs = castToSliceString(sublist)
}
r.mu.Unlock()

if ip, ok := r.nc.(*net.TCPConn); ok {
addr := ip.RemoteAddr().(*net.TCPAddr)
switch conn := r.nc.(type) {
case *net.TCPConn, *tls.Conn:
addr := conn.RemoteAddr().(*net.TCPAddr)
ri.Port = addr.Port
ri.IP = addr.IP.String()
}
r.mu.Unlock()
rs.Routes = append(rs.Routes, ri)
}
s.mu.Unlock()
Expand Down
38 changes: 38 additions & 0 deletions server/monitor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1397,3 +1397,41 @@ func TestMonitorHandler(t *testing.T) {
t.Fatal("HTTP Handler should be nil")
}
}

func TestMonitorRoutezRace(t *testing.T) {
resetPreviousHTTPConnections()
srvAOpts := DefaultMonitorOptions()
srvAOpts.Cluster.Port = -1
srvA := RunServer(srvAOpts)
defer srvA.Shutdown()

srvBOpts := nextServerOpts(srvAOpts)
srvBOpts.Routes = RoutesFromStr(fmt.Sprintf("nats://127.0.0.1:%d", srvA.ClusterAddr().Port))

url := fmt.Sprintf("http://127.0.0.1:%d/", srvA.MonitorAddr().Port)
doneCh := make(chan struct{})
go func() {
defer func() {
doneCh <- struct{}{}
}()
for i := 0; i < 10; i++ {
time.Sleep(10 * time.Millisecond)
srvB := RunServer(srvBOpts)
time.Sleep(20 * time.Millisecond)
srvB.Shutdown()
}
}()
done := false
for !done {
if resp, err := http.Get(url + "routez"); err != nil {
time.Sleep(10 * time.Millisecond)
} else {
resp.Body.Close()
}
select {
case <-doneCh:
done = true
default:
}
}
}

0 comments on commit 56649b3

Please sign in to comment.