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(data-warehouse): eagerly load import sources at worker boot (#61067)

Julian Bez1b8e92c43605
diff --git a/posthog/management/commands/start_temporal_worker.py b/posthog/management/commands/start_temporal_worker.py
index b32d1b7680cc..e232c3f6a4b5 100644
--- a/posthog/management/commands/start_temporal_worker.py
+++ b/posthog/management/commands/start_temporal_worker.py
@@ -6,6 +6,7 @@ import datetime as dt
import functools
import threading
import faulthandler
+import collections.abc
from collections import defaultdict
import structlog
@@ -48,6 +49,7 @@ from posthog.temporal.data_imports.settings import (
EMIT_SIGNALS_WORKFLOWS as DATA_IMPORT_EMIT_SIGNALS_WORKFLOWS,
WORKFLOWS as DATA_SYNC_WORKFLOWS,
)
+from posthog.temporal.data_imports.sources import load_all_sources
from posthog.temporal.data_modeling import (
ACTIVITIES as DATA_MODELING_ACTIVITIES,
WORKFLOWS as DATA_MODELING_WORKFLOWS,
@@ -371,6 +373,11 @@ WORKFLOWS_DICT = _workflows
ACTIVITIES_DICT = _activities
+def workflows_include_data_import_syncs(workflows: collections.abc.Iterable[type]) -> bool:
+ """True when this worker runs data-warehouse source syncs and must eagerly load the sources."""
+ return any(wf in DATA_SYNC_WORKFLOWS for wf in workflows)
+
+
if settings.DEBUG:
TASK_QUEUE_METRIC_PREFIXES = {}
else:
@@ -504,6 +511,16 @@ class Command(BaseCommand):
except KeyError:
raise ValueError(f'Task queue "{task_queue}" not found in WORKFLOWS_DICT or ACTIVITIES_DICT')
+ # Data-import source modules import vendor SDKs (google-ads, etc.) at module scope, and those
+ # SDKs register protobuf descriptors into a process-global pool that rejects a second
+ # registration of the same symbol. They must be imported exactly once per process. Do it here
+ # — synchronously, at worker boot, on the main thread — for any queue that runs data syncs.
+ # Deferring to the first SourceRegistry.get_source() at runtime (the lazy path) let the
+ # registration recur and broke unrelated syncs with "duplicate symbol". Other workers never
+ # import the vendor SDKs, so they keep their fast startup.
+ if workflows_include_data_import_syncs(workflows):
+ load_all_sources()
+
if options["client_key"]:
options["client_key"] = "--SECRET--"
diff --git a/posthog/management/commands/test/test_start_temporal_worker.py b/posthog/management/commands/test/test_start_temporal_worker.py
new file mode 100644
index 000000000000..5be99499edbd
--- /dev/null
+++ b/posthog/management/commands/test/test_start_temporal_worker.py
@@ -0,0 +1,25 @@
+import pytest
+
+from posthog.management.commands.start_temporal_worker import DATA_SYNC_WORKFLOWS, workflows_include_data_import_syncs
+
+
+class _NotADataSyncWorkflow:
+ pass
+
+
+# Data-import sources import vendor SDKs (google-ads, etc.) that register protobuf descriptors into a
+# process-global pool exactly once. The worker eagerly loads them at boot only for queues that run
+# data syncs; everything else stays lazy to keep startup fast. Queue settings collapse to a single
+# dev queue under DEBUG, so assert the gating predicate directly against workflow sets.
+ "workflows,expected",
+ [
+ (list(DATA_SYNC_WORKFLOWS), True),
+ ([DATA_SYNC_WORKFLOWS[0]], True),
+ ([DATA_SYNC_WORKFLOWS[0], _NotADataSyncWorkflow], True),
+ ([_NotADataSyncWorkflow], False),
+ ([], False),
+ ],
+)
+def test_only_data_import_queues_warm_sources(workflows: list[type], expected: bool) -> None:
+ assert workflows_include_data_import_syncs(workflows) is expected