#Help with a race condition?

8 messages · Page 1 of 1 (latest)

tidal spruce
#

I have the following code, it exits with a race condition with the data coming out of the lr.C channel, and I don't understand why? It's a basic telnet client to log in to a game server.

mellow bronze
#

```go
your code
```

tidal spruce
#

import (
    "bufio"
    "fmt"
    "os"
    "sync"
    "time"

    "github.com/aprice/telnet"
    "github.com/aprice/telnet/linereader"
)

func main() {

    //    conn, err := telnet.Dial("kallistimud.com:4069", ExposeMSDP)
    conn, err := telnet.Dial("kallistimud.com:4000")
    if err != nil {
        return
    }

    lr := linereader.New()
    //go func() {
    //    fmt.Printf("crank up readline\n")
    //    err := lr.ReadLines(conn)
    //    fmt.Printf("finished readline: %v\n", err)
    //}()
    go lr.ReadLines(conn)

    reader := bufio.NewReader(os.Stdin)
    go func() {
        for {
            inp, err := reader.ReadString('\n')
            if len(inp) == 0 && err != nil {
                break
            }
            conn.Write([]byte(inp))
        }
    }()

    wg := new(sync.WaitGroup)
    wg.Add(1)
    go func() {
        defer wg.Done()

        for {
            msg := <-lr.C
            fmt.Printf("%v\n", string(msg))
            time.Sleep(10 * time.Millisecond)
        }
    }()

    wg.Wait()
}
slender lion
#

i think its because theres 2 goroutines that are trying to read from the "lr.c" channel simultaneously

tidal spruce
#

yeah, i found a fork of that module, am trying it now

#

it seems to work better, found a bug there too

#

surprised there's not a better telnet-with-IAC support module

sleek night
#

just ask ChatGPT (openai)