From 2bcf2586ed1d36078e2499ac992c24beb7f3d016 Mon Sep 17 00:00:00 2001 From: Divjot Arora Date: Thu, 20 Aug 2020 14:56:21 -0400 Subject: [PATCH] GODRIVER-1719 Resolve data race between mongotest and proxy dialer --- mongo/integration/mtest/mongotest.go | 2 +- mongo/integration/mtest/proxy_dialer.go | 13 +++++++++++++ 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/mongo/integration/mtest/mongotest.go b/mongo/integration/mtest/mongotest.go index 29e97564b8..f068fd6f7c 100644 --- a/mongo/integration/mtest/mongotest.go +++ b/mongo/integration/mtest/mongotest.go @@ -355,7 +355,7 @@ func (t *T) GetProxiedMessages() []*ProxyMessage { if t.proxyDialer == nil { return nil } - return t.proxyDialer.messages + return t.proxyDialer.Messages() } // ClearEvents clears the existing command monitoring events. diff --git a/mongo/integration/mtest/proxy_dialer.go b/mongo/integration/mtest/proxy_dialer.go index f89324ffb7..f0e63274ea 100644 --- a/mongo/integration/mtest/proxy_dialer.go +++ b/mongo/integration/mtest/proxy_dialer.go @@ -129,6 +129,19 @@ func (p *proxyDialer) storeReceivedMessage(wm []byte, addr string) error { return nil } +// Messages returns a slice of proxied messages. This slice is a copy of the messages proxied so far and will not be +// updated for messages proxied after this call. +func (p *proxyDialer) Messages() []*ProxyMessage { + p.Lock() + defer p.Unlock() + + copiedMessages := make([]*ProxyMessage, 0, len(p.messages)) + for _, msg := range p.messages { + copiedMessages = append(copiedMessages, msg) + } + return copiedMessages +} + // proxyConn is a net.Conn that wraps a network connection. All messages sent/received through a proxyConn are stored // in the associated proxyDialer and are forwarded over the wrapped connection. Errors encountered when parsing and // storing wire messages are wrapped to add context, while errors returned from the underlying network connection are