census-instrumentation / opencensus-go

A stats collection and distributed tracing framework
http://opencensus.io
Apache License 2.0
2.05k stars 327 forks source link

ochttp.Handler.ServeHTTP func param r * http.Request did not modify r.MultipartForm correctly #1292

Open voctior opened 1 year ago

voctior commented 1 year ago

Please answer these questions before submitting a bug report.

What version of OpenCensus are you using?

v0.24.0

What version of Go are you using?

go version go1.19.2 darwin/arm64

What did you do?

// ochttp func (h Handler) ServeHTTP(w http.ResponseWriter, r http.Request) { var tags addedTags // ----- shallow copy r, traceEnd := h.startTrace(w, r) defer traceEnd() w, statsEnd := h.startStats(w, r) defer statsEnd(&tags) handler := h.Handler if handler == nil { handler = http.DefaultServeMux } // ----- shallow copy r = r.WithContext(context.WithValue(r.Context(), addedTagsKey{}, &tags)) handler.ServeHTTP(w, r) }

// std http func (c *conn) serve(ctx context.Context){ // w.req has not change serverHandler{c.server}.ServeHTTP(w, w.req) w.finishRequest() }

What did you expect to see?

w.req has changed

What did you see instead?

Additional context

Add any other context about the problem here.

voctior commented 1 year ago

i have a demo like this. I guess it's this mistake

import "testing"

type TT struct {
    t *TT
    c int
}

func TestXxx(t *testing.T) {
    var a = TT{t: &TT{c: 1}}
    A(a.t)
    if a.t.c != 2 {
        panic(a.t.c)
    }
    B(a.t)
    if a.t.c != 6 {
        panic(a.t.c)
    }
}

func A(in *TT) {
    in.c++
    in = &TT{c: 6}
}

func B(in *TT) {
    in.c = 6
}

test has no panic

voctior commented 1 year ago

I want to use response-finishRequest to delete locally mixed files .like this

image
voctior commented 1 year ago

fix like this?

// ochttp
func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
var tags addedTags
r2, traceEnd := h.startTrace(w, r)
defer traceEnd()
w, statsEnd := h.startStats(w, r2)
defer statsEnd(&tags)
handler := h.Handler
if handler == nil {
handler = http.DefaultServeMux
}
r2 = r2.WithContext(context.WithValue(r.2Context(), addedTagsKey{}, &tags))
// value copy
*r = *r2

handler.ServeHTTP(w, r)
}