Go 1.8 Beta - unable to use gorilla/mux - "NewContext passed an unknown http.Request"

431 views
Skip to first unread message

JP Robinson

unread,
Jun 29, 2017, 2:08:50 PM6/29/17
to google-appengine-go
Hi there,

I've been attempting to move some services over to 1.8 but have been running into panics due to the use of the "gorilla/mux" router. I have a feeling it may be due to the soft copies of the request that happen within the package but not 100% sure.

The code runs fine locally with `goapp` and `dev_appserver.py` but starts panicing once deployed to the standard environment.

SDK version: go version 1.8.3 (appengine-1.9.56) darwin/amd64

Code to reproduce:
package hello


import (
 
"io"
 
"net/http"


 
"google.golang.org/appengine"
 
"google.golang.org/appengine/log"


 
"github.com/gorilla/mux"
)


func init
() {
 mx
:= mux.NewRouter()
 mx
.HandleFunc("/", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
 ctx
:= appengine.NewContext(r)
 log
.Debugf(ctx, "time to say hello!")
 io
.WriteString(w, "howdy!")
 
}))
 http
.Handle("/", mx)
}



Produces this panic: 

panic: appengine: NewContext passed an unknown http.Request goroutine 6 [running]: panic(0x956ce0, 0xc008565de0) go/src/runtime/panic.go:489 +0x2cf log.Panic(0xc0084315c0, 0x1, 0x1) go/src/log/log.go:322 +0xc0 appengine_internal.NewContext(0xc00849e800, 0x0) go/src/appengine_internal/api_prod.go:459 +0x130 appengine.NewContext(0xc00849e800, 0x4ec008, 0x100) go/src/appengine/appengine.go:55 +0x2b google.golang.org/appengine/internal.WithContext(0x2aef6f35d168, 0xc008410390, 0xc00849e800, 0x0, 0x2aef6f35d168) google.golang.org/appengine/internal/api_classic.go:63 +0x2b google.golang.org/appengine.WithContext(0x2aef6f35d168, 0xc008410390, 0xc00849e800, 0x2aef6f35d168, 0xc008410390) google.golang.org/appengine/appengine.go:73 +0x3f google.golang.org/appengine.NewContext(0xc00849e800, 0xcc4760, 0xc008555d40) google.golang.org/appengine/appengine.go:66 +0x67 main36526.init.1.func1(0xcbcbe0, 0xc00844bc00, 0xc00849e800) hello.go:16 +0x2f net/http.HandlerFunc.ServeHTTP(0xa75b10, 0xcbcbe0, 0xc00844bc00, 0xc00849e800) go/src/net/http/server.go:1942 +0x44 github.com/gorilla/mux.(*Router).ServeHTTP(0xc008436460, 0xcbcbe0, 0xc00844bc00, 0xc00849e800) github.com/gorilla/mux/mux.go:114 +0x10c net/http.(*ServeMux).ServeHTTP(0xd04540, 0xcbcbe0, 0xc00844bc00, 0xc00849e600) go/src/net/http/server.go:2238 +0x130 appengine_internal.executeRequestSafely(0xc00844bc00, 0xc00849e600) go/src/appengine_internal/api_prod.go:304 +0x77 appengine_internal.(*server).HandleRequest(0xd005c0, 0xc0084feea0, 0xc008578300, 0xc0085663c0, 0x0, 0x0) go/src/appengine_internal/api_prod.go:238 +0xaab reflect.Value.call(0x9a13c0, 0xd005c0, 0x213, 0xa58d7f, 0x4, 0xc008431f78, 0x3, 0x3, 0xc0085663c0, 0x9f4140, ...) go/src/reflect/value.go:434 +0x91f reflect.Value.Call(0x9a13c0, 0xd005c0, 0x213, 0xc008431f78, 0

JP Robinson

unread,
Jun 29, 2017, 2:38:32 PM6/29/17
to google-appengine-go
For now I'm going to get around this by grabbing the GAE context and injecting into the request before handing the request over to Gorilla.

    r = r.WithContext(appengine.NewContext(r))
    mux
.ServeHTTP(w, r)

From there, I'll just use `r.Context()` to get the GAE enabled context within my handlers.

Chris Broadfoot

unread,
Jun 29, 2017, 3:05:48 PM6/29/17
to JP Robinson, google-appengine-go
Yes, gorilla uses WithContext, which copies the Request.

Thanks for testing - I'm working on a patch.

--
You received this message because you are subscribed to the Google Groups "google-appengine-go" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-appengine-go+unsub...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
Message has been deleted
0 new messages