go-zeromq / zmq4

[WIP] Pure-Go implementation of ZeroMQ-4
BSD 3-Clause "New" or "Revised" License
341 stars 56 forks source link

Send timeout does not works #147

Closed egorse closed 9 months ago

egorse commented 10 months ago

Even default 5 minutes timeout not working on pair push/pull sockets over ipc://@ if reader does not reads

golang 1.21.4, zmq4 v0.16.0, Ubuntu 20.04.6 LTS

package main

import (
    "context"
    "log"
    "time"

    "github.com/go-zeromq/zmq4"
)

func main() {
    ep := "ipc://@fifo"
    ctx, cancel := context.WithCancel(context.Background())
    defer cancel()
    push := zmq4.NewPush(ctx)
    pull := zmq4.NewPull(ctx)

    if err := push.Listen(ep); err != nil {
        log.Fatal(err)
    }
    if err := pull.Dial(ep); err != nil {
        log.Fatal(err)
    }

    rx := func() {
        for {
            _, err := pull.Recv()
            if err != nil {
                log.Fatal(err)
            }
            for {
                time.Sleep(1 * time.Second)
            }
        }
    }
    tx := func() {
        n := uint64(0)
        msg := zmq4.NewMsgString("this is just test message. this is just test message. this is just test message. this is just test message. this is just test message. this is just test message. this is just test message. this is just test message. ")
        for {
            n++
            if n == 117 {
                _ = n
            }
            if err := push.Send(msg); err != nil {
                log.Fatal(err)
            }
            log.Printf("tx %v", n)
        }
    }

    go rx()
    tx()

}
egorse commented 10 months ago

Seems https://github.com/go-zeromq/zmq4/blob/e16dc3e41eac7ae42c39a106e3d3ef6512be4245/msgio.go#L168 does not respect ctx cancellation

sbinet commented 10 months ago

Seems [like] mwriter.write does not respect ctx cancellation

I am not saying that isn't the case, but why do you think mwriter.write does not respect ctx cancellation ?

egorse commented 10 months ago

The ctx passed to it has default 5 minutes timeout - but Send (as in example above) wont return control ever (I was waiting for over 10 minutes. And we step into problems on real app where it was stuck for hours), if reader does not read nor close the reading side. If it would respect parent context - it will return deadline exceeded or something like that

egorse commented 10 months ago

I think the problem is that errgroup.WithContext does not imply the error group would be aborted by parent context cancellation - it must be done explicitly in the Do payload