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.

2.6 KB
Raw Blame
rules:
    - id: query-runners-propagate-thread-context
      message: |
          Bare `threading.Thread` in a query runner. A new thread starts with empty ContextVars, so
          the worker loses everything the query path reads from context: the OpenTelemetry span
          context, the ClickHouse query tags, and the warehouse-warning accumulator. Losing the span
          context means each worker opens its spans with no parent, so one parallel insight reports
          a separate trace per series instead of a single request trace, and the request trace shows
          an unexplained gap where the query work should be. Losing the query tags means the
          worker's ClickHouse queries land in `system.query_log` with no product or feature
          attribution. A raw thread per work item is also unbounded, so one request can take a web
          worker's threads and a large share of the shared Postgres connection pool.

          Use `run_in_parallel_threads` from `posthog.hogql_queries.utils.parallel`, which runs the
          work in a bounded pool and copies the caller's context into every worker. Pass thunks
          (`functools.partial`) instead of `target=`/`args=`:

              run_in_parallel_threads([partial(run, index, query) for index, query in enumerate(queries)])

          Use `map_in_caller_context` from `posthog.hogql_queries.utils.caller_context` instead when
          the work returns values rather than writing into a shared result list. It runs serially
          under `TEST`, so a test that must reach the pool cannot use it.

          A hand-rolled `ThreadPoolExecutor` loses the context the same way, and this rule does not
          detect that form. Submit a fresh `contextvars.copy_context().run` per task, as
          `posthog/hogql_queries/utils/timestamp_utils.py` does. One `Context` cannot be entered
          twice, so hoisting a single `copy_context()` above the loop raises `RuntimeError` on the
          second task.

          For a thread that genuinely must not inherit the caller's context, add
          `# nosemgrep: query-runners-propagate-thread-context` with a one-line why.
      severity: WARNING
      languages: [python]
      # Anchored at the repo root, which is where CI scans from. The leading slash also opts out of
      # semgrep's Semgrepignore v2 deprecation warning for unanchored patterns.
      paths:
          include:
              - '/posthog/hogql_queries/**'
              - '/products/*/backend/hogql_queries/**'
      pattern-either:
          - pattern: threading.Thread(...)
          - pattern: Thread(...)
      metadata:
          category: best-practice
          subcategory:
              - audit