Why do the client and server cause a data race?

Viewed 123

I'm trying to create a TCP relay using channels as a basic PubSub. My goal is to relay one TCP stream to many clients (one to many). I haven't been able to fix a data race between the client and server connections. I would be grateful for any insight as to why the data race occurs between the client and server connections?

I think the pubsub part is OK. It was adapted from the following blog:

https://eli.thegreenplace.net/2020/pubsub-using-channels-in-go/

According to the data race warning, the race occurs in the main function code block below. I put some comments on the lines that cause the data race. I thought it would be possible to run a server and client concurrently, am I mistaken?

package main

import (
    "flag"
    "net"
    "os"
    "sync"
)

var (
    laddr = flag.String("l", "", "listen address (:port)")
    raddr = flag.String("r", "", "remote address (host:port)")
)

type Sub struct {
    topic string
    id    int64
}

type Pubsub struct {
    mu      sync.RWMutex
    subs    map[Sub]chan []byte
    closed  bool
    counter int64
}

func NewPubsub() *Pubsub {
    ps := &Pubsub{}
    ps.subs = make(map[Sub]chan []byte)
    ps.closed = false
    return ps
}

func (ps *Pubsub) Close() {
    ps.mu.Lock()
    defer ps.mu.Unlock()

    if !ps.closed {
        ps.closed = true
        for _, sub := range ps.subs {
            close(sub)
        }
    }
}

func (ps *Pubsub) Subscribe(topic string) (<-chan []byte, Sub) {
    ps.mu.Lock()
    defer ps.mu.Unlock()

    // initialze the subscription
    sub := Sub{topic: topic, id: ps.counter}

    // Add the subscription to the map
    ch := make(chan []byte, 1)
    ps.subs[sub] = ch

    // Increment the counter
    ps.counter++

    return ch, sub
}

func (ps *Pubsub) Unsubscribe(s Sub) {
    ps.mu.Lock()
    defer ps.mu.Unlock()
    delete(ps.subs, s)
}

func (ps *Pubsub) Publish(topic string, msg []byte) {
    ps.mu.RLock()
    defer ps.mu.RUnlock()

    for sub, ch := range ps.subs {
        if sub.topic == topic {
            ch <- msg
        }
    }
}

func main() {
    flag.Parse()
    if *laddr == "" || *raddr == "" {
        flag.PrintDefaults()
        os.Exit(1)
    }

    ps := NewPubsub()

    publisher := func(topic string) {
        remote, err := net.Dial("tcp", *raddr)
        if err != nil {
            return
        }
        buf := make([]byte, 2048)
        for {
            n, _ := remote.Read(buf)  // *** RACE HERE ***
            ps.Publish(topic, buf[:n])
        }
    }

    go publisher("relay")

    subscriber := func(conn net.Conn, ch <-chan []byte) {
        for i := range ch {
            conn.Write([]byte(i))  // *** RACE HERE ***
        }
    }

    ln, err := net.Listen("tcp", *laddr)
    if err != nil {
        return
    }

    for {
        conn, err := ln.Accept()
        if err != nil {
            continue
        }
        ch, _ := ps.Subscribe("relay")
        go subscriber(conn, ch)

    }

}

The data race output when using "go run -race pubsub.go" is shown below.

The data race warning does not occur until the first client connects to the listening server's port.

I have not seen any other types of data races while this program runs. But when I relay binary data, bytes are infrequently either corrupted or missing, suggesting that their may be other issues with my naïve implementation.

==================
WARNING: DATA RACE
Write at 0x00c0000f8000 by goroutine 7:
  internal/race.WriteRange()
      /usr/local/go/src/internal/race/race.go:49 +0xaa
  syscall.Read()
      /usr/local/go/src/syscall/syscall_unix.go:190 +0x89
  internal/poll.ignoringEINTRIO()
      /usr/local/go/src/internal/poll/fd_unix.go:581 +0x1c8
  internal/poll.(*FD).Read()
      /usr/local/go/src/internal/poll/fd_unix.go:162 +0x17c
  net.(*netFD).Read()
      /usr/local/go/src/net/fd_posix.go:55 +0x68
  net.(*conn).Read()
      /usr/local/go/src/net/net.go:183 +0xeb
  net.(*TCPConn).Read()
      <autogenerated>:1 +0x69
  main.main.func1()
      /pubsub/pubsub.go:101 +0x154

Previous read at 0x00c0000f8000 by goroutine 9:
  internal/race.ReadRange()
      /usr/local/go/src/internal/race/race.go:45 +0xb0
  syscall.Write()
      /usr/local/go/src/syscall/syscall_unix.go:215 +0x94
  internal/poll.ignoringEINTRIO()
      /usr/local/go/src/internal/poll/fd_unix.go:581 +0x16e
  internal/poll.(*FD).Write()
      /usr/local/go/src/internal/poll/fd_unix.go:274 +0x294
  net.(*netFD).Write()
      /usr/local/go/src/net/fd_posix.go:73 +0x68
  net.(*conn).Write()
      /usr/local/go/src/net/net.go:195 +0xeb
  net.(*TCPConn).Write()
      <autogenerated>:1 +0x69
  main.main.func2()
      /pubsub/pubsub.go:110 +0x84

Goroutine 7 (running) created at:
  main.main()
      /pubsub/pubsub.go:106 +0x288

Goroutine 9 (running) created at:
  main.main()
      /pubsub/pubsub.go:125 +0x38f
==================
1 Answers

Quick fix:

    // buf := make([]byte, 2048) // <- move this ...
    for {
        buf := make([]byte, 2048) // <- ... to here
        n, _ := remote.Read(buf)
        ps.Publish(topic, buf[:n])
    }

Why is this broken? Since a single (constant) buf is passed via a channel to multiple subscribers (readers) - when the next for iteration occurs - those readers will get corrupt racy data.

Creating a unique buffer per iteration will ensure no new writes will corrupt old messages sent & still being processed by subscribers.

Related