Using a context not associated with a request

517 views
Skip to first unread message

wil...@voodoo.io

unread,
May 3, 2017, 12:57:21 PM5/3/17
to Google App Engine

Hello,


I was trying to deploy an API using PubSub with AppEngine but I got a "not an App Engine context" error, it's related to the following code:


package main 

import (
"log"

)

var (
ctx context.Context
pubsubClient *pubsub.Client
)  

func InitPubSub () {
ctx = context.Background()

psClient, err := pubsub.NewClient(ctx, "myproject-645411")
if err != nil {
log.Println("(init pub sub) error while creating new pubsub client:", err)

} else {
pubsubClient = psClient

}
}


So I was looking at the BackgroundContext func from the appengine package but it says that it only works with AppEngine flexible environment (standard environment seems more appropriate to my app): https://godoc.org/google.golang.org/appengine#BackgroundContext


Do you know if there's another function I can use? Or should I create and close a client for each request?


Thanks!

George (Cloud Platform Support)

unread,
May 3, 2017, 2:09:56 PM5/3/17
to Google App Engine
Hello William, 

What version of Go have you installed? If you want to program for the standard environment you need Go version 1.6. 

wil...@voodoo.io

unread,
May 4, 2017, 5:22:12 AM5/4/17
to Google App Engine
Hello George,

I have changed my version of Go to 1.6 but I'm still getting the same error for the same line:
psClient, err := pubsub.NewClient(ctx, "myproject-645411")

Another question related to this one, if I want to create a new BigQuery client, which function should I use to create an AppEngine context (https://cloud.google.com/bigquery/docs/reference/libraries#client-libraries-install-go) ?

Also, I sometimes get this error when I try to execute a go get or go build command with Go 1.6, is that normal?
fatal error: MSpanList_Insert

runtime stack:
runtime.throw(0x637260, 0x10)
[...]

George (Cloud Platform Support)

unread,
May 4, 2017, 3:04:07 PM5/4/17
to Google App Engine
Hi William, 

More information on the error is needed, to determine where the message gets generated and the reason behind it. More useful data can be gathered by enabling error collection in Go, as described in "How to get stacktraces from errors in Golang with go-errors/errors". 

For building the BigQuery client you could simply follow the example provided in the "BigQuery Client Libraries" document

wil...@voodoo.io

unread,
May 4, 2017, 4:42:26 PM5/4/17
to Google App Engine
Hi George,

I'm not at my workplace right now (it's quite late in EU). I got this from this from the Cloud platform's console, does this help ?

panic: not an App Engine context

goroutine 1 [running]:
panic(0x17db1c0, 0xc010526280)
	go/src/runtime/panic.go:481 +0x3e6
google.golang.org/appengine/internal.fullyQualifiedAppID(0x2a6f03567698, 0xc01040f5e8, 0x0, 0x0)
	google.golang.org/appengine/internal/identity_classic.go:54 +0x8b
google.golang.org/appengine/internal.FullyQualifiedAppID(0x2a6f03567698, 0xc01040f5e8, 0x0, 0x0)
	google.golang.org/appengine/internal/api_common.go:77 +0xe2
google.golang.org/appengine/internal.AppID(0x2a6f03567698, 0xc01040f5e8, 0x0, 0x0)
	google.golang.org/appengine/internal/identity.go:13 +0x37
google.golang.org/appengine.AppID(0x2a6f03567698, 0xc01040f5e8, 0x0, 0x0)
	google.golang.org/appengine/identity.go:20 +0x37
golang.org/x/oauth2/google.FindDefaultCredentials(0x2a6f03567698, 0xc01040f5e8, 0xc0105f3f60, 0x2, 0x2, 0xc0105f3f60, 0x0, 0x0)
	golang.org/x/oauth2/google/default.go:86 +0x5a8
golang.org/x/oauth2/google.DefaultTokenSource(0x2a6f03567698, 0xc01040f5e8, 0xc0105f3f60, 0x2, 0x2, 0x0, 0x0, 0x0, 0x0)
	golang.org/x/oauth2/google/default.go:43 +0x6b
google.golang.org/api/transport.DialGRPC(0x2a6f03567698, 0xc01040f5e8, 0xc01053de80, 0x3, 0x4, 0x1, 0x0, 0x0)
	google.golang.org/api/transport/dial.go:149 +0x379
