Repository navigation
Conversation
ModuleInit compiled ModuleImpl.CodeSrc (or unmarshalled CodeBuf) into ModuleImpl.Code without synchronization. A registered ModuleImpl is shared by every Context, so contexts importing such a module for the first time at the same time raced on impl.Code and on the py.Code being built. Prepare the code under a package-level mutex. It is still compiled once and then shared, since a built py.Code is read-only.
drew-512
force-pushed
the
stdlib-module-code-race
branch
from
November 10, 2026 00:58
645a9b4 to
988383f
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
ModuleInitcompilesModuleImpl.CodeSrc(or unmarshalsCodeBuf) intoModuleImpl.Codeon first use, without any locking. A registeredModuleImplis shared by everypy.Context, so when several contexts import such a module for the first time at once they race onimpl.Codeand on thepy.Codestill being built.go test -racewith 8 contexts importing oneCodeSrcmodule reports 25–26 races per run on main.The code is now prepared in a small
moduleCodehelper under a package-level mutex. It is still compiled once and then shared, since a builtpy.Codeis read-only (the multi-context example already relies on that). The mutex is only held while checking or buildingimpl.Code.Tests:
CodeSrcmodule at the same time and call into it. Fails under-raceon main, 0 races in 60 runs on this branch.go test -race ./...passes on Go 1.18, 1.19 and 1.27.Fixes #266.