: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(clickhouse): close inherited descriptors in udf wrapper scripts (#105651)
Sandy Spicerdc81b02d0deb
diff --git a/.github/workflows/ci-clickhouse-util-udfs.yml b/.github/workflows/ci-clickhouse-util-udfs.ymlindex 4d422c712df4..a34723457928 100644--- a/.github/workflows/ci-clickhouse-util-udfs.yml+++ b/.github/workflows/ci-clickhouse-util-udfs.yml@@ -6,6 +6,7 @@ on:- '.github/workflows/ci-clickhouse-util-udfs.yml'- 'clickhouse-udfs/util/**'- 'docker/clickhouse/user_defined_function.xml'+ - 'posthog/user_scripts/run_udf.sh'- 'posthog/user_scripts/decompress_udf*'- 'posthog/user_scripts/json_clean_posthog_event_properties_udf*'- 'posthog/user_scripts/json_drop_keys_udf*'diff --git a/clickhouse-udfs/util/scripts/integration_test.sh b/clickhouse-udfs/util/scripts/integration_test.shindex e22bb70fd871..6bc0a66e33a9 100755--- a/clickhouse-udfs/util/scripts/integration_test.sh+++ b/clickhouse-udfs/util/scripts/integration_test.sh@@ -143,3 +143,44 @@ if [[ "$output" != $'1\t1\t1' ]]; thenexit 1fiecho "Passed nested-array quarantine JSON casts."++# ClickHouse started each pooled UDF process above while a client connection was open. A UDF process that keeps a+# copy of a client socket holds the connection open after the server closes it, so the client's next request on+# that connection never gets a response. Run as the clickhouse user, which owns the UDF processes.+docker compose -f "$COMPOSE_FILE" exec -T -u clickhouse clickhouse bash -s <<'BASH'+set -euo pipefail+checked=0+leaks=0+for proc in /proc/[0-9]*; do+ args=()+ mapfile -d '' args 2>/dev/null < "$proc/cmdline" || continue+ # A wrapper runs as `<shell> /var/lib/clickhouse/user_scripts/<script>`, and it starts its binary from /tmp.+ if [[ "${args[1]:-}" != /var/lib/clickhouse/user_scripts/* && "${args[0]:-}" != /tmp/* ]]; then+ continue+ fi+ checked=$((checked + 1))+ inspected=0+ for fd in "$proc"/fd/*; do+ target=$(readlink "$fd" 2>/dev/null) || continue+ inspected=$((inspected + 1))+ if [[ "$target" == socket:* ]]; then+ echo "UDF process '${args[*]}' holds descriptor ${fd##*/} ($target)." >&2+ leaks=$((leaks + 1))+ fi+ done+ # A UDF process always has stdin open, so a live process with no readable descriptor means this check cannot+ # see its descriptors, for example because it runs as another user. That must fail, not pass as clean.+ if ((inspected == 0)) && [[ -e "$proc" ]]; then+ echo "Cannot read the descriptors of UDF process '${args[*]}'." >&2+ exit 1+ fi+done+if ((checked == 0)); then+ echo "Expected running pooled UDF processes, found none." >&2+ exit 1+fi+if ((leaks > 0)); then+ exit 1+fi+BASH+echo "Passed pooled UDF processes hold no sockets."diff --git a/posthog/user_scripts/aggregate_funnel b/posthog/user_scripts/aggregate_funnelindex 5231f2fd439d..b7e180ec43c7 100755--- a/posthog/user_scripts/aggregate_funnel+++ b/posthog/user_scripts/aggregate_funnel@@ -1,36 +1,2 @@#!/bin/bash--# ClickHouse starts this script with copies of its open files, including client connections. A connection stays open-# while any copy of it is open, so a long-running UDF process keeps a connection open after the server closes it,-# and the next request on that connection never gets a response. Close every inherited descriptor except stdin,-# stdout and stderr. The script needs bash because sh cannot close a descriptor above 9.-for fd_path in /proc/$$/fd/*; do- fd=${fd_path##*/}- if [ "$fd" -gt 2 ]; then- exec {fd}>&-- fi-done--DIR_NAME=$(dirname "$0")--# Create a temporary directory in /tmp-TEMP_DIR=$(mktemp -d -p /tmp "aggregate_funnel_XXXXXXXXXX")--# Trap to clean up the temporary directory on exit-trap 'rm -rf "$TEMP_DIR"; exit' 0--# Determine which executable to use based on architecture-case $( uname -m ) in-aarch64)- EXECUTABLE_NAME="aggregate_funnel_aarch64"- ;;-*)- EXECUTABLE_NAME="aggregate_funnel_x86_64"- ;;-esac--# Copy the executable to a temporary location-cp "$DIR_NAME/$EXECUTABLE_NAME" "$TEMP_DIR/$EXECUTABLE_NAME"-chmod +x "$TEMP_DIR/$EXECUTABLE_NAME"-"$TEMP_DIR/$EXECUTABLE_NAME" "$@"-+. "$(dirname "$0")/run_udf.sh"diff --git a/posthog/user_scripts/aggregate_funnel_array_trends_test.py b/posthog/user_scripts/aggregate_funnel_array_trends_test.pyindex ae5d7f24387b..97da07a2e72d 100755--- a/posthog/user_scripts/aggregate_funnel_array_trends_test.py+++ b/posthog/user_scripts/aggregate_funnel_array_trends_test.py@@ -1,9 +1,13 @@#!/usr/bin/python3+import osimport sysimport jsonif __name__ == "__main__":+ # ClickHouse starts this script with copies of its open client connections. A retained copy keeps a connection+ # open after the server closes it, so the client's next request on that connection never gets a response.+ os.closerange(3, os.sysconf("SC_OPEN_MAX"))for line in sys.stdin:try:print(json.dumps({"result": line})) # noqa: T201diff --git a/posthog/user_scripts/aggregate_funnel_test.py b/posthog/user_scripts/aggregate_funnel_test.pyindex d524cc8e83e9..1473a11dfd6f 100755--- a/posthog/user_scripts/aggregate_funnel_test.py+++ b/posthog/user_scripts/aggregate_funnel_test.py@@ -1,9 +1,13 @@#!/usr/bin/python3+import osimport sysimport jsonimport tracebackif __name__ == "__main__":+ # ClickHouse starts this script with copies of its open client connections. A retained copy keeps a connection+ # open after the server closes it, so the client's next request on that connection never gets a response.+ os.closerange(3, os.sysconf("SC_OPEN_MAX"))for line in sys.stdin:try:print(json.dumps({"result": line})) # noqa: T201diff --git a/posthog/user_scripts/decompress_udf b/posthog/user_scripts/decompress_udfindex 28a05e2b75da..b7e180ec43c7 100755--- a/posthog/user_scripts/decompress_udf+++ b/posthog/user_scripts/decompress_udf@@ -1,29 +1,2 @@#!/bin/bash--# ClickHouse starts this script with copies of its open files, including client connections. A connection stays open-# while any copy of it is open, so a long-running UDF process keeps a connection open after the server closes it,-# and the next request on that connection never gets a response. Close every inherited descriptor except stdin,-# stdout and stderr. The script needs bash because sh cannot close a descriptor above 9.-for fd_path in /proc/$$/fd/*; do- fd=${fd_path##*/}- if [ "$fd" -gt 2 ]; then- exec {fd}>&-- fi-done--DIR_NAME=$(dirname "$0")-TEMP_DIR=$(mktemp -d -p /tmp "decompress_udf_XXXXXXXXXX")-trap 'rm -rf "$TEMP_DIR"; exit' 0--case $(uname -m) in-aarch64)- EXECUTABLE_NAME="decompress_udf_aarch64"- ;;-*)- EXECUTABLE_NAME="decompress_udf_x86_64"- ;;-esac--cp "$DIR_NAME/$EXECUTABLE_NAME" "$TEMP_DIR/$EXECUTABLE_NAME"-chmod +x "$TEMP_DIR/$EXECUTABLE_NAME"-"$TEMP_DIR/$EXECUTABLE_NAME" "$@"+. "$(dirname "$0")/run_udf.sh"diff --git a/posthog/user_scripts/json_clean_posthog_event_properties_udf b/posthog/user_scripts/json_clean_posthog_event_properties_udfindex 0ea02df38a9d..b7e180ec43c7 100755--- a/posthog/user_scripts/json_clean_posthog_event_properties_udf+++ b/posthog/user_scripts/json_clean_posthog_event_properties_udf@@ -1,29 +1,2 @@#!/bin/bash--# ClickHouse starts this script with copies of its open files, including client connections. A connection stays open-# while any copy of it is open, so a long-running UDF process keeps a connection open after the server closes it,-# and the next request on that connection never gets a response. Close every inherited descriptor except stdin,-# stdout and stderr. The script needs bash because sh cannot close a descriptor above 9.-for fd_path in /proc/$$/fd/*; do- fd=${fd_path##*/}- if [ "$fd" -gt 2 ]; then- exec {fd}>&-- fi-done--DIR_NAME=$(dirname "$0")-TEMP_DIR=$(mktemp -d -p /tmp "json_clean_posthog_event_properties_udf_XXXXXXXXXX")-trap 'rm -rf "$TEMP_DIR"; exit' 0--case $(uname -m) in-aarch64)- EXECUTABLE_NAME="json_clean_posthog_event_properties_udf_aarch64"- ;;-*)- EXECUTABLE_NAME="json_clean_posthog_event_properties_udf_x86_64"- ;;-esac--cp "$DIR_NAME/$EXECUTABLE_NAME" "$TEMP_DIR/$EXECUTABLE_NAME"-chmod +x "$TEMP_DIR/$EXECUTABLE_NAME"-"$TEMP_DIR/$EXECUTABLE_NAME" "$@"+. "$(dirname "$0")/run_udf.sh"diff --git a/posthog/user_scripts/json_drop_keys_udf b/posthog/user_scripts/json_drop_keys_udfindex cc9c5ffdadf9..b7e180ec43c7 100755--- a/posthog/user_scripts/json_drop_keys_udf+++ b/posthog/user_scripts/json_drop_keys_udf@@ -1,35 +1,2 @@#!/bin/bash--# ClickHouse starts this script with copies of its open files, including client connections. A connection stays open-# while any copy of it is open, so a long-running UDF process keeps a connection open after the server closes it,-# and the next request on that connection never gets a response. Close every inherited descriptor except stdin,-# stdout and stderr. The script needs bash because sh cannot close a descriptor above 9.-for fd_path in /proc/$$/fd/*; do- fd=${fd_path##*/}- if [ "$fd" -gt 2 ]; then- exec {fd}>&-- fi-done--DIR_NAME=$(dirname "$0")--# Create a temporary directory in /tmp-TEMP_DIR=$(mktemp -d -p /tmp "json_drop_keys_udf_XXXXXXXXXX")--# Trap to clean up the temporary directory on exit-trap 'rm -rf "$TEMP_DIR"; exit' 0--# Determine which executable to use based on architecture-case $( uname -m ) in-aarch64)- EXECUTABLE_NAME="json_drop_keys_udf_aarch64"- ;;-*)- EXECUTABLE_NAME="json_drop_keys_udf_x86_64"- ;;-esac--# Copy the executable to a temporary location-cp "$DIR_NAME/$EXECUTABLE_NAME" "$TEMP_DIR/$EXECUTABLE_NAME"-chmod +x "$TEMP_DIR/$EXECUTABLE_NAME"-"$TEMP_DIR/$EXECUTABLE_NAME" "$@"+. "$(dirname "$0")/run_udf.sh"diff --git a/posthog/user_scripts/json_strip_empty_strings_and_nulls_udf b/posthog/user_scripts/json_strip_empty_strings_and_nulls_udfindex 72f444f0ef3b..b7e180ec43c7 100755--- a/posthog/user_scripts/json_strip_empty_strings_and_nulls_udf+++ b/posthog/user_scripts/json_strip_empty_strings_and_nulls_udf@@ -1,29 +1,2 @@#!/bin/bash--# ClickHouse starts this script with copies of its open files, including client connections. A connection stays open-# while any copy of it is open, so a long-running UDF process keeps a connection open after the server closes it,-# and the next request on that connection never gets a response. Close every inherited descriptor except stdin,-# stdout and stderr. The script needs bash because sh cannot close a descriptor above 9.-for fd_path in /proc/$$/fd/*; do- fd=${fd_path##*/}- if [ "$fd" -gt 2 ]; then- exec {fd}>&-- fi-done--DIR_NAME=$(dirname "$0")-TEMP_DIR=$(mktemp -d -p /tmp "json_strip_empty_strings_and_nulls_udf_XXXXXXXXXX")-trap 'rm -rf "$TEMP_DIR"; exit' 0--case $(uname -m) in-aarch64)- EXECUTABLE_NAME="json_strip_empty_strings_and_nulls_udf_aarch64"- ;;-*)- EXECUTABLE_NAME="json_strip_empty_strings_and_nulls_udf_x86_64"- ;;-esac--cp "$DIR_NAME/$EXECUTABLE_NAME" "$TEMP_DIR/$EXECUTABLE_NAME"-chmod +x "$TEMP_DIR/$EXECUTABLE_NAME"-"$TEMP_DIR/$EXECUTABLE_NAME" "$@"+. "$(dirname "$0")/run_udf.sh"diff --git a/posthog/user_scripts/run_udf.sh b/posthog/user_scripts/run_udf.shnew file mode 100644index 000000000000..b4c59bc14cda--- /dev/null+++ b/posthog/user_scripts/run_udf.sh@@ -0,0 +1,36 @@+# shellcheck shell=bash+# Sourced by every wrapper in this directory. The wrapper's file name is the base name of the binary it starts, so+# every wrapper is the same two lines. Each version directory carries its own copy of this file, because a deployed+# version must not depend on files outside it.++# ClickHouse starts a wrapper with copies of its open files, including client connections. A connection stays open+# while any copy of it is open, so a long-running UDF process keeps a connection open after the server closes it,+# and the next request on that connection never gets a response. Close every inherited descriptor except stdin,+# stdout and stderr before anything else. The wrappers run under bash because sh cannot close a descriptor above 9.+for fd_path in /proc/$$/fd/*; do+ fd=${fd_path##*/}+ if (( fd > 2 )); then+ exec {fd}>&-+ fi+done++DIR_NAME=$(dirname "$0")+UDF_NAME=$(basename "$0")++TEMP_DIR=$(mktemp -d -p /tmp "${UDF_NAME}_XXXXXXXXXX")+trap 'rm -rf "$TEMP_DIR"; exit' 0++case $(uname -m) in+aarch64)+ EXECUTABLE_NAME="${UDF_NAME}_aarch64"+ ;;+*)+ EXECUTABLE_NAME="${UDF_NAME}_x86_64"+ ;;+esac++# A deploy replaces the binaries in user_scripts while pooled processes still run them. Each process runs a private+# copy, so the replacement never touches a file a process is executing.+cp "$DIR_NAME/$EXECUTABLE_NAME" "$TEMP_DIR/$EXECUTABLE_NAME"+chmod +x "$TEMP_DIR/$EXECUTABLE_NAME"+"$TEMP_DIR/$EXECUTABLE_NAME" "$@"diff --git a/posthog/user_scripts/v11/aggregate_funnel b/posthog/user_scripts/v11/aggregate_funnelindex 5231f2fd439d..b7e180ec43c7 100755--- a/posthog/user_scripts/v11/aggregate_funnel+++ b/posthog/user_scripts/v11/aggregate_funnel@@ -1,36 +1,2 @@#!/bin/bash--# ClickHouse starts this script with copies of its open files, including client connections. A connection stays open-# while any copy of it is open, so a long-running UDF process keeps a connection open after the server closes it,-# and the next request on that connection never gets a response. Close every inherited descriptor except stdin,-# stdout and stderr. The script needs bash because sh cannot close a descriptor above 9.-for fd_path in /proc/$$/fd/*; do- fd=${fd_path##*/}- if [ "$fd" -gt 2 ]; then- exec {fd}>&-- fi-done--DIR_NAME=$(dirname "$0")--# Create a temporary directory in /tmp-TEMP_DIR=$(mktemp -d -p /tmp "aggregate_funnel_XXXXXXXXXX")--# Trap to clean up the temporary directory on exit-trap 'rm -rf "$TEMP_DIR"; exit' 0--# Determine which executable to use based on architecture-case $( uname -m ) in-aarch64)- EXECUTABLE_NAME="aggregate_funnel_aarch64"- ;;-*)- EXECUTABLE_NAME="aggregate_funnel_x86_64"- ;;-esac--# Copy the executable to a temporary location-cp "$DIR_NAME/$EXECUTABLE_NAME" "$TEMP_DIR/$EXECUTABLE_NAME"-chmod +x "$TEMP_DIR/$EXECUTABLE_NAME"-"$TEMP_DIR/$EXECUTABLE_NAME" "$@"-+. "$(dirname "$0")/run_udf.sh"diff --git a/posthog/user_scripts/v11/aggregate_funnel_array_trends_test.py b/posthog/user_scripts/v11/aggregate_funnel_array_trends_test.pyindex ae5d7f24387b..97da07a2e72d 100755--- a/posthog/user_scripts/v11/aggregate_funnel_array_trends_test.py+++ b/posthog/user_scripts/v11/aggregate_funnel_array_trends_test.py@@ -1,9 +1,13 @@#!/usr/bin/python3+import osimport sysimport jsonif __name__ == "__main__":+ # ClickHouse starts this script with copies of its open client connections. A retained copy keeps a connection+ # open after the server closes it, so the client's next request on that connection never gets a response.+ os.closerange(3, os.sysconf("SC_OPEN_MAX"))for line in sys.stdin:try:print(json.dumps({"result": line})) # noqa: T201diff --git a/posthog/user_scripts/v11/aggregate_funnel_test.py b/posthog/user_scripts/v11/aggregate_funnel_test.pyindex d524cc8e83e9..1473a11dfd6f 100755--- a/posthog/user_scripts/v11/aggregate_funnel_test.py+++ b/posthog/user_scripts/v11/aggregate_funnel_test.py@@ -1,9 +1,13 @@#!/usr/bin/python3+import osimport sysimport jsonimport tracebackif __name__ == "__main__":+ # ClickHouse starts this script with copies of its open client connections. A retained copy keeps a connection+ # open after the server closes it, so the client's next request on that connection never gets a response.+ os.closerange(3, os.sysconf("SC_OPEN_MAX"))for line in sys.stdin:try:print(json.dumps({"result": line})) # noqa: T201diff --git a/posthog/user_scripts/v11/run_udf.sh b/posthog/user_scripts/v11/run_udf.shnew file mode 100644index 000000000000..b4c59bc14cda--- /dev/null+++ b/posthog/user_scripts/v11/run_udf.sh@@ -0,0 +1,36 @@+# shellcheck shell=bash+# Sourced by every wrapper in this directory. The wrapper's file name is the base name of the binary it starts, so+# every wrapper is the same two lines. Each version directory carries its own copy of this file, because a deployed+# version must not depend on files outside it.++# ClickHouse starts a wrapper with copies of its open files, including client connections. A connection stays open+# while any copy of it is open, so a long-running UDF process keeps a connection open after the server closes it,+# and the next request on that connection never gets a response. Close every inherited descriptor except stdin,+# stdout and stderr before anything else. The wrappers run under bash because sh cannot close a descriptor above 9.+for fd_path in /proc/$$/fd/*; do+ fd=${fd_path##*/}+ if (( fd > 2 )); then+ exec {fd}>&-+ fi+done++DIR_NAME=$(dirname "$0")+UDF_NAME=$(basename "$0")++TEMP_DIR=$(mktemp -d -p /tmp "${UDF_NAME}_XXXXXXXXXX")+trap 'rm -rf "$TEMP_DIR"; exit' 0++case $(uname -m) in+aarch64)+ EXECUTABLE_NAME="${UDF_NAME}_aarch64"+ ;;+*)+ EXECUTABLE_NAME="${UDF_NAME}_x86_64"+ ;;+esac++# A deploy replaces the binaries in user_scripts while pooled processes still run them. Each process runs a private+# copy, so the replacement never touches a file a process is executing.+cp "$DIR_NAME/$EXECUTABLE_NAME" "$TEMP_DIR/$EXECUTABLE_NAME"+chmod +x "$TEMP_DIR/$EXECUTABLE_NAME"+"$TEMP_DIR/$EXECUTABLE_NAME" "$@"diff --git a/posthog/user_scripts/v12/aggregate_funnel b/posthog/user_scripts/v12/aggregate_funnelindex 5231f2fd439d..b7e180ec43c7 100755--- a/posthog/user_scripts/v12/aggregate_funnel+++ b/posthog/user_scripts/v12/aggregate_funnel@@ -1,36 +1,2 @@#!/bin/bash--# ClickHouse starts this script with copies of its open files, including client connections. A connection stays open-# while any copy of it is open, so a long-running UDF process keeps a connection open after the server closes it,-# and the next request on that connection never gets a response. Close every inherited descriptor except stdin,-# stdout and stderr. The script needs bash because sh cannot close a descriptor above 9.-for fd_path in /proc/$$/fd/*; do- fd=${fd_path##*/}- if [ "$fd" -gt 2 ]; then- exec {fd}>&-- fi-done--DIR_NAME=$(dirname "$0")--# Create a temporary directory in /tmp-TEMP_DIR=$(mktemp -d -p /tmp "aggregate_funnel_XXXXXXXXXX")--# Trap to clean up the temporary directory on exit-trap 'rm -rf "$TEMP_DIR"; exit' 0--# Determine which executable to use based on architecture-case $( uname -m ) in-aarch64)- EXECUTABLE_NAME="aggregate_funnel_aarch64"- ;;-*)- EXECUTABLE_NAME="aggregate_funnel_x86_64"- ;;-esac--# Copy the executable to a temporary location-cp "$DIR_NAME/$EXECUTABLE_NAME" "$TEMP_DIR/$EXECUTABLE_NAME"-chmod +x "$TEMP_DIR/$EXECUTABLE_NAME"-"$TEMP_DIR/$EXECUTABLE_NAME" "$@"-+. "$(dirname "$0")/run_udf.sh"diff --git a/posthog/user_scripts/v12/aggregate_funnel_array_trends_test.py b/posthog/user_scripts/v12/aggregate_funnel_array_trends_test.pyindex ae5d7f24387b..97da07a2e72d 100755--- a/posthog/user_scripts/v12/aggregate_funnel_array_trends_test.py+++ b/posthog/user_scripts/v12/aggregate_funnel_array_trends_test.py@@ -1,9 +1,13 @@#!/usr/bin/python3+import osimport sysimport jsonif __name__ == "__main__":+ # ClickHouse starts this script with copies of its open client connections. A retained copy keeps a connection+ # open after the server closes it, so the client's next request on that connection never gets a response.+ os.closerange(3, os.sysconf("SC_OPEN_MAX"))for line in sys.stdin:try:print(json.dumps({"result": line})) # noqa: T201diff --git a/posthog/user_scripts/v12/aggregate_funnel_test.py b/posthog/user_scripts/v12/aggregate_funnel_test.pyindex d524cc8e83e9..1473a11dfd6f 100755--- a/posthog/user_scripts/v12/aggregate_funnel_test.py+++ b/posthog/user_scripts/v12/aggregate_funnel_test.py@@ -1,9 +1,13 @@#!/usr/bin/python3+import osimport sysimport jsonimport tracebackif __name__ == "__main__":+ # ClickHouse starts this script with copies of its open client connections. A retained copy keeps a connection+ # open after the server closes it, so the client's next request on that connection never gets a response.+ os.closerange(3, os.sysconf("SC_OPEN_MAX"))for line in sys.stdin:try:print(json.dumps({"result": line})) # noqa: T201diff --git a/posthog/user_scripts/v12/json_drop_keys_udf b/posthog/user_scripts/v12/json_drop_keys_udfindex cc9c5ffdadf9..b7e180ec43c7 100755--- a/posthog/user_scripts/v12/json_drop_keys_udf+++ b/posthog/user_scripts/v12/json_drop_keys_udf@@ -1,35 +1,2 @@#!/bin/bash--# ClickHouse starts this script with copies of its open files, including client connections. A connection stays open-# while any copy of it is open, so a long-running UDF process keeps a connection open after the server closes it,-# and the next request on that connection never gets a response. Close every inherited descriptor except stdin,-# stdout and stderr. The script needs bash because sh cannot close a descriptor above 9.-for fd_path in /proc/$$/fd/*; do- fd=${fd_path##*/}- if [ "$fd" -gt 2 ]; then- exec {fd}>&-- fi-done--DIR_NAME=$(dirname "$0")--# Create a temporary directory in /tmp-TEMP_DIR=$(mktemp -d -p /tmp "json_drop_keys_udf_XXXXXXXXXX")--# Trap to clean up the temporary directory on exit-trap 'rm -rf "$TEMP_DIR"; exit' 0--# Determine which executable to use based on architecture-case $( uname -m ) in-aarch64)- EXECUTABLE_NAME="json_drop_keys_udf_aarch64"- ;;-*)- EXECUTABLE_NAME="json_drop_keys_udf_x86_64"- ;;-esac--# Copy the executable to a temporary location-cp "$DIR_NAME/$EXECUTABLE_NAME" "$TEMP_DIR/$EXECUTABLE_NAME"-chmod +x "$TEMP_DIR/$EXECUTABLE_NAME"-"$TEMP_DIR/$EXECUTABLE_NAME" "$@"+. "$(dirname "$0")/run_udf.sh"diff --git a/posthog/user_scripts/v12/run_udf.sh b/posthog/user_scripts/v12/run_udf.shnew file mode 100644index 000000000000..b4c59bc14cda--- /dev/null+++ b/posthog/user_scripts/v12/run_udf.sh@@ -0,0 +1,36 @@+# shellcheck shell=bash+# Sourced by every wrapper in this directory. The wrapper's file name is the base name of the binary it starts, so+# every wrapper is the same two lines. Each version directory carries its own copy of this file, because a deployed+# version must not depend on files outside it.++# ClickHouse starts a wrapper with copies of its open files, including client connections. A connection stays open+# while any copy of it is open, so a long-running UDF process keeps a connection open after the server closes it,+# and the next request on that connection never gets a response. Close every inherited descriptor except stdin,+# stdout and stderr before anything else. The wrappers run under bash because sh cannot close a descriptor above 9.+for fd_path in /proc/$$/fd/*; do+ fd=${fd_path##*/}+ if (( fd > 2 )); then+ exec {fd}>&-+ fi+done++DIR_NAME=$(dirname "$0")+UDF_NAME=$(basename "$0")++TEMP_DIR=$(mktemp -d -p /tmp "${UDF_NAME}_XXXXXXXXXX")+trap 'rm -rf "$TEMP_DIR"; exit' 0++case $(uname -m) in+aarch64)+ EXECUTABLE_NAME="${UDF_NAME}_aarch64"+ ;;+*)+ EXECUTABLE_NAME="${UDF_NAME}_x86_64"+ ;;+esac++# A deploy replaces the binaries in user_scripts while pooled processes still run them. Each process runs a private+# copy, so the replacement never touches a file a process is executing.+cp "$DIR_NAME/$EXECUTABLE_NAME" "$TEMP_DIR/$EXECUTABLE_NAME"+chmod +x "$TEMP_DIR/$EXECUTABLE_NAME"+"$TEMP_DIR/$EXECUTABLE_NAME" "$@"diff --git a/tools/snob_backend_test_selection_shadow.py b/tools/snob_backend_test_selection_shadow.pyindex b2f12f4c9182..536218c893aa 100755--- a/tools/snob_backend_test_selection_shadow.py+++ b/tools/snob_backend_test_selection_shadow.py@@ -37,6 +37,7 @@ run on the PR. Recall beats precision: when in doubt, add a FULL_RUN_PATTERNS enfrom __future__ import annotationsimport os+import reimport astimport sysimport json@@ -418,8 +419,18 @@ def _candidate_conventional_tests(path: str) -> list[str]:return [str(candidate) for candidate in candidates if (REPO_ROOT / candidate).is_file()]-def _existing_test_files(paths: list[str] | set[str]) -> list[str]:- return sorted(path for path in paths if (REPO_ROOT / path).is_file())+def _pytest_ignored_prefixes() -> tuple[str, ...]:+ """The --ignore paths in pytest.ini. pytest skips them when it walks a directory, but it still collects a file+ that it gets by name, so a selected file under one of them fails collection."""+ config = REPO_ROOT / "pytest.ini"+ if not config.is_file():+ return ()+ return tuple(f"{path.rstrip('/')}/" for path in re.findall(r"--ignore[= ](\S+)", config.read_text()))+++def _runnable_test_files(paths: list[str] | set[str]) -> list[str]:+ ignored = _pytest_ignored_prefixes()+ return sorted(path for path in paths if (REPO_ROOT / path).is_file() and not path.startswith(ignored))def _add_group(groups: dict[str, set[str]], name: str, tests: list[str] | set[str]) -> None:@@ -434,7 +445,7 @@ def ast_select_tests(changed_files: list[str], features_by_path: dict[str, TestFall_test_files = set(features_by_path.keys())# ── 1. Changed test files themselves ─────────────────────────────- changed_tests = _existing_test_files([path for path in changed_files if _is_test_file(path)])+ changed_tests = _runnable_test_files([path for path in changed_files if _is_test_file(path)])_add_group(groups, "changed_tests", changed_tests)# ── 2. Conventional test neighbors (test_<name>.py next to <name>.py) ─@@ -584,7 +595,7 @@ def snob_select_tests(changed_files: list[str]) -> dict[str, Any]:return {"status": "error", "error": f"could not import snob_lib: {exc}", "tests": [], "count": 0}try:- tests = _existing_test_files({normalize_repo_path(str(test)) for test in snob_lib.get_tests(changed_py_files)})+ tests = _runnable_test_files({normalize_repo_path(str(test)) for test in snob_lib.get_tests(changed_py_files)})except Exception as exc:return {"status": "error", "error": f"snob_lib.get_tests failed: {exc}", "tests": [], "count": 0}@@ -691,7 +702,7 @@ def build_result(base_ref: str) -> dict[str, Any]:snob_selection = snob_select_tests(changed_files)snob_tests = [str(test) for test in snob_selection.get("tests", [])]- combined_tests = _existing_test_files(set(snob_tests) | set(ast_selection.tests))+ combined_tests = _runnable_test_files(set(snob_tests) | set(ast_selection.tests))durations = load_durations()selected_seconds = estimate_duration(combined_tests, durations)diff --git a/tools/test_snob_backend_test_selection_shadow.py b/tools/test_snob_backend_test_selection_shadow.pyindex 12ef54c3f0b1..fc17e5ef87ab 100644--- a/tools/test_snob_backend_test_selection_shadow.py+++ b/tools/test_snob_backend_test_selection_shadow.py@@ -133,6 +133,12 @@ class TestSnobBackendTestSelectionShadow(unittest.TestCase):selected_test.parent.mkdir(parents=True)selected_test.write_text("def test_feature_flags():\n pass\n")deleted_test = selection.REPO_ROOT / "posthog" / "api" / "test" / "test_deleted.py"+ (selection.REPO_ROOT / "pytest.ini").write_text(+ "[pytest]\naddopts = --reuse-db --ignore=posthog/user_scripts\n"+ )+ pytest_ignored_file = selection.REPO_ROOT / "posthog" / "user_scripts" / "aggregate_funnel_test.py"+ pytest_ignored_file.parent.mkdir(parents=True)+ pytest_ignored_file.write_text("")seen_changed_files: list[list[str]] = []@@ -140,7 +146,7 @@ class TestSnobBackendTestSelectionShadow(unittest.TestCase):def get_tests(changed_files: list[str]) -> set[str]:seen_changed_files.append(changed_files)- return {str(selected_test), str(deleted_test)}+ return {str(selected_test), str(deleted_test), str(pytest_ignored_file)}fake_snob.get_tests = get_tests # type: ignore[attr-defined]previous_snob = sys.modules.get("snob_lib")