cloud.google.com/go/pubsub/apiv1.NewPublisherClient(0x2a6f03567698, 0xc01040f5e8, 0xc010581d30, 0x1, 0x1, 0x2a6f03567920, 0x0, 0x0)
	cloud.google.com/go/pubsub/apiv1/publisher_client.go:122 +0x1cc
cloud.google.com/go/pubsub.newPubSubService(0x2a6f03567698, 0xc01040f5e8, 0xc010581d30, 0x1, 0x1, 0x0, 0x0, 0x0)
	cloud.google.com/go/pubsub/service.go:91 +0x79
cloud.google.com/go/pubsub.NewClient(0x2a6f03567698, 0xc01040f5e8, 0x1995f20, 0xd, 0x0, 0x0, 0x0, 0xc01061c1e0, 0x0, 0x0)
	cloud.google.com/go/pubsub/pubsub.go:68 +0x495
main.InitPubSub()
	pubsub.go:18 +0xbb
main.main()
	main.go:14 +0x1c

If not, I'll follow the guide you linked me and provide you with the stacktrace asap.
Thanks again for your help.

George (Cloud Platform Support)

unread,
May 5, 2017, 4:51:37 PM5/5/17
to Google App Engine
The information provided is relevant. Waiting, at the moment, for input from developers, who might request more information soon. 

wil...@voodoo.io

unread,
May 9, 2017, 1:51:14 PM5/9/17
to Google App Engine
Hi George,

Thanks for your reply.

While waiting, I tried directly streaming the data to BigQuery instead of using PubSub so I created a client by following the link you gave me but I'm getting a similar error as when I create a PubSub client:

panic: not an App Engine context
goroutine 1 [running]:
panic(0x1a83d00, 0xc010528280)
golang.org/x/oauth2/google.FindDefaultCredentials(0x2ad463222500, 0xc01040f540, 0xc010641470, 0x1, 0x1, 0xc010641470, 0x0, 0x0)
golang.org/x/oauth2/google.DefaultTokenSource(0x2ad463222500, 0xc01040f540, 0xc010641470, 0x1, 0x1, 0x0, 0x0, 0x0, 0x0)
google.golang.org/api/transport.NewHTTPClient(0x2ad463222500, 0xc01040f540, 0xc010583c48, 0x3, 0x3, 0x0, 0x0, 0x0, 0x0, 0x0)
cloud.google.com/go/bigquery.NewClient(0x2ad463222500, 0xc01040f540, 0x1c4f990, 0xd, 0x0, 0x0, 0x0, 0xc010400180, 0x0, 0x0)
main.InitBigQuery()
main.main()
main.go:20 +0x1c

Code:
var (
    ctx                             context
.Context
    bigQueryClient                    
*bigquery.Client
)  

func
InitBigQuery() {
    ctx
= context.Background()

    bqClient
, err := bigquery.NewClient(ctx, "voodoo-165916")
   
if err != nil {
        log
.Println("(init big query) error while creating new bigquery client:", err)

   
} else {
        bigQueryClient
= bqClient
       
InitUploader()

   
}
}



George (Cloud Platform Support)

unread,
May 9, 2017, 2:50:41 PM5/9/17
to Google App Engine
Hi William, 

Before attempting to go in any more depth: how do you run your code, in detail (not only the function definition)? The error indicates a function main(). It might prove helpful to see the main function in context, how you call your InitBigQuery() function, exactly. 

What is the directory structure of your code, how is your workspace organized? 

Otherwise, the example given on the "BigQuery Client Libraries" page seems quite helpful for what you attempt to do. 

wil...@voodoo.io

unread,
May 9, 2017, 5:28:55 PM5/9/17
to Google App Engine
Here is the main function:

package main

import (
    "encoding/json"
    "io/ioutil"
    "net/http"
    "fmt"

    "google.golang.org/appengine"
)


func main () {
    // BigQuery
    InitBigQuery()
    defer bigQueryClient.Close()

    http.HandleFunc("/", HandlerBase)
    http.HandleFunc("/user", HandlerUser)
    http.HandleFunc("/event", HandlerEvent)

    appengine.Main()
}


All the files (main.go, bigquery.go (client creation, data streaming), handlers.go, etc.) are in the same folder.

George (Cloud Platform Support)

unread,
May 17, 2017, 9:16:30 AM5/17/17
to Google App Engine
Hi William, 

Email your exact project name privately once again, using the drop-down menu of the "reply" button. 

There is a suspicion of user-error in coding or simple misconfiguration, things like misspelled app or bucket names, or incorrect imports. It is worthwhile double or even triple checking. 
Reply all
Reply to author
Forward
0 new messages