Skip to content

LLM-Driven Extraction of Unstructured Data — Built for API Deployments & ETL Pipeline Workflows

REVERSE MERGE - v0.138.15-hotfix (#1625)

alif3e6b4c58be9
diff --git a/docker/dockerfiles/worker-unified.Dockerfile b/docker/dockerfiles/worker-unified.Dockerfile
index 5853000c0..6f853a4f5 100644
--- a/docker/dockerfiles/worker-unified.Dockerfile
+++ b/docker/dockerfiles/worker-unified.Dockerfile
@@ -21,6 +21,7 @@ ENV PYTHONDONTWRITEBYTECODE=1 \
# Install system dependencies (minimal for workers)
RUN apt-get update \
&& apt-get --no-install-recommends install -y \
+ build-essential \
curl \
gcc \
libmagic-dev \
diff --git a/unstract/connectors/src/unstract/connectors/databases/bigquery/bigquery.py b/unstract/connectors/src/unstract/connectors/databases/bigquery/bigquery.py
index 983c36554..35ca323fa 100644
--- a/unstract/connectors/src/unstract/connectors/databases/bigquery/bigquery.py
+++ b/unstract/connectors/src/unstract/connectors/databases/bigquery/bigquery.py
@@ -6,8 +6,9 @@ import uuid
from enum import Enum
from typing import Any
-import google.api_core.exceptions
-
+# DO NOT import Google API libraries at module level!
+# These imports initialize gRPC state before Celery fork, causing SIGSEGV.
+# Import them INSIDE methods where they're used (after fork).
from unstract.connectors.constants import DatabaseTypeConstants
from unstract.connectors.databases.exceptions import (
BigQueryForbiddenException,
@@ -229,6 +230,9 @@ class BigQuery(UnstractDB):
BigQueryNotFoundException: raised due to unavailable resource
ColumnMissingException: raised due to missing columns in table query
"""
+ # Import INSIDE method to avoid module-level gRPC initialization
+ import google.api_core.exceptions
+
table_name = kwargs.get("table_name", None)
if table_name is None:
raise ValueError("Please enter a valid table_name to to create/insert table")
diff --git a/unstract/connectors/src/unstract/connectors/filesystems/google_cloud_storage/google_cloud_storage.py b/unstract/connectors/src/unstract/connectors/filesystems/google_cloud_storage/google_cloud_storage.py
index 03b3beb49..5a087b196 100644
--- a/unstract/connectors/src/unstract/connectors/filesystems/google_cloud_storage/google_cloud_storage.py
+++ b/unstract/connectors/src/unstract/connectors/filesystems/google_cloud_storage/google_cloud_storage.py
@@ -2,6 +2,7 @@ import base64
import json
import logging
import os
+import threading
from datetime import UTC, datetime
from typing import Any
@@ -12,8 +13,24 @@ from unstract.connectors.filesystems.unstract_file_system import UnstractFileSys
logger = logging.getLogger(__name__)
+# Global lock for thread-safe Google Cloud Storage initialization
+# Prevents deadlock when multiple threads simultaneously create GCS clients
+# (GCSFileSystem uses gRPC which has internal locks)
+_GCS_INIT_LOCK = threading.Lock()
+
class GoogleCloudStorageFS(UnstractFileSystem):
+ """Fork-safe and thread-safe Google Cloud Storage filesystem connector.
+
+ IMPORTANT: This class ensures safety across ALL Celery pool types:
+ - Thread pool: Global lock prevents gRPC deadlock
+ - Prefork pool: Lazy initialization prevents SIGSEGV
+ - Gevent pool: threading.Lock is gevent-compatible
+
+ GCS filesystem client is NOT created in __init__ to avoid creating
+ gRPC connections before fork, which would cause SIGSEGV in child processes.
+ """
+
def __init__(self, settings: dict[str, Any]):
"""Initializing gcs
@@ -23,26 +40,16 @@ class GoogleCloudStorageFS(UnstractFileSystem):
ConnectorError: Error raised when connection initialization fails
"""
super().__init__("GoogleCloudStorage")
- project_id = settings.get("project_id", "")
- json_credentials_str = settings.get("json_credentials", "{}")
- try:
- from gcsfs import GCSFileSystem
-
- json_credentials = json.loads(json_credentials_str)
- self.gcs_fs = GCSFileSystem(
- token=json_credentials,
- project=project_id,
- cache_timeout=0,
- use_listings_cache=False,
- )
- except json.JSONDecodeError as e:
- logger.error(f"Failed to parse JSON credentials: {str(e)}")
- error_msg = (
- "Failed to connect to Google Cloud Storage. \n"
- "GCS credentials are not in proper JSON format. \n"
- f"Error: \n```\n{str(e)}\n```"
- )
- raise ConnectorError(error_msg) from e
+
+ # Store connection settings WITHOUT creating GCSFileSystem
+ # GCSFileSystem uses gRPC which is NOT fork-safe!
+ # The filesystem will be created lazily in get_fsspec_fs() AFTER fork
+ self._project_id = settings.get("project_id", "")
+ self._json_credentials_str = settings.get("json_credentials", "{}")
+
+ # Lazy initialization - create GCS client only when needed (after fork)
+ # This prevents SIGSEGV crashes in Celery ForkPoolWorker processes from gRPC calls
+ self._gcs_fs = None
@staticmethod
def get_id() -> str:
@@ -84,7 +91,58 @@ class GoogleCloudStorageFS(UnstractFileSystem):
return True
def get_fsspec_fs(self) -> AbstractFileSystem:
- return self.gcs_fs
+ """Get GCS filesystem with lazy initialization (fork-safe and thread-safe).
+
+ This method creates the GCS filesystem client on first access,
+ ensuring it's created AFTER Celery fork to avoid SIGSEGV crashes.
+
+ GCSFileSystem uses gRPC for Google Cloud API communication. Creating it
+ in __init__ causes it to be created in the parent process before fork(),
+ resulting in stale gRPC connections in child processes that trigger
+ segmentation faults when the child tries to use the filesystem.
+
+ The lazy initialization pattern ensures that gRPC-based Google Cloud
+ clients are created in the child process after fork(), not in the
+ parent process before fork().
+
+ Thread-safe: Uses global lock to prevent gRPC deadlock when multiple
+ threads create GCS clients simultaneously.
+
+ Returns:
+ GCSFileSystem: The initialized Google Cloud Storage filesystem client
+
+ Raises:
+ ConnectorError: If GCS credentials are invalid or connection fails
+ """
+ if self._gcs_fs is None:
+ # Use global lock to serialize GCS client creation
+ # This prevents deadlock when multiple threads hit this simultaneously
+ with _GCS_INIT_LOCK:
+ # Double-check pattern for thread safety
+ if self._gcs_fs is None:
+ logger.info("Initializing GCS client (lazy init after fork)")
+ try:
+ from gcsfs import GCSFileSystem
+
+ # Parse credentials and create GCS filesystem AFTER fork
+ json_credentials = json.loads(self._json_credentials_str)
+ self._gcs_fs = GCSFileSystem(
+ token=json_credentials,
+ project=self._project_id,
+ cache_timeout=0,
+ use_listings_cache=False,
+ )
+ logger.info("GCS client initialized successfully")
+ except json.JSONDecodeError as e:
+ logger.error(f"Failed to parse JSON credentials: {str(e)}")
+ error_msg = (
+ "Failed to connect to Google Cloud Storage. \n"
+ "GCS credentials are not in proper JSON format. \n"
+ f"Error: \n```\n{str(e)}\n```"
+ )
+ raise ConnectorError(error_msg) from e
+
+ return self._gcs_fs
def extract_metadata_file_hash(self, metadata: dict[str, Any]) -> str | None:
"""Extracts a unique file hash from metadata.
diff --git a/unstract/connectors/src/unstract/connectors/filesystems/google_drive/google_drive.py b/unstract/connectors/src/unstract/connectors/filesystems/google_drive/google_drive.py
index a4e63180f..9a6b862b5 100644
--- a/unstract/connectors/src/unstract/connectors/filesystems/google_drive/google_drive.py
+++ b/unstract/connectors/src/unstract/connectors/filesystems/google_drive/google_drive.py
@@ -6,26 +6,41 @@ from datetime import datetime
from pathlib import Path
from typing import Any
-import google.api_core.exceptions as GoogleApiException
-from oauth2client.client import OAuth2Credentials
-from pydrive2.auth import GoogleAuth
-from pydrive2.fs import GDriveFileSystem
-
from unstract.connectors.exceptions import ConnectorError, FSAccessDeniedError
from unstract.connectors.filesystems.google_drive.constants import GDriveConstants
from unstract.connectors.filesystems.unstract_file_system import UnstractFileSystem
from unstract.connectors.gcs_helper import GCSHelper
+# DO NOT import Google API libraries at module level!
+# These imports initialize gRPC state before Celery fork, causing SIGSEGV.
+# Import them INSIDE methods where they're used (after fork).
+# Previously caused crashes:
+# import google.api_core.exceptions
+# from oauth2client.client import OAuth2Credentials
+# from pydrive2.auth import GoogleAuth
+# from pydrive2.fs import GDriveFileSystem
+
logging.getLogger("gdrive").setLevel(logging.ERROR)
logger = logging.getLogger(__name__)
-# Module-level cache for client secrets to avoid repeated Secret Manager calls
-# Secret Manager uses gRPC which is not fork-safe, so we cache the result
-_client_secrets_cache = None
-_client_secrets_lock = threading.Lock()
+# Global lock for thread-safe Google API initialization
+# Prevents deadlock when multiple threads simultaneously create gRPC clients
+# (Secret Manager, Google Drive clients use gRPC which has internal locks)
+_GOOGLE_DRIVE_INIT_LOCK = threading.Lock()
class GoogleDriveFS(UnstractFileSystem):
+ """Fork-safe and thread-safe Google Drive filesystem connector.
+
+ IMPORTANT: This class ensures safety across ALL Celery pool types:
+ - Thread pool: Global lock prevents gRPC deadlock
+ - Prefork pool: Lazy initialization prevents SIGSEGV
+ - Gevent pool: threading.Lock is gevent-compatible
+
+ Credentials and clients are NOT created in __init__ to avoid creating
+ gRPC connections before fork, which would cause SIGSEGV in child processes.
+ """
+
# Settings dict should contain the following:
# {
# "access_token": "<access_token>",
@@ -35,46 +50,22 @@ class GoogleDriveFS(UnstractFileSystem):
def __init__(self, settings: dict[str, Any]):
super().__init__("GoogleDrive")
- # Use module-level cache to avoid repeated Secret Manager calls
- # Secret Manager uses gRPC which is not fork-safe
- global _client_secrets_cache
- if _client_secrets_cache is None:
- with _client_secrets_lock:
- # Double-check pattern
- if _client_secrets_cache is None:
- logger.info(
- "Loading Google Drive client secrets from Secret Manager "
- "(one-time per process)"
- )
- try:
- _client_secrets_cache = json.loads(
- GCSHelper().get_secret("google_drive_client_secret")
- )
- except GoogleApiException.PermissionDenied as e:
- user_message = (
- "Permission denied. Please check your credentials. "
- )
- raise FSAccessDeniedError(
- user_message,
- treat_as_user_message=True,
- ) from e
-
- self.client_secrets = _client_secrets_cache
- self.oauth2_credentials = {
- "client_id": self.client_secrets["web"]["client_id"],
- "client_secret": self.client_secrets["web"]["client_secret"],
- "token_uri": self.client_secrets["web"]["token_uri"],
- "user_agent": None,
- "invalid": False,
+ # Store OAuth settings WITHOUT loading secrets from Secret Manager
+ # Secret Manager uses gRPC which is NOT fork-safe!
+ # Secrets will be loaded lazily in get_fsspec_fs() AFTER fork
+ self._oauth_settings = {
"access_token": settings["access_token"],
"refresh_token": settings["refresh_token"],
GDriveConstants.TOKEN_EXPIRY: settings[GDriveConstants.TOKEN_EXPIRY],
}
+
# Store settings file path for lazy initialization
self._settings_file = f"{os.path.dirname(__file__)}/static/settings.yaml"
- # Lazy initialization - create client only when needed (after fork)
- # This prevents SIGSEGV crashes in Celery ForkPoolWorker processes
+ # Lazy initialization - create client secrets and drive client only when needed (after fork)
+ # This prevents SIGSEGV crashes in Celery ForkPoolWorker processes from gRPC calls
+ self._client_secrets = None
+ self._client_secrets_lock = threading.Lock()
self._drive = None
self._drive_lock = threading.Lock()
@@ -117,30 +108,101 @@ class GoogleDriveFS(UnstractFileSystem):
def can_read() -> bool:
return True
- def get_fsspec_fs(self) -> GDriveFileSystem:
- """Get GDrive filesystem with lazy initialization (fork-safe).
+ def _get_client_secrets(self) -> dict[str, Any]:
+ """Get client secrets with lazy initialization (fork-safe).
+
+ This method loads secrets from Google Cloud Secret Manager on first access,
+ ensuring the gRPC call happens AFTER Celery fork to avoid SIGSEGV crashes.
+
+ Secret Manager uses gRPC which is not fork-safe. Loading secrets in __init__
+ causes them to be loaded in the parent process before fork(), resulting in
+ stale gRPC connections in child processes that trigger segmentation faults.
+
+ Returns:
+ dict: Client secrets configuration from Secret Manager
+
+ Raises:
+ FSAccessDeniedError: If permission denied accessing Secret Manager
+ """
+ if self._client_secrets is None:
+ with self._client_secrets_lock:
+ # Double-check pattern for thread safety
+ if self._client_secrets is None:
+ # Import INSIDE method to avoid module-level gRPC initialization
+ import google.api_core.exceptions as GoogleApiException
+
+ logger.info(
+ "Loading Google Drive client secrets from Secret Manager "
+ "(lazy init after fork)"
+ )
+ try:
+ # This gRPC call happens AFTER fork in child process
+ self._client_secrets = json.loads(
+ GCSHelper().get_secret("google_drive_client_secret")
+ )
+ logger.info("Google Drive client secrets loaded successfully")
+ except GoogleApiException.PermissionDenied as e:
+ user_message = (
+ "Permission denied accessing Google Drive secrets. "
+ "Please check your credentials."
+ )
+ raise FSAccessDeniedError(
+ user_message,
+ treat_as_user_message=True,
+ ) from e
+
+ return self._client_secrets
+
+ def get_fsspec_fs(self) -> Any:
+ """Get GDrive filesystem with lazy initialization (fork-safe and thread-safe).
This method creates the Google Drive API client on first access,
ensuring it's created AFTER Celery fork to avoid SIGSEGV crashes.
- The lazy initialization pattern ensures that gRPC-based Google API
- clients are created in the child process after fork(), not in the
- parent process before fork.
+ The lazy initialization pattern ensures that:
+ 1. Secret Manager gRPC calls happen in child process (after fork)
+ 2. Google Drive API client is created in child process (after fork)
+ 3. No stale gRPC connections exist from parent process
+
+ Thread-safe: Uses global lock to prevent gRPC deadlock when multiple
+ threads create Google Drive clients simultaneously.
Returns:
GDriveFileSystem: The initialized Google Drive filesystem client
"""
if self._drive is None:
- with self._drive_lock:
+ # Use global lock to serialize Google Drive client creation
+ # This prevents deadlock when multiple threads hit this simultaneously
+ with _GOOGLE_DRIVE_INIT_LOCK:
# Double-check pattern for thread safety
if self._drive is None:
+ # Import INSIDE method to avoid module-level gRPC initialization
+ from oauth2client.client import OAuth2Credentials
+ from pydrive2.auth import GoogleAuth
+ from pydrive2.fs import GDriveFileSystem
+
logger.info("Initializing Google Drive client (lazy init after fork)")
+
+ # Load client secrets AFTER fork (gRPC call)
+ client_secrets = self._get_client_secrets()
+
+ # Build OAuth2 credentials with secrets loaded after fork
+ oauth2_credentials = {
+ "client_id": client_secrets["web"]["client_id"],
+ "client_secret": client_secrets["web"]["client_secret"],
+ "token_uri": client_secrets["web"]["token_uri"],
+ "user_agent": None,
+ "invalid": False,
+ **self._oauth_settings, # Add access token, refresh token, expiry
+ }
+
+ # Create Google Auth and Drive filesystem
gauth = GoogleAuth(
settings_file=self._settings_file,
- settings={"client_config": self.client_secrets["web"]},
+ settings={"client_config": client_secrets["web"]},
)
gauth.credentials = OAuth2Credentials.from_json(
- json_data=json.dumps(self.oauth2_credentials)
+ json_data=json.dumps(oauth2_credentials)
)
self._drive = GDriveFileSystem(path="root", google_auth=gauth)
logger.info("Google Drive client initialized successfully")
diff --git a/unstract/connectors/src/unstract/connectors/gcs_helper.py b/unstract/connectors/src/unstract/connectors/gcs_helper.py
index c8449cfd2..3ab65dcc6 100644
--- a/unstract/connectors/src/unstract/connectors/gcs_helper.py
+++ b/unstract/connectors/src/unstract/connectors/gcs_helper.py
@@ -2,11 +2,9 @@ import base64
import json
import logging
import os
+import threading
from typing import Any
-from google.oauth2 import service_account
-from google.oauth2.credentials import Credentials
-
class GCSHelperEnvNotSetException(Exception):
def __init__(self, message: str):
@@ -15,14 +13,29 @@ class GCSHelperEnvNotSetException(Exception):
logger = logging.getLogger(__name__)
+# Global lock for thread-safe Google API initialization
+# Prevents deadlock when multiple threads simultaneously create gRPC clients
+# (Secret Manager, GCS clients use gRPC which has internal locks)
+_GOOGLE_API_INIT_LOCK = threading.Lock()
+
class GCSHelper:
- def __init__(self) -> None:
- from google.cloud.storage import Client
+ """Fork-safe and thread-safe helper for Google Cloud Storage and Secret Manager.
+
+ IMPORTANT: This class ensures safety across ALL Celery pool types:
+ - Thread pool: Global lock prevents gRPC deadlock
+ - Prefork pool: Lazy initialization prevents SIGSEGV
+ - Gevent pool: threading.Lock is gevent-compatible
- self.client = Client
+ Credentials and clients are NOT created in __init__ to avoid creating
+ gRPC connections before fork, which would cause SIGSEGV in child processes.
+ """
+
+ def __init__(self) -> None:
+ # Only store JSON strings and config, NOT credential objects
self.google_service_json = os.environ.get("GDRIVE_GOOGLE_SERVICE_ACCOUNT")
self.google_project_id = os.environ.get("GDRIVE_GOOGLE_PROJECT_ID")
+
if self.google_service_json is None:
raise GCSHelperEnvNotSetException(
"GDRIVE_GOOGLE_SERVICE_ACCOUNT environment variable not set"
@@ -32,28 +45,71 @@ class GCSHelper:
"GDRIVE_GOOGLE_PROJECT_ID environment variable not set"
)
- self.google_credentials = service_account.Credentials.from_service_account_info(
- json.loads(self.google_service_json)
- )
-
- def get_google_credentials(self) -> Credentials:
- return self.google_credentials
+ # Lazy initialization - credentials created only when needed (after fork)
+ self._google_credentials: Any | None = None
+ self._credentials_lock = threading.Lock()
+
+ def _get_credentials(self) -> Any:
+ """Lazily create credentials object (fork-safe).
+
+ This method is called after fork, ensuring gRPC state is created
+ in the child process, not inherited from parent.
+ """
+ if self._google_credentials is None:
+ with self._credentials_lock:
+ if self._google_credentials is None:
+ # Import inside method to avoid module-level initialization
+ from google.oauth2 import service_account
+
+ logger.debug("Creating Google credentials (lazy init after fork)")
+ self._google_credentials = (
+ service_account.Credentials.from_service_account_info(
+ json.loads(self.google_service_json)
+ )
+ )
+ return self._google_credentials
+
+ def get_google_credentials(self) -> Any:
+ """Get Google credentials, creating them lazily if needed."""
+ return self._get_credentials()
def get_secret(self, secret_name: str) -> str:
- from google.cloud import secretmanager
+ """Get secret from Google Secret Manager (lazy init after fork).
+
+ Thread-safe: Uses global lock to prevent gRPC deadlock when multiple
+ threads create Secret Manager clients simultaneously.
+ """
+ # Use global lock to serialize Secret Manager client creation ONLY
+ # This prevents deadlock when multiple threads hit this simultaneously
+ with _GOOGLE_API_INIT_LOCK:
+ from google.cloud import secretmanager
+
+ credentials = self._get_credentials()
+ google_secrets_client = secretmanager.SecretManagerServiceClient(
+ credentials=credentials
+ )
- google_secrets_client = secretmanager.SecretManagerServiceClient(
- credentials=self.google_credentials
- )
+ # Network I/O happens OUTSIDE the lock for better concurrency
s = google_secrets_client.access_secret_version(
request={
- "name": f"projects/{self.google_project_id}/secrets/{secret_name}/versions/latest" # noqa: E501
- }
+ "name": f"projects/{self.google_project_id}/secrets/{secret_name}/versions/latest"
+ },
+ timeout=30.0, # Add reasonable timeout to prevent indefinite hangs
)
return s.payload.data.decode("UTF-8")
+ def _get_storage_client(self) -> Any:
+ """Lazily create GCS client (fork-safe and thread-safe)."""
+ # Use global lock to serialize GCS client creation
+ with _GOOGLE_API_INIT_LOCK:
+ from google.cloud.storage import Client
+
+ credentials = self._get_credentials()
+ return Client(credentials=credentials)
+
def get_object_checksum(self, bucket_name: str, object_name: str) -> str:
- client = self.client(credentials=self.google_credentials)
+ """Get MD5 checksum of GCS object."""
+ client = self._get_storage_client()
bucket = client.bucket(bucket_name)
md5_hash_hex = ""
try:
@@ -65,33 +121,35 @@ class GCSHelper:
return md5_hash_hex
def upload_file(self, bucket_name: str, object_name: str, file_path: str) -> None:
- client = self.client(credentials=self.google_credentials)
+ """Upload file to GCS."""
+ client = self._get_storage_client()
bucket = client.bucket(bucket_name)
blob = bucket.blob(object_name)
blob.upload_from_filename(file_path)
def upload_text(self, bucket_name: str, object_name: str, text: str) -> None:
- client = self.client(credentials=self.google_credentials)
+ """Upload text to GCS."""
+ client = self._get_storage_client()
bucket = client.bucket(bucket_name)
blob = bucket.blob(object_name)
blob.upload_from_string(text)
def upload_object(self, bucket_name: str, object_name: str, object: Any) -> None:
- client = self.client(credentials=self.google_credentials)
+ """Upload object to GCS."""
+ client = self._get_storage_client()
bucket = client.bucket(bucket_name)
blob = bucket.blob(object_name)
blob.upload_from_string(object, content_type="application/octet-stream")
def read_file(self, bucket_name: str, object_name: str) -> Any:
+ """Read file from GCS."""
logger.info(f"Reading file {object_name} from bucket {bucket_name}")
- client = self.client(credentials=self.google_credentials)
+ client = self._get_storage_client()
bucket = client.bucket(bucket_name)
- logger.info(f"Reading file {object_name} from bucket {bucket_name}")
try:
blob = bucket.get_blob(object_name)
- logger.info(f"Reading file {object_name} from bucket {bucket_name}")
obj = blob.download_as_bytes()
- logger.info(f"Reading file {object_name} from bucket {bucket_name}")
+ logger.info(f"Successfully read file {object_name} from bucket {bucket_name}")
return obj
except Exception:
logger.error(f"Could not get blob {object_name} from bucket {bucket_name}")
diff --git a/workers/client_plugin_registry.py b/workers/client_plugin_registry.py
index 41bc4d939..310c66e51 100644
--- a/workers/client_plugin_registry.py
+++ b/workers/client_plugin_registry.py
@@ -134,18 +134,35 @@ class ClientPluginRegistry:
try:
# For workers, use importlib with proper path handling
+ import glob
import importlib
import os
# Convert plugin path to file path
module_path, class_name = plugin_path.rsplit(".", 1)
- relative_path = module_path.replace(".", os.sep) + ".py"
+ relative_path_base = module_path.replace(".", os.sep)
# Get absolute path from workers directory
workers_dir = os.path.dirname(__file__)
- plugin_file_path = os.path.join(workers_dir, relative_path)
- if os.path.exists(plugin_file_path):
+ # Search for both .py and .so files (supports Nuitka-compiled modules)
+ # Try .py first, then .so files
+ plugin_file_path = None
+
+ # Try .py file first
+ py_path = os.path.join(workers_dir, relative_path_base + ".py")
+ if os.path.exists(py_path):
+ plugin_file_path = py_path
+ else:
+ # Try .so files (Nuitka compiled modules: .cpython-312-*.so)
+ so_pattern = os.path.join(workers_dir, relative_path_base + ".*.so")
+ so_files = glob.glob(so_pattern)
+ if so_files:
+ # Use the first .so file found
+ plugin_file_path = so_files[0]
+ logger.debug(f"Found compiled module: {plugin_file_path}")
+
+ if plugin_file_path:
# Use importlib.util for file-based import
import importlib.util
@@ -170,7 +187,10 @@ class ClientPluginRegistry:
f"Successfully loaded plugin class {class_name} from {plugin_file_path}"
)
else:
- logger.debug(f"Plugin file not found: {plugin_file_path}")
+ logger.debug(
+ f"Plugin file not found for {module_path} "
+ f"(searched for .py and .so files)"
+ )
except Exception as e:
logger.debug(f"Failed to load plugin {plugin_name} using worker loader: {e}")
diff --git a/workers/run-worker.sh b/workers/run-worker.sh
index ed3f44527..3e0ebff18 100755
--- a/workers/run-worker.sh
+++ b/workers/run-worker.sh
@@ -86,6 +86,7 @@ OPTIONS:
-c, --concurrency N Set worker concurrency (default: auto)
-q, --queues QUEUES Override default queues (comma-separated)
-p, --health-port N Override health check port
+ -P, --pool TYPE Set Celery pool type (threads, prefork, gevent, solo, eventlet)
-n, --hostname NAME Set custom worker hostname/name
-k, --kill Kill running workers and exit
-s, --status Show status of running workers
@@ -111,6 +112,10 @@ EXAMPLES:
$0 -n api-01 api
$0 -n api-02 api
+ # Run with specific pool type
+ $0 -P prefork file
+ $0 --pool threads general
+
# Check worker status
$0 -s
@@ -261,7 +266,8 @@ run_worker() {
local concurrency=$4
local custom_queues=$5
local health_port=$6
- local custom_hostname=$7
+ local pool_type=$7
+ local custom_hostname=$8
local worker_dir="$WORKERS_DIR/$worker_type"
@@ -321,6 +327,11 @@ run_worker() {
"--hostname=${worker_instance_name}@%h"
)
+ # Add pool type if specified
+ if [[ -n "$pool_type" ]]; then
+ cmd_args+=("--pool=$pool_type")
+ fi
+
# Add concurrency if specified
if [[ -n "$concurrency" ]]; then
cmd_args+=("--concurrency=$concurrency")
@@ -379,6 +390,7 @@ run_all_workers() {
local detach=$1
local log_level=$2
local concurrency=$3
+ local pool_type=$4
print_status $GREEN "Starting all workers..."
@@ -388,7 +400,7 @@ run_all_workers() {
# Run each worker in background
(
- run_worker "$worker" "true" "$log_level" "$concurrency" "" ""
+ run_worker "$worker" "true" "$log_level" "$concurrency" "" "" "$pool_type" ""
) &
sleep 2 # Give each worker time to start
@@ -414,6 +426,7 @@ LOG_LEVEL=""
CONCURRENCY=""
CUSTOM_QUEUES=""
HEALTH_PORT=""
+POOL_TYPE=""
CUSTOM_HOSTNAME=""
KILL_WORKERS=false
SHOW_STATUS=false
@@ -444,6 +457,10 @@ while [[ $# -gt 0 ]]; do
HEALTH_PORT="$2"
shift 2
;;
+ -P|--pool)
+ POOL_TYPE="$2"
+ shift 2
+ ;;
-n|--hostname)
CUSTOM_HOSTNAME="$2"
shift 2
@@ -507,8 +524,8 @@ export PYTHONPATH="$WORKERS_DIR:${PYTHONPATH:-}"
# Run the requested worker(s)
if [[ "$WORKER_TYPE" == "all" ]]; then
- run_all_workers "$DETACH" "$LOG_LEVEL" "$CONCURRENCY"
+ run_all_workers "$DETACH" "$LOG_LEVEL" "$CONCURRENCY" "$POOL_TYPE"
else
WORKER_DIR_NAME="${WORKERS[$WORKER_TYPE]}"
- run_worker "$WORKER_DIR_NAME" "$DETACH" "$LOG_LEVEL" "$CONCURRENCY" "$CUSTOM_QUEUES" "$HEALTH_PORT" "$CUSTOM_HOSTNAME"
+ run_worker "$WORKER_DIR_NAME" "$DETACH" "$LOG_LEVEL" "$CONCURRENCY" "$CUSTOM_QUEUES" "$HEALTH_PORT" "$POOL_TYPE" "$CUSTOM_HOSTNAME"
fi
diff --git a/workers/sample.env b/workers/sample.env
index 3f281817d..28fdb3ec0 100644
--- a/workers/sample.env
+++ b/workers/sample.env
@@ -90,7 +90,15 @@ DATABASE_URL=postgresql://unstract_dev:unstract_pass@unstract-db:5432/unstract_d
# =============================================================================
# Worker Singleton Infrastructure - Controls shared resource management
-ENABLE_API_CLIENT_SINGLETON=true
+# Setting to "false" prevents SIGSEGV crashes in prefork workers by ensuring
+# each API client creates its own HTTP session after fork.
+#
+# Only set to "true" if:
+# - Using thread pool (not prefork)
+# - Backend and workers are tightly coupled (same process)
+# - Performance optimization is critical AND fork-safety is not needed
+#
+ENABLE_API_CLIENT_SINGLETON=false
DEBUG_API_CLIENT_INIT=false
WORKER_INFRASTRUCTURE_HEALTH_CHECK=true
diff --git a/workers/shared/api/internal_client.py b/workers/shared/api/internal_client.py
index c3f6448f1..971ffb845 100644
--- a/workers/shared/api/internal_client.py
+++ b/workers/shared/api/internal_client.py
@@ -174,10 +174,11 @@ class InternalAPIClient(CachedAPIClientMixin):
"""Initialize clients the traditional way (for backward compatibility)."""
if self.config.debug_api_client_init:
logger.info(
- "Using traditional API client initialization (6 separate instances)"
+ "Using traditional API client initialization (8 separate instances)"
)
self.base_client = BaseAPIClient(self.config)
self.execution_client = ExecutionAPIClient(self.config)
+ self.workflow_client = WorkflowAPIClient()
self.file_client = FileAPIClient(self.config)
self.webhook_client = WebhookAPIClient(self.config)
self.organization_client = OrganizationAPIClient(self.config)
diff --git a/workers/shared/infrastructure/config/worker_config.py b/workers/shared/infrastructure/config/worker_config.py
index 03b5d4400..eb1c0b8e7 100644
--- a/workers/shared/infrastructure/config/worker_config.py
+++ b/workers/shared/infrastructure/config/worker_config.py
@@ -242,8 +242,12 @@ class WorkerConfig:
)
# API Client Performance Optimization
+ # CRITICAL: Session sharing is DISABLED by default for fork-safety
+ # The session sharing optimization bypasses BaseAPIClient's fork-safe session property
+ # by directly assigning shared_session to client.session, which breaks fork detection.
+ # Only enable if you understand the fork-safety implications and are not using prefork pool.
enable_api_client_singleton: bool = field(
- default_factory=lambda: os.getenv("ENABLE_API_CLIENT_SINGLETON", "true").lower()
+ default_factory=lambda: os.getenv("ENABLE_API_CLIENT_SINGLETON", "false").lower()
== "true"
)
enable_organization_context_cache: bool = field(
diff --git a/workers/worker.py b/workers/worker.py
index 9e3faff7f..482d1d36e 100755
--- a/workers/worker.py
+++ b/workers/worker.py
@@ -8,14 +8,25 @@ It uses WorkerBuilder to ensure proper configuration including chord retry setti
import logging
import os
import sys
+import threading
+import time
+
+# CRITICAL: Enable gRPC fork support BEFORE any Google API imports
+# This must be set before importing any gRPC-based libraries (Google Cloud, etc.)
+# to prevent SIGSEGV crashes in forked worker processes
+os.environ.setdefault("GRPC_ENABLE_FORK_SUPPORT", "1")
+os.environ.setdefault("GRPC_POLL_STRATEGY", "poll")
+
+from celery import bootsteps, signals # noqa: E402
# Add the workers directory to Python path
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
# Import the WorkerBuilder and WorkerType
-from shared.enums.worker_enums import WorkerType
-from shared.infrastructure import initialize_worker_infrastructure
-from shared.infrastructure.config.builder import WorkerBuilder
+from shared.enums.worker_enums import WorkerType # noqa: E402
+from shared.infrastructure import initialize_worker_infrastructure # noqa: E402
+from shared.infrastructure.config.builder import WorkerBuilder # noqa: E402
+from shared.models.worker_models import get_celery_setting # noqa: E402
# Determine worker type from environment FIRST
WORKER_TYPE = os.environ.get("WORKER_TYPE", "general")
@@ -45,6 +56,276 @@ WorkerBuilder.setup_logging(worker_type)
# Now get logger after setup is complete
logger = logging.getLogger(__name__)
+
+# ============= ADD HEARTBEATKEEPER CLASS HERE =============
+class HeartbeatKeeper(bootsteps.StartStopStep):
+ """Maintains RabbitMQ heartbeat ONLY during warm shutdown to prevent task duplication
+
+ During normal operation: Remains dormant - Celery's built-in heartbeat thread handles everything
+ During warm shutdown: Takes over heartbeat duties when Celery's thread stops
+
+ This fixes the issue where RabbitMQ requeues tasks during K8s pod termination
+ because Celery stops sending heartbeats during warm shutdown.
+ """
+
+ requires = {"celery.worker.components:Pool"}
+
+ def __init__(self, worker, worker_type, **kwargs):
+ self.worker = worker
+ self.worker_type = worker_type
+ self.heartbeat_thread = None
+ self.should_stop = threading.Event()
+ self._shutdown_in_progress = False
+
+ def start(self, worker):
+ """Start heartbeat keeper thread - remains dormant until warm shutdown"""
+ # Load all configuration once and store as instance variables
+ # Read broker_heartbeat from actual Celery configuration (single source of truth)
+ # Default to 10 seconds if None (Celery's default is None)
+ self.broker_heartbeat = self.worker.app.conf.broker_heartbeat or 10
+
+ # Get HeartbeatKeeper-specific configuration using hierarchical config system
+ self.shutdown_interval = get_celery_setting(
+ "HEARTBEAT_KEEPER_SHUTDOWN_INTERVAL",
+ self.worker_type,
+ max(1, self.broker_heartbeat // 5),
+ int,
+ )
+ self.normal_check_interval = get_celery_setting(
+ "HEARTBEAT_KEEPER_CHECK_INTERVAL",
+ self.worker_type,
+ self.broker_heartbeat,
+ int,
+ )
+ self.max_shutdown_errors = get_celery_setting(
+ "HEARTBEAT_KEEPER_MAX_ERRORS", self.worker_type, 10, int
+ )
+
+ logger.info(
+ "HeartbeatKeeper: Starting in standby mode (prevents task duplication during shutdown)"
+ )
+ logger.info(
+ "HeartbeatKeeper: Will remain dormant during normal operation - Celery handles heartbeats"
+ )
+ logger.info(
+ f"HeartbeatKeeper: Will activate during warm shutdown (interval: {self.shutdown_interval}s, broker timeout: {self.broker_heartbeat}s)"
+ )
+
+ self.heartbeat_thread = threading.Thread(
+ target=self._heartbeat_loop,
+ name="HeartbeatKeeper",
+ daemon=True, # Daemon thread won't block shutdown
+ )
+ self.heartbeat_thread.start()
+
+ def stop(self, worker):
+ """Called during warm shutdown - keep heartbeat alive"""
+ self._shutdown_in_progress = True
+ logger.warning(
+ "HeartbeatKeeper: Warm shutdown detected - maintaining heartbeat for task completion"
+ )
+ # Let heartbeat continue during shutdown
+ # K8s terminationGracePeriodSeconds handles the timeout
+
+ def terminate(self, worker):
+ """Called during final termination"""
+ logger.info("HeartbeatKeeper: Final termination - stopping heartbeat thread")
+ self.should_stop.set()
+ if self.heartbeat_thread and self.heartbeat_thread.is_alive():
+ self.heartbeat_thread.join(timeout=1) # Short timeout since daemon thread
+
+ def _is_connection_ready(self) -> tuple[bool, any]:
+ """Check if worker connection is available and ready for heartbeat.
+
+ Returns:
+ tuple: (is_ready, consumer_or_none)
+ """
+ if not hasattr(self.worker, "consumer") or not self.worker.consumer:
+ return (False, None)
+
+ consumer = self.worker.consumer
+ if not consumer.connection or not consumer.connection.connected:
+ return (False, None)
+
+ return (True, consumer)
+
+ def _send_heartbeat_with_error_handling(
+ self, consumer: any, consecutive_errors: int, last_heartbeat: float
+ ) -> tuple[int, float]:
+ """Send heartbeat and handle errors.
+
+ Args:
+ consumer: Celery consumer instance
+ consecutive_errors: Current count of consecutive errors
+ last_heartbeat: Timestamp of last successful heartbeat
+
+ Returns:
+ tuple: (updated_consecutive_errors, updated_last_heartbeat)
+ """
+ current_time = time.time()
+ elapsed = current_time - last_heartbeat
+
+ try:
+ # Use heartbeat_check with rate=0 to force immediate send without validation
+ consumer.connection.heartbeat_check(rate=0)
+ logger.info(
+ f"HeartbeatKeeper: Heartbeat sent during shutdown (elapsed: {elapsed:.1f}s)"
+ )
+ return (0, current_time) # Reset errors on success, update timestamp
+
+ except Exception as hb_error:
+ consecutive_errors += 1
+ # During shutdown, we expect some errors - log but continue
+ if "Too many heartbeats missed" in str(hb_error):
+ logger.warning(
+ f"HeartbeatKeeper: Connection degraded during shutdown "
+ f"(attempt {consecutive_errors}), continuing..."
+ )
+ else:
+ logger.warning(
+ f"HeartbeatKeeper: Heartbeat error during shutdown: {hb_error}"
+ )
+
+ # Don't break the loop during shutdown - keep trying
+ if consecutive_errors > self.max_shutdown_errors:
+ logger.info(
+ "HeartbeatKeeper: Connection appears closed, but will continue attempts"
+ )
+
+ return (consecutive_errors, last_heartbeat) # Keep old timestamp on error
+
+ def _attempt_shutdown_heartbeat(
+ self, consecutive_errors: int, last_heartbeat: float
+ ) -> tuple[int, float]:
+ """Attempt to send heartbeat during shutdown with full error handling.
+
+ Args:
+ consecutive_errors: Current count of consecutive errors
+ last_heartbeat: Timestamp of last successful heartbeat
+
+ Returns:
+ tuple: (updated_consecutive_errors, updated_last_heartbeat)
+ """
+ try:
+ is_ready, consumer = self._is_connection_ready()
+ if not is_ready:
+ return (consecutive_errors, last_heartbeat)
+
+ return self._send_heartbeat_with_error_handling(
+ consumer, consecutive_errors, last_heartbeat
+ )
+
+ except OSError as conn_error:
+ logger.info(
+ f"HeartbeatKeeper: Expected connection issue during shutdown: {conn_error}"
+ )
+ except Exception as e:
+ logger.debug(f"HeartbeatKeeper: Error during shutdown (continuing): {e}")
+
+ return (consecutive_errors, last_heartbeat)
+
+ def _wait_in_standby_mode(self, logged_standby: bool) -> tuple[bool, bool]:
+ """Handle standby mode during normal operation.
+
+ Args:
+ logged_standby: Whether we've already logged standby status
+
+ Returns:
+ tuple: (should_continue_loop, updated_logged_standby)
+ """
+ if not logged_standby:
+ logger.info(
+ "HeartbeatKeeper: In standby mode - Celery's heartbeat thread is active"
+ )
+ logged_standby = True
+
+ # Wait and check periodically if shutdown has started
+ self.should_stop.wait(self.normal_check_interval)
+ return (True, logged_standby)
+
+ def _handle_shutdown_transition(
+ self, logged_standby: bool, last_heartbeat: float
+ ) -> tuple[bool, float]:
+ """Handle transition from standby to active shutdown mode.
+
+ Args:
+ logged_standby: Whether we're currently in logged standby state
+ last_heartbeat: Current last heartbeat timestamp
+
+ Returns:
+ tuple: (updated_logged_standby, updated_last_heartbeat)
+ """
+ if logged_standby:
+ logger.warning(
+ "HeartbeatKeeper: Taking over heartbeat duties from Celery during warm shutdown"
+ )
+ logged_standby = False
+ last_heartbeat = time.time() # Reset timer when we start
+
+ return (logged_standby, last_heartbeat)
+
+ def _heartbeat_loop(self):
+ """Maintain heartbeats ONLY during warm shutdown - dormant during normal operation"""
+ # Initialize state variables
+ consecutive_shutdown_errors = 0
+ last_heartbeat = time.time()
+ logged_standby = False
+
+ # Log the configuration on startup (debug level to avoid noise)
+ logger.debug(
+ f"HeartbeatKeeper config: shutdown_interval={self.shutdown_interval}s, "
+ f"check_interval={self.normal_check_interval}s, "
+ f"broker_heartbeat={self.broker_heartbeat}s, "
+ f"max_errors={self.max_shutdown_errors}"
+ )
+
+ while not self.should_stop.is_set():
+ # During normal operation - just sleep, let Celery handle heartbeats
+ if not self._shutdown_in_progress:
+ should_continue, logged_standby = self._wait_in_standby_mode(
+ logged_standby
+ )
+ if should_continue:
+ continue
+
+ # Handle transition from standby to active shutdown mode
+ logged_standby, last_heartbeat = self._handle_shutdown_transition(
+ logged_standby, last_heartbeat
+ )
+
+ # Active heartbeat sending during shutdown
+ (
+ consecutive_shutdown_errors,
+ last_heartbeat,
+ ) = self._attempt_shutdown_heartbeat(
+ consecutive_shutdown_errors, last_heartbeat
+ )
+
+ # Short wait during shutdown for frequent heartbeats
+ self.should_stop.wait(self.shutdown_interval)
+
+
+# ============= END OF HEARTBEATKEEPER CLASS =============
+
+
+# ============= WORKER-TYPE-AWARE HEARTBEATKEEPER =============
+class WorkerTypeAwareHeartbeatKeeper(HeartbeatKeeper):
+ """HeartbeatKeeper with worker_type from module context.
+
+ This wrapper class captures the worker_type from the module-level variable
+ and passes it to HeartbeatKeeper's __init__. This allows Celery's bootstep
+ system to instantiate it with just (worker, **kwargs) while still providing
+ the worker_type needed for configuration.
+ """
+
+ def __init__(self, worker, **kwargs):
+ # worker_type is captured from module scope via closure
+ super().__init__(worker, worker_type, **kwargs)
+
+
+# ============= END OF WORKER-TYPE-AWARE HEARTBEATKEEPER =============
+
+
logger.info("🚀 Unified Worker Entry Point - Using WorkerBuilder System")
logger.info(f"📋 Worker Type: {WORKER_TYPE}")
logger.info(f"🐳 Running from: {os.getcwd()}")
@@ -55,6 +336,61 @@ logger.info(f"📦 Converted '{WORKER_TYPE}' to {worker_type}")
logger.info(f"🔧 Building Celery app using WorkerBuilder for {worker_type}")
app, config = WorkerBuilder.build_celery_app(worker_type)
+
+# ============= WORKER PROCESS INIT HOOK FOR GRPC FORK-SAFETY =============
[email protected]_process_init.connect
+def on_worker_process_init(**kwargs):
+ """Initialize worker process after fork to fix gRPC fork-safety issues.
+
+ This signal handler runs in each forked worker process AFTER the fork,
+ ensuring that gRPC connections and Google API clients are created fresh
+ in the child process, not inherited from the parent.
+
+ Without this, Google Cloud libraries (Drive, GCS, BigQuery, Secret Manager)
+ crash with SIGSEGV because they inherit stale gRPC connections from the
+ parent process.
+
+ The GRPC_ENABLE_FORK_SUPPORT environment variable set at the top of this
+ file enables gRPC's experimental fork support, and this handler ensures
+ proper reinitialization after fork.
+ """
+ import gc
+
+ logger.info("🔄 Worker process initialized after fork (PID: %s)", os.getpid())
+ logger.info("🔒 gRPC fork support enabled: GRPC_ENABLE_FORK_SUPPORT=1")
+ logger.info(
+ "📡 gRPC poll strategy: %s", os.environ.get("GRPC_POLL_STRATEGY", "default")
+ )
+
+ # Force garbage collection to clean up any stale gRPC state from parent
+ gc.collect()
+
+ logger.info("✅ Worker process ready for gRPC-based operations (Google APIs)")
+
+
+# ============= END OF WORKER PROCESS INIT HOOK =============
+
+
+# ============= REGISTER HEARTBEATKEEPER HERE =============
+# Check if HeartbeatKeeper should be enabled (default: enabled)
+# Uses hierarchical config system: CLI args > worker-specific > global > default
+heartbeat_keeper_enabled = get_celery_setting(
+ "HEARTBEAT_KEEPER_ENABLED", worker_type, True, bool
+)
+
+if heartbeat_keeper_enabled:
+ # Register the HeartbeatKeeper bootstep (class defined at module level)
+ app.steps["worker"].add(WorkerTypeAwareHeartbeatKeeper)
+ logger.info(
+ "✅ HeartbeatKeeper registered (dormant until warm shutdown) to prevent task duplication"
+ )
+else:
+ logger.info(
+ "⚠️ HeartbeatKeeper DISABLED - tasks may be duplicated if worker is terminated during warm shutdown"
+ )
+# ============= END OF REGISTRATION =============
+
+
# Initialize worker infrastructure (singleton API clients, cache managers, etc.)
# This must happen BEFORE task imports so tasks can use shared infrastructure
logger.info("🏗️ Initializing worker infrastructure (singleton pattern)...")