-
Notifications
You must be signed in to change notification settings - Fork 17.9k
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: Server fails to clean up MultipartForm files when Request.WithContext is used #58809
Comments
Causes it to fail in what way? (What are the symptoms you actually saw?) |
package main
import (
"context"
"fmt"
"io"
"net/http"
"os"
)
func main() {
http.HandleFunc("/upload", foo(bar(upload)))
http.ListenAndServe(":1323", nil)
}
func foo(next http.HandlerFunc) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("foo("))
// do something before
ctx := context.WithValue(r.Context(), "foo", "bar")
r = r.WithContext(ctx)
next(w, r)
w.Write([]byte(")"))
}
}
func bar(next http.HandlerFunc) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("bar("))
next(w, r)
w.Write([]byte(")"))
}
}
func upload(w http.ResponseWriter, r *http.Request) {
r.ParseMultipartForm(32 << 20)
file, handler, err := r.FormFile("file")
if err != nil {
fmt.Println(err)
return
}
defer file.Close()
f, err := os.OpenFile(handler.Filename, os.O_WRONLY|os.O_CREATE, 0666)
if err != nil {
fmt.Println(err)
return
}
defer f.Close()
io.Copy(f, file)
fmt.Fprintln(w, "upload ok!")
} After calling WithContext with 'r=r.WithContext (ctx)', the request variable pointer address was changed, so it was not executed finishRequest() method, resulting in temporary files not being cleared |
|
How to solve it? |
call |
You mean that the middleware implements |
What version of Go are you using (
go version
)?Does this issue reproduce with the latest release?
yes
What operating system and processor architecture are you using (
go env
)?go env
OutputWhat did you do?
What did you expect to see?
labstack/echo#2413
What did you see instead?
The pointer address changes after calling the WithContext method, which causes the server. go finishRequest() method to fail to execute normally
The text was updated successfully, but these errors were encountered: