Fix datarace for WebRTCPeer.closed

The race condition occurs because concurrent goroutines are intermixing
reads and writes of `WebRTCPeer.closed`.

Spotted when integrating Snowflake inside OONI in
https://github.com/ooni/probe-cli/pull/373.
This commit is contained in:
Cecylia Bocovich 2021-06-17 16:36:50 -04:00
parent ed2d5df87d
commit ddcdfc4f09
3 changed files with 23 additions and 11 deletions

View file

@ -83,7 +83,7 @@ func (p *Peers) Pop() *WebRTCPeer {
if !ok {
return nil
}
if snowflake.closed {
if snowflake.Closed() {
continue
}
// Set to use the same rate-limited traffic logger to keep consistency.
@ -110,7 +110,7 @@ func (p *Peers) purgeClosedPeers() {
next := e.Next()
conn := e.Value.(*WebRTCPeer)
// Purge those marked for deletion.
if conn.closed {
if conn.Closed() {
p.activePeers.Remove(e)
}
e = next