Skip to content

Commit

Permalink
Ensure that introspection types don't race during initialization.
Browse files Browse the repository at this point in the history
  • Loading branch information
egonelbre committed Apr 3, 2019
1 parent bed865f commit 55ad5ec
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 0 deletions.
7 changes: 7 additions & 0 deletions definition.go
Original file line number Diff line number Diff line change
Expand Up @@ -399,6 +399,13 @@ func NewObject(config ObjectConfig) *Object {

return objectType
}

// ensureCache ensures that both fields and interfaces have been initialized properly,
// to prevent races.
func (gt *Object) ensureCache() {
gt.Fields()
gt.Interfaces()
}
func (gt *Object) AddFieldConfig(fieldName string, fieldConfig *Field) {
if fieldName == "" || fieldConfig == nil {
return
Expand Down
7 changes: 7 additions & 0 deletions introspection.go
Original file line number Diff line number Diff line change
Expand Up @@ -611,6 +611,13 @@ func init() {
Type: TypeType,
})

SchemaType.ensureCache()
DirectiveType.ensureCache()
TypeType.ensureCache()
FieldType.ensureCache()
InputValueType.ensureCache()
EnumValueType.ensureCache()

// Note that these are FieldDefinition and not FieldConfig,
// so the format for args is different.
SchemaMetaFieldDef = &FieldDefinition{
Expand Down
69 changes: 69 additions & 0 deletions race_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package graphql_test

import (
"io/ioutil"
"os"
"os/exec"
"testing"
)

func TestRace(t *testing.T) {
tempfile, err := ioutil.TempFile("", "examplerace.*.go")
if err != nil {
t.Fatal(err)
}
filename := tempfile.Name()
t.Log(filename)

defer os.Remove(filename)

_, err = tempfile.Write([]byte(`
package main
import (
"runtime"
"sync"
"github.com/graphql-go/graphql"
)
func main() {
var wg sync.WaitGroup
wg.Add(2)
for i := 0; i < 2; i++ {
go func() {
defer wg.Done()
schema, _ := graphql.NewSchema(graphql.SchemaConfig{
Query: graphql.NewObject(graphql.ObjectConfig{
Name: "RootQuery",
Fields: graphql.Fields{
"hello": &graphql.Field{
Type: graphql.String,
Resolve: func(p graphql.ResolveParams) (interface{}, error) {
return "world", nil
},
},
},
}),
})
runtime.KeepAlive(schema)
}()
}
wg.Wait()
}
`))
if err != nil {
t.Fatal(err)
}

if err := tempfile.Close(); err != nil {
t.Fatal(err)
}

result, err := exec.Command("go", "run", "-race", filename).CombinedOutput()
if err != nil || len(result) != 0 {
t.Log(string(result))
t.Fatal(err)
}
}

0 comments on commit 55ad5ec

Please sign in to comment.