googlearchive / cloud-functions-go

Unofficial Native Go Runtime for Google Cloud Functions
Apache License 2.0
423 stars 44 forks source link

Function always dies when issuing HTTP(S)-Requests with nodego.OverrideLogger() #24

Closed andwun closed 6 years ago

andwun commented 6 years ago

I've been having a bad time trying to issue HTTP(S)-Requests from a Cloud Function that uses this package.

Consider this absolute minimal repro:

package main

import (
    "flag"
    "net/http"

    "github.com/GoogleCloudPlatform/cloud-functions-go/nodego"
)

func init() {
    nodego.OverrideLogger()
}

func main() {
    flag.Parse()

    http.HandleFunc(nodego.HTTPTrigger, nodego.WithLoggerFunc(func(w http.ResponseWriter, r *http.Request) {
        http.Get("http://example.com/")
    }))

    nodego.TakeOver()
}

When called, it always dies:

Function execution took 296 ms, finished with status: 'connection error'
Error: function crashed out of request scope Function invocation was interrupted.

Just when writing this, it occured to me to leave out the line nodego.OverrideLogger(). Now the Cloud Function finally does what it is supposed to.

Does anyone have a clue what's wrong here?

ssttevee commented 6 years ago

After a little bit of testing, it seems that this occurs whenever there is any significant waiting time which. However, I was not able to reproduce the same results with several dummy goroutines and the logger goroutine disabled.

I was also still getting the same problem even without the nodego.OverrideLogger() call.

My next suspicion would be the that the instance is actually being killed by a failed report. Some more investigation is needed.

ssttevee commented 6 years ago

I was correct in suspecting that the instance was actually being killed, but it ended up being a problem with error handling that caused it.

andwun commented 6 years ago

Thanks for looking into this problem. I can confirm that the issue is gone when applying your fix.