Skip to content

Commit f26e2e4

Browse files
committed
Merge pull request #116 from nats-io/race
Race Fixes for 1.5 use of GOMAXPROCS
2 parents 7620f4a + b30af11 commit f26e2e4

File tree

7 files changed

+41
-22
lines changed

7 files changed

+41
-22
lines changed

.travis.yml

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
language: go
22
go:
33
- 1.4
4+
- 1.5
45
install:
56
- DST=~/gopath/src/github.com/nats-io
67
- mkdir -p "$DST"
@@ -13,7 +14,7 @@ script:
1314
- ./travis/gofmt.sh
1415
- ./travis/govet.sh
1516
- go test -i -race ./...
16-
- GOMAXPROCS=1 go test -v -race ./...
17+
- go test -v -race ./...
1718
- ./travis/coveralls-script.sh
1819
env:
1920
global:

gnatsd.go

-2
Original file line numberDiff line numberDiff line change
@@ -113,13 +113,11 @@ func configureAuth(s *server.Server, opts *server.Options) {
113113
Username: opts.Username,
114114
Password: opts.Password,
115115
}
116-
117116
s.SetAuthMethod(auth)
118117
} else if opts.Authorization != "" {
119118
auth := &auth.Token{
120119
Token: opts.Authorization,
121120
}
122-
123121
s.SetAuthMethod(auth)
124122
}
125123
}

server/client.go

+5-2
Original file line numberDiff line numberDiff line change
@@ -313,7 +313,9 @@ func (c *client) sendErr(err string) {
313313
c.mu.Lock()
314314
if c.bw != nil {
315315
c.bw.WriteString(fmt.Sprintf("-ERR '%s'\r\n", err))
316-
c.pcd[c] = needFlush
316+
// Flush errors in place.
317+
c.bw.Flush()
318+
//c.pcd[c] = needFlush
317319
}
318320
c.mu.Unlock()
319321
}
@@ -326,12 +328,13 @@ func (c *client) sendOK() {
326328
}
327329

328330
func (c *client) processPing() {
331+
c.mu.Lock()
329332
c.traceInOp("PING", nil)
330333
if c.nc == nil {
334+
c.mu.Unlock()
331335
return
332336
}
333337
c.traceOutOp("PONG", nil)
334-
c.mu.Lock()
335338
c.bw.WriteString("PONG\r\n")
336339
err := c.bw.Flush()
337340
if err != nil {

server/route.go

+12-1
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,11 @@ func (s *Server) createRoute(conn net.Conn, rURL *url.URL) *client {
8888
r := &route{didSolicit: didSolicit}
8989
c := &client{srv: s, nc: conn, opts: clientOpts{}, typ: ROUTER, route: r}
9090

91+
// Grab JSON info string
92+
s.mu.Lock()
93+
info := s.routeInfoJSON
94+
s.mu.Unlock()
95+
9196
// Grab lock
9297
c.mu.Lock()
9398

@@ -104,7 +109,7 @@ func (s *Server) createRoute(conn net.Conn, rURL *url.URL) *client {
104109
}
105110

106111
// Send our info to the other side.
107-
s.sendInfo(c)
112+
s.sendInfo(c, info)
108113

109114
// Check for Auth required state for incoming connections.
110115
if s.routeInfo.AuthRequired && !didSolicit {
@@ -221,6 +226,9 @@ func (s *Server) broadcastToRoutes(proto string) {
221226
// broadcastSubscribe will forward a client subscription
222227
// to all active routes.
223228
func (s *Server) broadcastSubscribe(sub *subscription) {
229+
if s.numRoutes() == 0 {
230+
return
231+
}
224232
rsid := routeSid(sub)
225233
proto := fmt.Sprintf(subProto, sub.subject, sub.queue, rsid)
226234
s.broadcastToRoutes(proto)
@@ -229,6 +237,9 @@ func (s *Server) broadcastSubscribe(sub *subscription) {
229237
// broadcastUnSubscribe will forward a client unsubscribe
230238
// action to all active routes.
231239
func (s *Server) broadcastUnSubscribe(sub *subscription) {
240+
if s.numRoutes() == 0 {
241+
return
242+
}
232243
rsid := routeSid(sub)
233244
maxStr := _EMPTY_
234245
// Set max if we have it set and have not tripped auto-unsubscribe

server/server.go

+15-11
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,9 @@ func New(opts *Options) *Server {
115115

116116
// Sets the authentication method
117117
func (s *Server) SetAuthMethod(authMethod Auth) {
118+
s.mu.Lock()
119+
defer s.mu.Unlock()
120+
118121
s.info.AuthRequired = true
119122
s.auth = authMethod
120123

@@ -385,6 +388,12 @@ func (s *Server) StartHTTPMonitoring() {
385388
func (s *Server) createClient(conn net.Conn) *client {
386389
c := &client{srv: s, nc: conn, opts: defaultOpts, mpay: s.info.MaxPayload}
387390

391+
// Grab JSON info string
392+
s.mu.Lock()
393+
info := s.infoJSON
394+
authRequired := s.info.AuthRequired
395+
s.mu.Unlock()
396+
388397
// Grab lock
389398
c.mu.Lock()
390399

@@ -393,15 +402,15 @@ func (s *Server) createClient(conn net.Conn) *client {
393402

394403
c.Debugf("Client connection created")
395404

396-
// Send our information.
397-
s.sendInfo(c)
398-
399405
// Check for Auth
400-
if s.info.AuthRequired {
406+
if authRequired {
401407
ttl := secondsToDuration(s.opts.AuthTimeout)
402408
c.setAuthTimer(ttl)
403409
}
404410

411+
// Send our information.
412+
s.sendInfo(c, info)
413+
405414
// Unlock to register
406415
c.mu.Unlock()
407416

@@ -414,13 +423,8 @@ func (s *Server) createClient(conn net.Conn) *client {
414423
}
415424

416425
// Assume the lock is held upon entry.
417-
func (s *Server) sendInfo(c *client) {
418-
switch c.typ {
419-
case CLIENT:
420-
c.nc.Write(s.infoJSON)
421-
case ROUTER:
422-
c.nc.Write(s.routeInfoJSON)
423-
}
426+
func (s *Server) sendInfo(c *client, info []byte) {
427+
c.nc.Write(info)
424428
}
425429

426430
func (s *Server) checkClientAuth(c *client) bool {

test/pedantic_test.go

+6-4
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@ import (
1010

1111
func runPedanticServer() *server.Server {
1212
opts := DefaultTestOptions
13+
14+
opts.NoLog = false
15+
opts.Trace = true
16+
1317
opts.Port = PROTO_TEST_PORT
1418
return RunServer(&opts)
1519
}
@@ -23,8 +27,7 @@ func TestPedanticSub(t *testing.T) {
2327

2428
send := sendCommand(t, c)
2529
expect := expectCommand(t, c)
26-
doConnect(t, c, true, true, false)
27-
expect(okRe)
30+
doConnect(t, c, false, true, false)
2831

2932
// Ping should still be same
3033
send("PING\r\n")
@@ -69,8 +72,7 @@ func TestPedanticPub(t *testing.T) {
6972

7073
send := sendCommand(t, c)
7174
expect := expectCommand(t, c)
72-
doConnect(t, c, true, true, false)
73-
expect(okRe)
75+
doConnect(t, c, false, true, false)
7476

7577
// Ping should still be same
7678
send("PING\r\n")

test/test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2012-2014 Apcera Inc. All rights reserved.
1+
// Copyright 2012-2015 Apcera Inc. All rights reserved.
22

33
package test
44

0 commit comments

Comments
 (0)