{"record":{"id":"34e05681911a45f4","repo":"langchain-ai/deepagents","slug":"extension-import-in-source-path-attempted-to-exi","errorCode":null,"errorMessage":"Extension import in {source.path} attempted to exit: {exc}","messagePattern":"Extension import in (.+?) attempted to exit: (.+?)","errorType":"exception","errorClass":"ExtensionError","httpStatus":null,"severity":"error","filePath":"libs/code/deepagents_code/extensions/loader.py","lineNumber":54,"sourceCode":"        else None,\n    )\n    if spec is None or spec.loader is None:\n        msg = f\"Could not import extension {source.path}\"\n        raise ExtensionError(msg)\n    module = importlib.util.module_from_spec(spec)\n    sys.modules[name] = module\n    try:\n        spec.loader.exec_module(module)\n    except (KeyboardInterrupt, SystemExit, Exception) as exc:\n        sys.modules.pop(name, None)\n        if isinstance(exc, KeyboardInterrupt):\n            raise\n        msg = (\n            f\"Extension import in {source.path} attempted to exit: {exc}\"\n            if isinstance(exc, SystemExit)\n            else f\"Failed to import {source.path}: {exc}\"\n        )\n        raise ExtensionError(msg) from exc\n    factory = getattr(module, \"extension\", None)\n    if not callable(factory):\n        sys.modules.pop(name, None)\n        msg = f\"{source.path} does not define a callable 'extension' factory\"\n        raise ExtensionError(msg)\n    if not inspect.iscoroutinefunction(factory):\n        sys.modules.pop(name, None)\n        msg = f\"Extension factory in {source.path} must be declared with 'async def'\"\n        raise ExtensionError(msg)\n    return name, factory\n\n\nasync def load_extension(\n    source: SourceInfo,\n    registry: ExtensionRegistry,\n    *,\n    cwd: Path,\n    mode: ExtensionMode,","sourceCodeStart":36,"sourceCodeEnd":72,"githubUrl":"https://github.com/langchain-ai/deepagents/blob/a1af029e6e73cb17c36bff823d227747b28e91e1/libs/code/deepagents_code/extensions/loader.py#L36-L72","documentation":"Raised by _import_factory when executing the extension module raises SystemExit — the extension called sys.exit() (or exit()) at import time. The loader converts this into an ExtensionError so a stray exit call cannot terminate the host process, and cleans the module from sys.modules.","triggerScenarios":"An extension module calls sys.exit(), exit(), or raises SystemExit at top level or during import-time initialization (e.g. argparse failing inside module code).","commonSituations":"Extension scripts written as CLI programs that call sys.exit when checks fail; copy-pasted argparse blocks running at import; guard code exiting when an env var is missing.","solutions":["Remove sys.exit()/exit() calls from the extension module's import-time code","Move CLI/argparse logic under `if __name__ == \"__main__\":`","Raise an exception or log a warning instead of exiting during import","Test importing the extension module directly with `python -c \"import <module>\"` to reproduce"],"exampleFix":"// before\nif not os.environ.get(\"API_KEY\"):\n    sys.exit(\"API_KEY missing\")\n\n// after\nif not os.environ.get(\"API_KEY\"):\n    raise RuntimeError(\"API_KEY missing\")  # or defer check to factory call","handlingStrategy":"try-catch","validationCode":"# smoke-test import in-process before registering\nimport importlib.util, sys\nspec = importlib.util.spec_from_file_location(\"_ext_probe\", path)\nmod = importlib.util.module_from_spec(spec)\ntry:\n    spec.loader.exec_module(mod)\nexcept SystemExit:\n    raise RuntimeError(\"extension calls sys.exit at import time\")","typeGuard":null,"tryCatchPattern":"try:\n    load_extension(source)\nexcept ExtensionError as exc:\n    logger.error(\"extension attempted to exit: %s\", exc)","preventionTips":["Never call sys.exit()/exit() in import-time code","Guard CLI code with `if __name__ == \"__main__\":`","Raise exceptions (catchable by the loader) instead of exiting"],"tags":["extensions","loader","systemexit","import"],"backgroundTag":"sys-exit-during-import","analyzedSha":"a1af029e6e73cb17c36bff823d227747b28e91e1","analyzedAt":"2026-08-29T11:43:24.718Z","schemaVersion":2},"datasetVersion":"2026-08-29T12:17:43.993Z"}