Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Odd behavior: continues to serve content after listener.Close() #39

Closed
AlasdairF opened this issue Jan 14, 2016 · 1 comment
Closed
Labels

Comments

@AlasdairF
Copy link

Hello,

I'm attempting to do a graceful shutdown by closing the listener. However, it doesn't work; I can continue to request new pages and they continue to be served, even after s.Serve returns, until the entire application ends.

    package main

    import (
        "github.com/valyala/fasthttp"
        "fmt"
        "log"
        "net"
        "time"
    )

    var listener net.Listener

    func root(ctx *fasthttp.RequestCtx) {
        if string(ctx.Path()) == `/close` {
            fmt.Fprint(ctx, "CLOSE\n")
            err := listener.Close()
            if err != nil {
                log.Println(err)
            }
        }
        fmt.Fprintf(ctx, "Hi there! RequestURI is %q", ctx.RequestURI())
    }

    func main() {
        var err error
        listener, err = net.Listen(`tcp`, `:8081`)
        if err != nil {
            log.Fatal(err)
        }
        s := &fasthttp.Server{
            Handler: root,
        }
        err = s.Serve(listener)
        if err != nil {
            log.Println(err)
        }
        log.Println(`Exiting in 30 seconds`)
        time.Sleep(time.Second * 30)
        log.Println(`Exiting`)
    }
@valyala
Copy link
Owner

valyala commented Jan 14, 2016

Server.Serve returns when listener is closed, so new connections aren't accepted. But the client (web browser) still holds old keep-alive connection, which is served by fasthttp regardless of the original listener state until the connection is closed.

BTW, net/http.Server does the same thing. Try it:

package main

import (
        "fmt"
        "log"
        "net"
        "net/http"
        "time"
)

var listener net.Listener

func root(w http.ResponseWriter, r *http.Request) {
        if r.URL.Path == `/close` {
                fmt.Fprint(w, "CLOSE\n")
                err := listener.Close()
                if err != nil {
                        log.Println(err)
                }
        }
        fmt.Fprintf(w, "Hi there! RequestURI is %q", r.RequestURI)
}

func main() {
        var err error
        listener, err = net.Listen(`tcp`, `:8081`)
        if err != nil {
                log.Fatal(err)
        }
        s := &http.Server{
                Handler: http.HandlerFunc(root),
        }
        err = s.Serve(listener)
        if err != nil {
                log.Println(err)
        }
        log.Println(`Exiting in 30 seconds`)
        time.Sleep(time.Second * 30)
        log.Println(`Exiting`)
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants