{"record":{"id":"71fef8c5e6fc0580","repo":"github/copilot-sdk","slug":"an-in-process-ffi-runtime-library-is-already-loade-71fef8","errorCode":null,"errorMessage":"An in-process FFI runtime library is already loaded from '{_loaded_library_path}'; loading a different library from '{library_path}' in the same process is not supported.","messagePattern":"An in-process FFI runtime library is already loaded from '(.+?)'; loading a different library from '(.+?)' in the same process is not supported\\.","errorType":"exception","errorClass":"RuntimeError","httpStatus":null,"severity":"error","filePath":"python/copilot/_ffi_runtime_host.py","lineNumber":206,"sourceCode":"        self.connection_write = getattr(lib, f\"{_SYMBOL_PREFIX}connection_write\")\n        self.connection_write.argtypes = [\n            ctypes.c_uint32,\n            ctypes.c_char_p,\n            ctypes.c_size_t,\n        ]\n        self.connection_write.restype = ctypes.c_bool\n\n        self.connection_close = getattr(lib, f\"{_SYMBOL_PREFIX}connection_close\")\n        self.connection_close.argtypes = [ctypes.c_uint32]\n        self.connection_close.restype = ctypes.c_bool\n\n\ndef _load_library(library_path: str) -> _FfiLibrary:\n    global _loaded_library, _loaded_library_path\n    with _load_lock:\n        if _loaded_library is not None:\n            if _loaded_library_path != library_path:\n                raise RuntimeError(\n                    f\"An in-process FFI runtime library is already loaded from \"\n                    f\"'{_loaded_library_path}'; loading a different library from \"\n                    f\"'{library_path}' in the same process is not supported.\"\n                )\n            return _FfiLibrary(_loaded_library)\n\n        # Load with immediate binding (RTLD_NOW) on POSIX, matching the .NET/Rust\n        # hosts. The runtime cdylib from the platform release package is self-contained;\n        # eager binding surfaces any load problem here rather than at first call.\n        if sys.platform == \"win32\":\n            lib = ctypes.WinDLL(library_path)\n        else:\n            lib = ctypes.CDLL(library_path, mode=os.RTLD_NOW | os.RTLD_LOCAL)\n        _loaded_library = lib\n        _loaded_library_path = library_path\n        return _FfiLibrary(lib)\n\n","sourceCodeStart":188,"sourceCodeEnd":224,"githubUrl":"https://github.com/github/copilot-sdk/blob/cd8cf15dc3f9e762615790aaed0a771a0f392755/python/copilot/_ffi_runtime_host.py#L188-L224","documentation":"_load_library enforces one FFI runtime library per process: once a native runtime is loaded, loading a library from a different path is rejected because the process cannot safely host two runtimes. Loading the same path again returns the cached handle.","triggerScenarios":"Constructing FfiRuntimeHost (via __init__/create) twice in one process with different resolved library_path values — e.g. one instance from a cached CLI path and another from a custom/in-process runtime path.","commonSituations":"Mixing a downloaded CLI runtime with a bundled in-process runtime in tests; two library versions resolving to different absolute paths; pytest fixtures creating hosts with different paths.","solutions":["Use a single FfiRuntimeHost per process, or reuse the existing one instead of creating a new host with a different library path.","Ensure all hosts resolve to the exact same absolute runtime library path (same version/install location).","If you must switch runtimes, do it in a separate subprocess.","Dispose the old host and reload only if the library truly matches the previously loaded path."],"exampleFix":null,"handlingStrategy":"validation","validationCode":"from copilot import _ffi_runtime_host as h\nif h._loaded_library is not None and h._loaded_library_path != str(Path(library_path).resolve()):\n    raise RuntimeError(\"different FFI runtime already loaded; reuse existing host\")","typeGuard":null,"tryCatchPattern":"try:\n    host = FfiRuntimeHost.create(library_path)\nexcept RuntimeError as e:\n    if \"already loaded\" in str(e):\n        host = get_existing_host()","preventionTips":["Use one FfiRuntimeHost (module-level singleton) per process.","Resolve all runtime paths to the same absolute location.","Run multi-runtime tests in separate subprocesses."],"tags":["ffi","singleton","runtime","process"],"backgroundTag":"invalid-state-transition","analyzedSha":"cd8cf15dc3f9e762615790aaed0a771a0f392755","analyzedAt":"2026-09-09T18:32:31.973Z","contentChangedAt":"2026-09-09T18:32:31.973Z","schemaVersion":2},"datasetVersion":"2026-09-17T15:17:12.973Z"}