{"record":{"id":"8fe6f0c67d832d05","repo":"microsoft/autogen","slug":"pip-install-was-cancelled","errorCode":null,"errorMessage":"Pip install was cancelled","messagePattern":"Pip install was cancelled","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"warning","filePath":"python/packages/autogen-ext/src/autogen_ext/code_executors/local/__init__.py","lineNumber":309,"sourceCode":"                py_executable = sys.executable\n\n            task = asyncio.create_task(\n                asyncio.create_subprocess_exec(\n                    py_executable,\n                    *cmd_args,\n                    cwd=self.work_dir,\n                    stdout=asyncio.subprocess.PIPE,\n                    stderr=asyncio.subprocess.PIPE,\n                )\n            )\n            cancellation_token.link_future(task)\n            try:\n                proc = await task\n                stdout, stderr = await asyncio.wait_for(proc.communicate(), self._timeout)\n            except asyncio.TimeoutError as e:\n                raise ValueError(\"Pip install timed out\") from e\n            except asyncio.CancelledError as e:\n                raise ValueError(\"Pip install was cancelled\") from e\n\n            if proc.returncode is not None and proc.returncode != 0:\n                raise ValueError(f\"Pip install failed. {stdout.decode()}, {stderr.decode()}\")\n\n        # Attempt to load the function file to check for syntax errors, imports etc.\n        exec_result = await self._execute_code_dont_check_setup(\n            [CodeBlock(code=func_file_content, language=\"python\")], cancellation_token\n        )\n\n        if exec_result.exit_code != 0:\n            raise ValueError(f\"Functions failed to load: {exec_result.output}\")\n\n        self._setup_functions_complete = True\n\n    async def execute_code_blocks(\n        self, code_blocks: List[CodeBlock], cancellation_token: CancellationToken\n    ) -> CommandLineCodeResult:\n        \"\"\"(Experimental) Execute the code blocks and return the result.","sourceCodeStart":291,"sourceCodeEnd":327,"githubUrl":"https://github.com/microsoft/autogen/blob/027ecf0a379bcc1d09956d46d12d44a3ad9cee14/python/packages/autogen-ext/src/autogen_ext/code_executors/local/__init__.py#L291-L327","documentation":"Raised during LocalCommandLineCodeExecutor function setup when the pip install subprocess future/task is cancelled - either via the CancellationToken passed to execute_code_blocks (linked through cancellation_token.link_future) or by cancelling the surrounding task - and asyncio.CancelledError is caught and rewrapped as ValueError.","triggerScenarios":"Cancelling the CancellationToken that was passed to execute_code_blocks while the deferred pip install of function dependencies is still running; shutting down the event loop or cancelling the surrounding task during the first code execution.","commonSituations":"User aborts or shuts down an agent session during the first code block (when lazy setup runs), orchestration layers with global timeouts that cancel in-flight tasks, tests cancelling tokens to simulate user aborts.","solutions":["If cancellation was intentional, no fix is needed - treat it as a clean abort and handle the ValueError in the caller.","Trigger setup earlier by calling execute_code_blocks with a trivial block before exposing cancellation controls to users.","Raise the executor timeout so setup is not externally cancelled as 'stuck'."],"exampleFix":"# before\ntoken = CancellationToken()\ntask = asyncio.create_task(executor.execute_code_blocks(blocks, token))\ntoken.cancel()  # -> ValueError: Pip install was cancelled\n\n# after\nresult = await executor.execute_code_blocks(blocks, CancellationToken())\n# cancel only for genuine aborts; catch and treat as aborted:\n#   except ValueError as e: ... if 'cancelled' in str(e): handle_abort()","handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"try:\n    await executor.execute_code_blocks(blocks, token)\nexcept ValueError as e:\n    if \"Pip install was cancelled\" in str(e):\n        return AbortedResult()  # treat as a clean user abort\n    raise","preventionTips":["Trigger lazy setup early with a trivial code block before exposing cancel controls to users.","Only cancel tokens for genuine aborts, not as a timeout mechanism during setup."],"tags":["cancellation","pip","asyncio","local-executor"],"backgroundTag":null,"analyzedSha":"027ecf0a379bcc1d09956d46d12d44a3ad9cee14","analyzedAt":"2026-08-15T03:38:00.719Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}