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

net/http: Handler read doesn't error on context cancel #20831

Closed
yonderblue opened this issue Jun 28, 2017 · 4 comments
Closed

net/http: Handler read doesn't error on context cancel #20831

yonderblue opened this issue Jun 28, 2017 · 4 comments

Comments

@yonderblue
Copy link

Please answer these questions before submitting your issue. Thanks!

What version of Go are you using (go version)?

1.8

What operating system and processor architecture are you using (go env)?

linux amd64

What did you do?

package main

import (
	"bytes"
	"context"
	"fmt"
	"io"
	"net/http"
	"time"
)

func handle(w http.ResponseWriter, r *http.Request) {
	go func() {
		<-r.Context().Done()
		fmt.Println("HANDLER CTX, err:", r.Context().Err())
	}()

	_, pw := io.Pipe() //just to block the reading of r.Body
	fmt.Println("HANDLER BEFORE COPY()")
	_, err := io.Copy(pw, r.Body)
	fmt.Println("HANDLER AFTER COPY(), err:", err)
}

func serve() {
	if err := http.ListenAndServe(":8080", http.HandlerFunc(handle)); err != nil {
		panic(err)
	}
}

func main() {
	go serve()

	r, err := http.NewRequest(http.MethodPost, "http://127.0.0.1:8080", bytes.NewReader([]byte("hello")))
	if err != nil {
		panic(err)
	}

	ctx, cancel := context.WithTimeout(context.Background(), time.Second)
	defer cancel()

	_, err = http.DefaultClient.Do(r.WithContext(ctx))
	if err != nil {
		fmt.Println("CLIENT DO(), err:", err)
	}
	time.Sleep(time.Second * 2)
}

What did you expect to see?

The io.Copy unblock with an error.

What did you see instead?

io.Copy never unblocks.

@bradfitz
Copy link
Contributor

This isn't a net/http issue.

The r.Body does fail on context cancel. The problem is that the write to your io.PipeWriter blocks forever.

If anything, this bug is that there's no way to create an io.Pipe pair with an associated context.

@bradfitz
Copy link
Contributor

I'm going to close this, since there's no demonstrated http bug here.

But feel free to file a feature request bug about adding context support to io.Pipe.

Let me know if I misunderstood this bug, though, and we can reopen.

@yonderblue
Copy link
Author

Ah I see thanks
So this does work which is closer to our case for anyone interested

package main

import (
	"context"
	"fmt"
	"io"
	"io/ioutil"
	"net/http"
	"time"
)

func handle(w http.ResponseWriter, r *http.Request) {
	go func() {
		<-r.Context().Done()
		fmt.Println("HANDLER CTX, err:", r.Context().Err())
	}()

	f, err := ioutil.TempFile("", "testing_")
	if err != nil {
		panic(err)
	}
	defer f.Close()

	fmt.Println("HANDLER BEFORE COPY()")
	_, err = io.Copy(f, r.Body)
	fmt.Println("HANDLER AFTER COPY(), err:", err)
}

func serve() {
	if err := http.ListenAndServe(":8080", http.HandlerFunc(handle)); err != nil {
		panic(err)
	}
}

func main() {
	go serve()

	pr, pw := io.Pipe()
	go func() {
		for {
			_, err := pw.Write([]byte("hi"))
			if err == io.ErrClosedPipe {
				fmt.Println("pipe closed")
				return
			}
			if err != nil {
				panic(err)
			}
		}
	}()

	time.Sleep(time.Second)

	r, err := http.NewRequest(http.MethodPost, "http://127.0.0.1:8080", pr)
	if err != nil {
		panic(err)
	}

	ctx, cancel := context.WithTimeout(context.Background(), time.Second)
	defer cancel()

	_, err = http.DefaultClient.Do(r.WithContext(ctx))
	if err != nil {
		fmt.Println("CLIENT DO(), err:", err)
	}
	time.Sleep(time.Second * 2)
}

@yonderblue
Copy link
Author

This also works (http2), which the client doesn't seem to use without the line http2.ConfigureTransport(tr)

package main

import (
	"context"
	"crypto/tls"
	"fmt"
	"io"
	"io/ioutil"
	"net/http"
	"time"

	"golang.org/x/net/http2"
)

func handle(w http.ResponseWriter, r *http.Request) {
	fmt.Println(r.Proto)
	go func() {
		<-r.Context().Done()
		fmt.Println("HANDLER CTX, err:", r.Context().Err())
	}()

	f, err := ioutil.TempFile("", "testing_")
	if err != nil {
		panic(err)
	}
	defer f.Close()

	fmt.Println("HANDLER BEFORE COPY()")
	_, err = io.Copy(f, r.Body)
	fmt.Println("HANDLER AFTER COPY(), err:", err)
}

func serve() {
	if err := http.ListenAndServeTLS(":443", "server.crt", "server.key", http.HandlerFunc(handle)); err != nil {
		panic(err)
	}
}

func main() {
	go serve()

	pr, pw := io.Pipe()
	go func() {
		for {
			_, err := pw.Write([]byte("hi"))
			if err == io.ErrClosedPipe {
				fmt.Println("pipe closed")
				return
			}
			if err != nil {
				panic(err)
			}
		}
	}()

	time.Sleep(time.Second)

	r, err := http.NewRequest(http.MethodPost, "https://localhost", pr)
	if err != nil {
		panic(err)
	}

	ctx, cancel := context.WithTimeout(context.Background(), time.Second)
	defer cancel()

	tr := &http.Transport{
		TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
	}
	http2.ConfigureTransport(tr)
	client := &http.Client{Transport: tr}

	_, err = client.Do(r.WithContext(ctx))
	if err != nil {
		fmt.Println("CLIENT DO(), err:", err)
	}
	time.Sleep(time.Second * 2)
}

@golang golang locked and limited conversation to collaborators Jun 28, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants