SG-43269 Fix sys.path and sys.meta_path isolation during core swap#1101
Open
julien-lang wants to merge 1 commit into
Open
SG-43269 Fix sys.path and sys.meta_path isolation during core swap#1101julien-lang wants to merge 1 commit into
julien-lang wants to merge 1 commit into
Conversation
When the site core (e.g. v0.23.8 bundled with SGD 3.0) boots, its tank_vendor/__init__.py inserts its pkgs.zip into sys.path[0] and registers a _TankVendorMetaFinder in sys.meta_path. Previously, _swap_core() cleaned up sys.modules but left both of those in place. This caused two distinct failures when swapping to an older project core: 1. sys.path pollution: direct imports like Version: ImageMagick 7.1.2-23 Q16-HDRI aarch64 6c51b2de5:20260516 https://imagemagick.org By default, 'file' is written in the MIFF image format. To specify a particular image format, precede the filename with an image format name and a colon (i.e. ps:image) or specify the image type as the filename suffix (i.e. image.ps). Specify 'file' as '-' for standard input or output. (not via tank_vendor.*) resolved to the site core's pkgs.zip instead of the project core's vendored copy. The site pkgs.zip lacks packages removed in newer releases (e.g. sgsix), causing ImportError. 2. Stale _TankVendorMetaFinder: when six.__path__ == [] caused CoreImportHandler to return None for , the stale meta finder intercepted the import and tried to resolve it against the old sys.path state (site pkgs.zip, no six), causing ModuleNotFoundError. Fix: in _swap_core(), before resetting self._core_path: - Strip all sys.path entries that live under the outgoing core root. - Remove any _TankVendorMetaFinder instances from sys.meta_path and delete sys._tank_vendor_meta_finder. The incoming core's tank_vendor/__init__.py will then insert its own pkgs.zip and register a fresh finder when Version: ImageMagick 7.1.2-23 Q16-HDRI aarch64 6c51b2de5:20260516 https://imagemagick.org By default, 'file' is written in the MIFF image format. To specify a particular image format, precede the filename with an image format name and a colon (i.e. ps:image) or specify the image type as the filename suffix (i.e. image.ps). Specify 'file' as '-' for standard input or output. loads it. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
| norm_p = os.path.normpath(p) | ||
| try: | ||
| if os.path.commonpath([norm_p, current_core_root]) == current_core_root: | ||
| log.debug("Removing sys.path entry belonging to outgoing core: %s" % p) |
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #1101 +/- ##
==========================================
+ Coverage 79.45% 79.46% +0.01%
==========================================
Files 198 198
Lines 20769 20785 +16
==========================================
+ Hits 16502 16517 +15
- Misses 4267 4268 +1
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
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.
What
Fixes a
sys.pathandsys.meta_pathisolation bug in_swap_core()that caused project bootstrapping to fail when the site core is newer (pkgs.zip-style, e.g. v0.23.8) and the project core is older (vendored-style, e.g. v0.22.4).Two things are now cleaned up in
_swap_core()before handing off to the incoming core:sys.pathcleanup — removes all entries that live under the outgoing core's root directory. This ensures the site core'spkgs.zipis no longer onsys.pathwhen the project core loads, so direct imports likeimport shotgun_api3resolve to the project core's vendored copy rather than the site's.sys.meta_pathcleanup — removes the stale_TankVendorMetaFinderinstance registered by the outgoing core'stank_vendor/__init__.py. The incoming core'stank_vendor/__init__.pyinstalls a fresh one whenimport tankloads it.Why
Two distinct customer-facing failures traced to the same root cause:
SGD 3.0 + project config ≤ v1.7.5 →
ModuleNotFoundError: No module named 'tank_vendor.six.moves'tank_vendor/__init__.pyinserts itspkgs.zipatsys.path[0]and registers_TankVendorMetaFinderinsys.meta_path._swap_core()runs → stashessys.modulesentries but leavessys.pathandsys.meta_pathuntouched.six.pyas a plain file;six.__path__ == []. Whentank_vendor.six.movesis requested,CoreImportHandler.find_specreturnsNone(emptypackage_path), and the stale_TankVendorMetaFinderintercepts the import — but the sitepkgs.ziphas nosix, so it fails.Flame 2025.1 + project config ≤ v1.10.20 →
ImportError: cannot import name 'sgsix' from 'shotgun_api3.lib'pkgs.zipis still atsys.path[0].shotgun_api3is not inNAMESPACES_TO_TRACK, soCoreImportHandlerignores it.PathFinderresolvesshotgun_api3from the sitepkgs.zip, which has a newer version withoutsgsix. Project code that expectssgsixfails.Changes
python/tank/bootstrap/import_handler.py—_swap_core(): strip outgoing-coresys.pathentries and remove stale_TankVendorMetaFinderbefore resettingself._core_path.Related
Closes / supersedes the diagnostic-only approach in #1099.