Skip to content

: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.

fix(warehouse-sources): run chdb without the preloaded jemalloc (#104806)

Raúl Negrón-Otero3eb032fd86ce
diff --git a/products/warehouse_sources/backend/models/table.py b/products/warehouse_sources/backend/models/table.py
index 998c83365366..04058b2a3a7e 100644
--- a/products/warehouse_sources/backend/models/table.py
+++ b/products/warehouse_sources/backend/models/table.py
@@ -1,3 +1,4 @@
+import os
import csv
import sys
import time
@@ -251,6 +252,12 @@ except Exception as e:
sys.stdout.write(str(result))
"""
+# chdb loads its engine with RTLD_DEEPBIND and passes every pointer its bundled jemalloc does not
+# own to glibc free(). When LD_PRELOAD puts another jemalloc under the interpreter, the interpreter's
+# heap belongs to that jemalloc, so glibc aborts the chdb import. MALLOC_CONF carries the tuning for
+# that preloaded jemalloc.
+_CHDB_SUBPROCESS_EXCLUDED_ENV_VARS = frozenset({"LD_PRELOAD", "MALLOC_CONF"})
+
def run_chdb_query(query: str, timeout: float = CHDB_QUERY_TIMEOUT_SECONDS) -> str:
# The query is passed over stdin because it embeds S3 credentials — argv is world-readable.
@@ -263,6 +270,7 @@ def run_chdb_query(query: str, timeout: float = CHDB_QUERY_TIMEOUT_SECONDS) -> s
capture_output=True,
text=True,
timeout=timeout,
+ env={name: value for name, value in os.environ.items() if name not in _CHDB_SUBPROCESS_EXCLUDED_ENV_VARS},
)
except subprocess.TimeoutExpired:
raise RuntimeError(f"chdb query timed out after {timeout}s")
diff --git a/products/warehouse_sources/backend/tests/test_table.py b/products/warehouse_sources/backend/tests/test_table.py
index 0f112bc2d689..a89331ef7913 100644
--- a/products/warehouse_sources/backend/tests/test_table.py
+++ b/products/warehouse_sources/backend/tests/test_table.py
@@ -349,6 +349,21 @@ class TestRunChdbQuery:
with pytest.raises(RuntimeError, match="timed out"):
run_chdb_query("SELECT sleep(2)", timeout=0.5)
+ def test_child_process_does_not_inherit_a_preloaded_allocator(self, monkeypatch: pytest.MonkeyPatch) -> None:
+ monkeypatch.setenv("LD_PRELOAD", "/usr/lib/aarch64-linux-gnu/libjemalloc.so.2")
+ monkeypatch.setenv("MALLOC_CONF", "background_thread:false")
+ monkeypatch.setenv("AWS_REGION", "us-east-1")
+ completed = subprocess.CompletedProcess(args=[], returncode=0, stdout="1\n", stderr="")
+ with patch(
+ "products.warehouse_sources.backend.models.table.subprocess.run", return_value=completed
+ ) as mock_run:
+ run_chdb_query("SELECT 1")
+
+ child_env = mock_run.call_args.kwargs["env"]
+ assert "LD_PRELOAD" not in child_env
+ assert "MALLOC_CONF" not in child_env
+ assert child_env["AWS_REGION"] == "us-east-1"
+
@pytest.mark.parametrize(
"stderr",
[