(goroutine泄漏)http.TimeoutHandler不会杀死相应的ServeHTTP goroutine

Timeout handler moves ServeHTTP execution on a new goroutine, but not able to kill that goroutine after the timer ends. On every request, it creates two goroutines, but ServeHTTP goroutines never kill with context.

Not able to find a way to kill goroutines.

Edit For-loop with time.Sleep function, represents huge computation which goes beyond our timer. Can replace it with any other function.

package main

import (
    "fmt"
    "io"
    "net/http"
    "runtime"
    "time"
)

type api struct{}

func (a api) ServeHTTP(w http.ResponseWriter, req *http.Request) {
    // For-loop block represents huge computation and usually takes more time
    // Can replace with any code
    i := 0
    for {
        if i == 500 {
            break
        }
        fmt.Printf("#goroutines: %d
", runtime.NumGoroutine())
        time.Sleep(1 * time.Second)
        i++
    }
    _, _ = io.WriteString(w, "Hello World!")
}

func main() {
    var a api
    s := http.NewServeMux()
    s.Handle("/", a)
    h := http.TimeoutHandler(s, 1*time.Second, `Timeout`)

    fmt.Printf("#goroutines: %d
", runtime.NumGoroutine())

    _ = http.ListenAndServe(":8080", h)
}

ServeHTTP goroutine should kill along with request context, normally which does not happen.

I found, if you do not have any way to reach to your channel then there is no way to kill or stop goroutine when it is running.

In the large computational task, you have to watch the channel on a specific interval or after specific task completion.

Use context.Context to instruct go-routines to abort their function. The go-routines, of course, have to listen for such cancelation events.

So for your code, do something like:

ctx := req.Context() // this will be implicitly canceled by your TimeoutHandler after 1s

i := 0
for {
    if i == 500 {
        break
    }

    // for any long wait (1s etc.) always check the state of your context
    select {
    case <-time.After(1 * time.Second): // no cancelation, so keep going
    case <-ctx.Done():
        fmt.Println("request context has been canceled:", ctx.Err())
        return // terminates go-routine
    }
    i++
}

Playground: https://play.golang.org/p/VEnW0vsItXm


Note: Context are designed to be chained - allowing for multiple levels of sub-tasks to be canceled in a cascading manner.

In a typical REST call one would initiate a database request. So, to ensure such a blocking and/or slow call completes in a timely manner, instead of using Query one should use QueryContext - passing in the http request's context as the first argument.