Skip to content

stdlib: data race importing a CodeSrc module from several contexts #266

Open

Description

@drew-512

A ModuleImpl registered with CodeSrc (or CodeBuf) is compiled into impl.Code on first import, with no locking (ModuleInit in stdlib/stdlib.go). A registered ModuleImpl is shared by every py.Context, so contexts that import it for the first time at once race on impl.Code and on the py.Code being built.

Reproducer, run with go test -race:

func TestConcurrentCodeSrc(t *testing.T) {
	py.RegisterModule(&py.ModuleImpl{
		Info:    py.ModuleInfo{Name: "codesrc_test", FileDesc: "<codesrc_test>"},
		CodeSrc: "X = 42\n",
	})
	var wg sync.WaitGroup
	for i := 0; i < 8; i++ {
		wg.Add(1)
		go func() {
			defer wg.Done()
			ctx := py.NewContext(py.DefaultContextOpts())
			defer ctx.Close()
			if err := py.Import(ctx, "codesrc_test"); err != nil {
				t.Error(err)
			}
		}()
	}
	wg.Wait()
}

On main (444ae5e) each run reports WARNING: DATA RACE (12–13 reports per run).

Go 1.27.1, darwin/arm64.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions