Skip to content

Commit cb74f10

Browse files
bpiwowarclaude
andcommitted
fix: handle broken codecarbon installations in CI
- Catch non-ImportError exceptions from broken codecarbon installs (e.g., missing nordic_emissions.json) and clean up sys.modules - Widen except clause in _display_carbon_tracking_status to catch all exceptions, not just ImportError - Disable carbon tracking by default in test settings - Pass no_environmental_impact=True in restart test subprocess - Add global pytest timeout (300s) to prevent indefinite hangs Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent 5580c66 commit cb74f10

5 files changed

Lines changed: 19 additions & 6 deletions

File tree

pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,7 @@ log_cli_format = "[%(levelname)s] %(asctime)s %(name)s [%(process)d/%(threadName
162162
log_date_format = "%H:%M:%S"
163163
log_cli_date_format = "%H:%M:%S"
164164
addopts = "--order-dependencies --ignore-unknown-dependency"
165+
timeout = 300
165166

166167
markers = [
167168
# Core foundation (no dependencies)

src/experimaestro/carbon/__init__.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,16 @@ def _is_codecarbon_available() -> bool:
7272
import codecarbon # noqa: F401
7373
except ImportError:
7474
return False
75+
except Exception:
76+
# Handle broken installations (e.g., missing data files like
77+
# nordic_emissions.json). Clean up partially-loaded modules to prevent
78+
# subsequent imports from using a corrupted module object.
79+
import sys
80+
81+
for key in list(sys.modules):
82+
if key == "codecarbon" or key.startswith("codecarbon."):
83+
del sys.modules[key]
84+
return False
7585

7686
system = platform.system()
7787
machine = platform.machine()

src/experimaestro/scheduler/experiment.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -344,8 +344,8 @@ def _display_carbon_tracking_status(self) -> None:
344344
"[Carbon Tracking] Disabled - %s",
345345
reason or "Not available",
346346
)
347-
except ImportError:
348-
# Carbon module not available at all
347+
except Exception:
348+
# Carbon module not available or broken installation
349349
if carbon_settings.warn_if_unavailable:
350350
self._show_carbon_warning(None)
351351
else:

src/experimaestro/settings.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import os
1+
import sys
22
from omegaconf import OmegaConf, SCMode
33
from dataclasses import field, dataclass
44
from functools import lru_cache
@@ -134,8 +134,10 @@ class Settings:
134134

135135
@lru_cache()
136136
def get_settings(path: Optional[Path] = None) -> Settings:
137-
if "PYTEST_CURRENT_TEST" in os.environ:
138-
return Settings()
137+
if getattr(sys, "_called_from_test", False):
138+
settings = Settings()
139+
settings.carbon.enabled = False
140+
return settings
139141
else:
140142
schema = OmegaConf.structured(Settings)
141143

src/experimaestro/tests/restart_main.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,5 @@
77
wspath, module, functionname = sys.argv[1:]
88
print("Importing", module)
99
f = getattr(importlib.import_module(module), functionname)
10-
with experiment(wspath, "restart") as xp:
10+
with experiment(wspath, "restart", no_environmental_impact=True) as xp:
1111
f(xp)

0 commit comments

Comments
 (0)