soheilhy / cmux

Connection multiplexer for GoLang: serve different services on the same port!
Apache License 2.0
2.58k stars 199 forks source link

Close not working properly on gRPC graceful stop #89

Open elvizlai opened 3 years ago

elvizlai commented 3 years ago

example code:

package main

import (
    "fmt"
    "net"
    "os"
    "os/signal"
    "syscall"
    "time"

    "github.com/soheilhy/cmux"
    "google.golang.org/grpc"
)

func main() {
    lis, err := net.Listen("tcp", ":8080")
    if err != nil {
        panic(err)
    }

    mux := cmux.New(lis)
    grpcL := mux.Match(cmux.HTTP2HeaderField("content-type", "application/grpc"))
    _ = grpcL

    s := grpc.NewServer()
    // xxx.RegisterXXXServiceServer(s, XXXIMPL)

    go func() {
        // FIXME: using muxed grpcL here can not graceful stop
        err = s.Serve(lis)
        fmt.Println(err)
    }()

    fmt.Println("wait for sig")
    c := make(chan os.Signal, 1)
    signal.Notify(c, []os.Signal{syscall.SIGTERM, syscall.SIGQUIT, syscall.SIGINT}...)
    <-c

        // FIXME: hangs on when start server with grpcL
    s.GracefulStop()

    fmt.Println("stopped")
    time.Sleep(time.Second * 1)
}
jgraettinger commented 3 years ago

See https://github.com/soheilhy/cmux/issues/76