Skip to content

AutoGPT is the vision of accessible AI for everyone, to use and to build on. Our mission is to provide the tools, so that you can focus on what matters.

fix(backend/executor): load the block registry before taking runs (#15015)

Bently62a38d53be2f
diff --git a/autogpt_platform/backend/backend/executor/manager.py b/autogpt_platform/backend/backend/executor/manager.py
index c065a93303..26f1e98b4f 100644
--- a/autogpt_platform/backend/backend/executor/manager.py
+++ b/autogpt_platform/backend/backend/executor/manager.py
@@ -18,7 +18,7 @@ from sentry_sdk.api import capture_exception as _sentry_capture_exception
from sentry_sdk.api import flush as _sentry_flush
from sentry_sdk.api import get_current_scope as _sentry_get_current_scope
-from backend.blocks import get_block
+from backend.blocks import get_block, get_blocks
from backend.blocks._base import BlockSchema
from backend.blocks.agent import AgentExecutorBlock
from backend.blocks.mcp.block import MCPToolBlock
@@ -1649,6 +1649,16 @@ class ExecutionManager(AppProcess):
addr=settings.config.pyro_host,
)
+ # Load the block registry before taking runs. Otherwise the first runs
+ # on a fresh pod load it inside get_block(), which takes tens of seconds
+ # on a busy host, and a stop request for such a run times out meanwhile.
+ load_started = time.monotonic()
+ block_count = len(get_blocks())
+ logger.info(
+ f"[{self.service_name}] Loaded {block_count} blocks in "
+ f"{time.monotonic() - load_started:.1f}s"
+ )
+
self.cancel_thread.start()
self.run_thread.start()
diff --git a/autogpt_platform/backend/backend/executor/manager_block_warmup_test.py b/autogpt_platform/backend/backend/executor/manager_block_warmup_test.py
new file mode 100644
index 0000000000..e233cf079f
--- /dev/null
+++ b/autogpt_platform/backend/backend/executor/manager_block_warmup_test.py
@@ -0,0 +1,41 @@
+"""The executor must load the block registry before it takes any run."""
+
+from unittest.mock import MagicMock, patch
+
+import pytest
+
+from backend.executor.manager import ExecutionManager
+
+
+class _StopRunLoop(Exception):
+ pass
+
+
+def test_run_loads_blocks_before_taking_runs() -> None:
+ """Pins the warm-up ahead of the run consumer.
+
+ Without it the first runs on a fresh pod load every block inside
+ get_block(), and a stop request for one of them times out while that load
+ finishes (the library "start and stop a saved task" e2e flake).
+ """
+ manager = ExecutionManager()
+ order: list[str] = []
+ manager._cancel_thread = MagicMock()
+ manager._cancel_thread.start.side_effect = lambda: order.append("cancel_thread")
+ manager._run_thread = MagicMock()
+ manager._run_thread.start.side_effect = lambda: order.append("run_thread")
+
+ def load_blocks() -> dict:
+ order.append("load_blocks")
+ return {}
+
+ with (
+ patch("backend.blocks.load_all_blocks", side_effect=load_blocks),
+ patch("backend.executor.manager.start_http_server"),
+ patch.object(ExecutionManager, "_update_prompt_metrics"),
+ patch("backend.executor.manager.time.sleep", side_effect=_StopRunLoop),
+ pytest.raises(_StopRunLoop),
+ ):
+ manager.run()
+
+ assert order == ["load_blocks", "cancel_thread", "run_thread"]