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

fix: panic with concurrent schema parsing #502

Merged
merged 7 commits into from
Feb 12, 2025
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
148 changes: 148 additions & 0 deletions concurrent_parse_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
package avro_test

import (
"sync"
"testing"

"github.com/hamba/avro/v2"
"github.com/stretchr/testify/require"
)

func TestConcurrentParse(t *testing.T) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add this to schema_test.go. The naming separates it from what it is trying to test.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have moved the test, also I have moved the schema to a file, it still reproduces the issue.

var wg sync.WaitGroup

for i := 0; i < 10000; i++ {
wg.Add(1)
go func() {
defer wg.Done()
_, err := avro.Parse(testSchema())
require.NoError(t, err)
}()
}

wg.Wait()
}

func testSchema() string {
return `{
"type": "record",
"name": "FootballUpdateEvent",
"namespace": "com.example.avro",
"fields": [
{
"name": "event_metadata",
"type": {
"type": "record",
"name": "EventMetadata",
"fields": [
{ "name": "name", "type": "string", "default": "" },
{ "name": "trace", "type": "string", "default": "" },
{ "name": "stamp", "type": "long", "default": 0 },
{ "name": "destination", "type": "string", "default": "" }
]
},
"default": {
"name": "",
"trace": "",
"stamp": 0,
"destination": ""
}
},
{
"name": "player",
"type": {
"type": "record",
"name": "Player",
"fields": [
{ "name": "team_id", "type": "string", "default": "" },
{ "name": "name", "type": "string", "default": "" },
{ "name": "team", "type": "string", "default": "" },
{ "name": "contract", "type": "long", "default": 0 },
{ "name": "xg", "type": "double", "default": 0.0 },
{ "name": "xgp", "type": "double", "default": 0.0 },
{ "name": "xgp99", "type": "double", "default": 0.0 },
{ "name": "xg90", "type": "double", "default": 0.0 },
{
"name": "xg90p",
"type": {
"type": "record",
"name": "MatchXG",
"fields": [
{ "name": "matchXG", "type": "double", "default": 0.0 },
{ "name": "matchXGP", "type": "string", "default": "" }
]
},
"default": {
"matchXG": 0.0,
"matchXGP": ""
}
},
{
"name": "ttd",
"type": "MatchXG",
"default": {
"matchXG": 0.0,
"matchXGP": ""
}
},
{
"name": "leagueXG",
"type": {
"type": "record",
"name": "LeagueXG",
"fields": [
{ "name": "top_assist", "type": "string", "default": "" },
{ "name": "top_score", "type": "string", "default": "" },
{ "name": "top_xg", "type": "string", "default": "" },
{ "name": "top_creation", "type": "string", "default": "" }
]
},
"default": {
"top_assist": "",
"top_score": "",
"top_xg": "",
"top_creation": ""
}
},
{
"name": "player_numbers",
"type": {
"type": "array",
"items": {
"type": "record",
"name": "PlayerNumber",
"fields": [
{ "name": "player_number", "type": "long", "default": 0 }
]
}
},
"default": []
},
{
"name": "contact_renewal",
"type": [
"null",
{
"type": "record",
"name": "ContactRenewal",
"fields": [
{ "name": "stamp", "type": "long", "default": 0 },
{ "name": "type", "type": "string", "default": "" },
{ "name": "xg", "type": "MatchXG", "default": {
"matchXG": 0.0,
"matchXGP": ""
} }
]
}
],
"default": null
},
{ "name": "defence", "type": "string", "default": "" },
{ "name": "offence", "type": "string", "default": "" }
]
},
"default": {}
}
]
}`
}
11 changes: 11 additions & 0 deletions schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,17 @@ func (c *SchemaCache) Get(name string) Schema {
return nil
}

// AddAll adds all schemas from the given cache to the current cache.
func (c *SchemaCache) AddAll(cache *SchemaCache) {
if cache == nil {
return
}
cache.cache.Range(func(key, value interface{}) bool {
c.cache.Store(key, value)
return true
})
}

// Schemas is a slice of Schemas.
type Schemas []Schema

Expand Down
14 changes: 10 additions & 4 deletions schema_parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func Parse(schema string) (Schema, error) {

// ParseWithCache parses a schema string using the given namespace and schema cache.
func ParseWithCache(schema, namespace string, cache *SchemaCache) (Schema, error) {
return ParseBytesWithCache([]byte(schema), namespace, cache)
return ParseBytesWithCache([]byte(schema), namespace)
}

// MustParse parses a schema string, panicking if there is an error.
Expand Down Expand Up @@ -64,21 +64,27 @@ func ParseFiles(paths ...string) (Schema, error) {

// ParseBytes parses a schema byte slice.
func ParseBytes(schema []byte) (Schema, error) {
return ParseBytesWithCache(schema, "", DefaultSchemaCache)
return ParseBytesWithCache(schema, "")
}

// ParseBytesWithCache parses a schema byte slice using the given namespace and schema cache.
func ParseBytesWithCache(schema []byte, namespace string, cache *SchemaCache) (Schema, error) {
func ParseBytesWithCache(schema []byte, namespace string) (Schema, error) {
var json any
if err := jsoniter.Unmarshal(schema, &json); err != nil {
json = string(schema)
}

internalCache := &SchemaCache{}
internalCache.AddAll(DefaultSchemaCache)

seen := seenCache{}
s, err := parseType(namespace, json, seen, cache)
s, err := parseType(namespace, json, seen, internalCache)
if err != nil {
return nil, err
}

DefaultSchemaCache.AddAll(internalCache)

return derefSchema(s), nil
}

Expand Down