Skip to content

Commit 1e3091f

Browse files
bartle-stripeDivjot Arora
authored andcommitted
Lock serversLock in Topology.String.
GODRIVER-1302
1 parent b89d991 commit 1e3091f

File tree

2 files changed

+26
-0
lines changed

2 files changed

+26
-0
lines changed

x/mongo/driver/topology/topology.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -591,6 +591,8 @@ func (t *Topology) addServer(addr address.Address) error {
591591
func (t *Topology) String() string {
592592
desc := t.Description()
593593
str := fmt.Sprintf("Type: %s\nServers:\n", desc.Kind)
594+
t.serversLock.Lock()
595+
defer t.serversLock.Unlock()
594596
for _, s := range t.servers {
595597
str += s.String() + "\n"
596598
}

x/mongo/driver/topology/topology_test.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -455,3 +455,27 @@ func TestMinPoolSize(t *testing.T) {
455455
t.Errorf("topology.Connect shouldn't error. got: %v", err)
456456
}
457457
}
458+
459+
func TestTopology_String_Race(t *testing.T) {
460+
ch := make(chan bool)
461+
topo := &Topology{
462+
servers: make(map[address.Address]*Server),
463+
}
464+
465+
go func() {
466+
topo.serversLock.Lock()
467+
srv := &Server{}
468+
srv.desc.Store(description.Server{})
469+
topo.servers[address.Address("127.0.0.1:27017")] = srv
470+
topo.serversLock.Unlock()
471+
ch <- true
472+
}()
473+
474+
go func() {
475+
topo.String()
476+
ch <- true
477+
}()
478+
479+
<-ch
480+
<-ch
481+
}

0 commit comments

Comments
 (0)