:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
fix(tasks): preserve the failing tail in dev-stack bake errors (#103734)
Alessandro Pogliaghi1d49c70a1f07
diff --git a/products/tasks/backend/logic/services/dev_stack_image.py b/products/tasks/backend/logic/services/dev_stack_image.pyindex c0df09828ed7..ebb736593e48 100644--- a/products/tasks/backend/logic/services/dev_stack_image.py+++ b/products/tasks/backend/logic/services/dev_stack_image.py@@ -50,7 +50,7 @@ BAKE_SANDBOX_MEMORY_GB = 32.0PUBLISH_SNAPSHOT_MAX_ATTEMPTS = 3# Tail of bake output retained for error reporting.-MAX_BAKE_LOG_CHARS = 20_000+MAX_BAKE_LOG_BYTES = 8_000# VM base image reference the last successful bake layered on, so the refresh sweep can# tell whether the published image is stale (mirrors SandboxCustomImage.base_image_reference).@@ -194,20 +194,20 @@ def bake_dev_stack_image(publish_name: str = DEV_STACK_IMAGE_NAME) -> str:try:sandbox.write_file(BAKE_SCRIPT_SANDBOX_PATH, script.encode())- log_tail: deque[str] = deque()+ log_tail: deque[bytes] = deque()tail_len = 0stream = sandbox.execute_stream(f"bash {BAKE_SCRIPT_SANDBOX_PATH} 2>&1")for line in stream.iter_stdout():logger.info("dev_stack_image_bake_output", extra={"sandbox_id": sandbox.id, "line": line.rstrip()})- log_tail.append(line)- tail_len += len(line)- while tail_len > MAX_BAKE_LOG_CHARS and len(log_tail) > 1:+ encoded_line = line.encode("utf-8")[-MAX_BAKE_LOG_BYTES:]+ log_tail.append(encoded_line)+ tail_len += len(encoded_line)+ while tail_len > MAX_BAKE_LOG_BYTES and len(log_tail) > 1:tail_len -= len(log_tail.popleft())result = stream.wait()if result.exit_code != 0:- raise DevStackImageBakeError(- f"Bake script exited with {result.exit_code}; output tail:\n{''.join(log_tail)[-MAX_BAKE_LOG_CHARS:]}"- )+ output_tail = b"".join(log_tail)[-MAX_BAKE_LOG_BYTES:].decode("utf-8", errors="ignore")+ raise DevStackImageBakeError(f"Bake script exited with {result.exit_code}; output tail:\n{output_tail}")image_id = _publish_snapshot_with_retries(sandbox, publish_name)_record_baked_base_reference(publish_name, base_reference)diff --git a/products/tasks/backend/tests/test_dev_stack_image.py b/products/tasks/backend/tests/test_dev_stack_image.pyindex 301e1014ba4a..be9c36b9faf1 100644--- a/products/tasks/backend/tests/test_dev_stack_image.py+++ b/products/tasks/backend/tests/test_dev_stack_image.py@@ -112,14 +112,27 @@ class TestBakeDevStackImage:assert refresh_dev_stack_image_if_base_changed(publish_name) is Falsedispatch_mock.assert_not_called()- def test_failed_bake_never_publishes_but_still_destroys_sandbox(self):+ @pytest.mark.parametrize(+ "output",+ [[], ["warming step\n"] * 2_000, ["x" * 25_000], ["€" * 12_000]],+ ids=["empty", "many-lines", "long-line", "multibyte-line"],+ )+ def test_failed_bake_never_publishes_but_still_destroys_sandbox(self, output: list[str]) -> None:# Publishing after a failed bake would overwrite the last good image with a broken# one under the same name — every internal VM run would then boot from it.fake_cls = _make_fake_sandbox_cls(exit_code=1)- with patch("products.tasks.backend.logic.services.dev_stack_image.get_sandbox_class", return_value=fake_cls):- with pytest.raises(DevStackImageBakeError):+ failing_marker = "FATAL: migration command exited 1\n"+ final_line = (output[-1] if output else "") + failing_marker+ with (+ patch("products.tasks.backend.logic.services.dev_stack_image.get_sandbox_class", return_value=fake_cls),+ patch.object(_FakeStream, "iter_stdout", return_value=iter([*output[:-1], final_line])),+ ):+ with pytest.raises(DevStackImageBakeError) as exc_info:bake_dev_stack_image(_unique_publish_name())+ message = str(exc_info.value)+ assert message.endswith(failing_marker)+ assert len(message.encode("utf-8")) < 10_000(sandbox,) = fake_cls.instancesassert sandbox.published_name is Noneassert sandbox.destroyed is True