Skip to content

Commit

Permalink
Ensure that introspection types don't race during initialization. (#469)
Browse files Browse the repository at this point in the history
* Ensure that introspection types don't race during initialization.

* Ensure tests run on Go1.8+
  • Loading branch information
egonelbre authored and Fontinalis committed Apr 3, 2019
1 parent bed865f commit 199d20b
Show file tree
Hide file tree
Showing 3 changed files with 78 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
64 changes: 64 additions & 0 deletions race_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package graphql_test

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

func TestRace(t *testing.T) {
tempdir, err := ioutil.TempDir("", "race")
if err != nil {
t.Fatal(err)
}
defer os.RemoveAll(tempdir)

filename := filepath.Join(tempdir, "example.go")
err = ioutil.WriteFile(filename, []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()
}
`), 0755)
if 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 199d20b

Please sign in to comment.