rabbitmq / rabbitmq-stream-go-client

A client library for RabbitMQ streams
MIT License
161 stars 13 forks source link

Environment.QueryOffset hang/deadlock #273

Closed HustonMmmavr closed 3 months ago

HustonMmmavr commented 4 months ago

Describe the bug

Hello! I met the scenario when call to the Environment.QueryOffset may hang:

package main

import (
    "fmt"
    "os"

    "github.com/rabbitmq/rabbitmq-stream-go-client/pkg/stream"
)

func CheckErr(err error) {
    if err != nil {
        fmt.Printf("%s ", err)
        os.Exit(1)
    }
}

func main() {
    env, err := stream.NewEnvironment(
        stream.NewEnvironmentOptions().
            SetHost("localhost").
            SetPort(5552).
            SetUser("guest").
            SetPassword("guest"))
    CheckErr(err)

    off, err := env.QueryOffset("consumer", "stream")
    fmt.Println(off, err)
}

Here is a description: Environment.QueryOffset calls Client.queryOffset which internally:

  1. calls Client.handleWriteWithResponse with removeResponse argument passed as false (so RemoveResponseById won't be called and closing of response.data channel wouldn't be performed). The last function calls waitCodeWithDefaultTimeOut
  2. right after returning from the handleWriteWithResponse queryOffset starts accepting the offset value on the channel here is a code on the next line.

Thus there may occur a deadlock, when the response was finished by timeout (for example rabbit service unavailable), but the Client.queryOffset method still waiting the data on channel which wouldn't be closed. Seems that error check should be added before reading from channel, like it's done at Client.open. I suppose that next methods also may have a deadlock: Client.StreamStats and Client.queryPublisherSequence

Reproduction steps

  1. start rabbitmq server
  2. setup breakpoint on this line
  3. run the code from the description and wait on the breakpoint
  4. stop the rabbitmq server
  5. continue code execution

Expected behavior

No hang

Additional context

No response

Gsantomaggio commented 4 months ago

@HustonMmmavr Thank you for the detailed report.

Feel free to propose a PR with the fix.