Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reset runtime context options before releasing it back into pool #500

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
38 changes: 30 additions & 8 deletions encode_test.go
Expand Up @@ -1999,30 +1999,33 @@ type marshalContextKey struct{}
type marshalContextStructType struct{}

func (t *marshalContextStructType) MarshalJSON(ctx context.Context) ([]byte, error) {
if ctx == nil {
return []byte(`"no context"`), nil
}
v := ctx.Value(marshalContextKey{})
if v == nil {
return []byte(`"no value in context"`), nil
}
s, ok := v.(string)
if !ok {
return nil, fmt.Errorf("failed to propagate parent context.Context")
return []byte(`"unexpected value in context"`), nil
}
if s != "hello" {
return nil, fmt.Errorf("failed to propagate parent context.Context")
}
return []byte(`"success"`), nil
return []byte(`"` + s + `"`), nil
}

func TestEncodeContextOption(t *testing.T) {
t.Run("MarshalContext", func(t *testing.T) {
ctx := context.WithValue(context.Background(), marshalContextKey{}, "hello")
ctx := context.WithValue(context.Background(), marshalContextKey{}, "success")
b, err := json.MarshalContext(ctx, &marshalContextStructType{})
if err != nil {
t.Fatal(err)
}
if string(b) != `"success"` {
t.Fatal("failed to encode with MarshalerContext")
t.Fatal("failed to encode with MarshalContext")
}
})
t.Run("EncodeContext", func(t *testing.T) {
ctx := context.WithValue(context.Background(), marshalContextKey{}, "hello")
ctx := context.WithValue(context.Background(), marshalContextKey{}, "success")
buf := bytes.NewBuffer([]byte{})
if err := json.NewEncoder(buf).EncodeContext(ctx, &marshalContextStructType{}); err != nil {
t.Fatal(err)
Expand All @@ -2031,6 +2034,25 @@ func TestEncodeContextOption(t *testing.T) {
t.Fatal("failed to encode with EncodeContext")
}
})
t.Run("Marshal after MarshalContext", func(t *testing.T) {
// Regression test for https://github.com/goccy/go-json/issues/499
ctx := context.WithValue(context.Background(), marshalContextKey{}, "success")
b, err := json.MarshalContext(ctx, &marshalContextStructType{})
if err != nil {
t.Fatal(err)
}
if string(b) != `"success"` {
t.Fatal("failed to encode with MarshalContext")
}

b, err = json.Marshal(&marshalContextStructType{})
if err != nil {
t.Fatal(err)
}
if string(b) != `"no context"` {
t.Fatal("failed to encode with Marshal")
}
})
}

func TestInterfaceWithPointer(t *testing.T) {
Expand Down
1 change: 1 addition & 0 deletions internal/decoder/context.go
Expand Up @@ -27,6 +27,7 @@ func TakeRuntimeContext() *RuntimeContext {
}

func ReleaseRuntimeContext(ctx *RuntimeContext) {
ctx.Option.Context = nil
runtimeContextPool.Put(ctx)
}

Expand Down
3 changes: 1 addition & 2 deletions internal/encoder/context.go
@@ -1,7 +1,6 @@
package encoder

import (
"context"
"sync"
"unsafe"

Expand Down Expand Up @@ -69,7 +68,6 @@ var (
)

type RuntimeContext struct {
Context context.Context
Buf []byte
MarshalBuf []byte
Ptrs []uintptr
Expand Down Expand Up @@ -101,5 +99,6 @@ func TakeRuntimeContext() *RuntimeContext {
}

func ReleaseRuntimeContext(ctx *RuntimeContext) {
ctx.Option.Context = nil
runtimeContextPool.Put(ctx)
}