Kong / go-pdk

Write Kong plugins in Go! 🦍
https://pkg.go.dev/github.com/Kong/go-pdk
Apache License 2.0
144 stars 47 forks source link

kong.Response.Exit() Hanging #169

Closed joeyagreco-sb closed 4 months ago

joeyagreco-sb commented 5 months ago

When calling Response.Exit(), code hangs.

Example for reproduction:

Set shouldExit to true, and the test hangs. Set shouldExit to false and the test does not hang.

package main

import (
    "errors"
    "fmt"
    "net/http"
    "testing"

    "github.com/Kong/go-pdk"
    "github.com/stretchr/testify/assert"

    k "github.com/Kong/go-pdk/test"
)

type foo struct {
    shouldExit bool
}

func New() *foo {
    return &foo{
        // manually change me to see both ways
        shouldExit: true,
    }
}

func (f *foo) Response(kong *pdk.PDK) {
    if f.shouldExit {
        fmt.Println("im trying to exit early")
        kong.Response.Exit(http.StatusInternalServerError, []byte(errors.New("exit").Error()), nil)
    }
    fmt.Println("im done")
}

func TestFoo(t *testing.T) {
    t.Parallel()

    env, err := k.New(t, k.Request{
        Method: "POST",
        Url:    "",
        Body:   []byte("{}"),
    })
    assert.NoError(t, err)
    env.DoHttps(New())
}

I will note that kong.Response.ExitStatus() does not hang.

gszr commented 4 months ago

Fixed by #174