Spaces:
Running
Running
File size: 16,798 Bytes
11952db |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 |
# Copyright (c) Meta Platforms, Inc. and affiliates.
# All rights reserved.
#
# This source code is licensed under the BSD-style license found in the
# LICENSE file in the root directory of this source tree.
"""
Local Julia Executor with Process Pool Support.
This module provides a Julia code executor with:
- Proper process cleanup on timeout (no zombie processes)
- Robust error handling and logging
- Process group management for complete cleanup
- Automatic retry on transient failures
- Optional process pool for 50-100x speedup on repeated executions
Performance Modes:
- Standard mode: Spawn new process for each execution (default for single executions)
- Pool mode: Reuse persistent Julia processes (recommended for repeated executions)
"""
from __future__ import annotations
import logging
import os
import shutil
import signal
import subprocess
import tempfile
import threading
import time
from dataclasses import dataclass
from pathlib import Path
from typing import Optional
# Use julia_env hierarchy to inherit handlers from app.py's setup_logging()
logger = logging.getLogger("julia_env.executor")
@dataclass
class CodeExecResult:
"""Result of code execution."""
stdout: str
stderr: str
exit_code: int
# Try to import process pool (optional dependency)
try:
from .julia_process_pool import JuliaProcessPool
POOL_AVAILABLE = True
except ImportError:
POOL_AVAILABLE = False
JuliaProcessPool = None
class JuliaExecutor:
"""
Executor for running Julia code with robust process management.
This class provides a safe interface to execute Julia code in isolation
and capture the results including stdout, stderr, and exit code.
Features:
- Proper timeout handling without zombie processes
- Process group cleanup for nested processes
- Automatic retry on transient failures
- Comprehensive logging for debugging
- Optional process pool for 50-100x speedup on repeated executions
Example:
>>> executor = JuliaExecutor()
>>> result = executor.run('println("Hello, Julia!")')
>>> print(result.stdout) # "Hello, Julia!\\n"
>>> print(result.exit_code) # 0
>>>
>>> # With process pool (recommended for repeated executions)
>>> JuliaExecutor.enable_process_pool(size=4)
>>> executor = JuliaExecutor(use_process_pool=True)
>>> for i in range(100):
... result = executor.run(f'println({i})') # 50-100x faster!
>>> JuliaExecutor.shutdown_pool() # Clean up when done
"""
# Class-level process pool (shared across all instances if enabled)
_shared_pool: Optional["JuliaProcessPool"] = None
_pool_lock = threading.Lock()
_pool_size: int = 0
_pool_timeout: int = 120
def __init__(
self,
timeout: Optional[int] = None,
max_retries: int = 0,
use_optimization_flags: bool = True,
use_process_pool: bool = True,
):
"""
Initialize the JuliaExecutor.
Args:
timeout: Maximum execution time in seconds. If None, reads from
JULIA_EXECUTION_TIMEOUT env var (default: 120 if not set)
max_retries: Number of retry attempts on transient failures (default: 0)
use_optimization_flags: Enable Julia performance flags (default: True)
use_process_pool: Use process pool if available (default: True)
Raises:
RuntimeError: If Julia executable is not found in PATH
"""
# Read timeout from env var if not explicitly provided
if timeout is None:
timeout = int(os.getenv("JULIA_EXECUTION_TIMEOUT", "120"))
logger.debug(
f"Executor timeout from JULIA_EXECUTION_TIMEOUT env var: {timeout}s"
)
self.timeout = timeout
self.max_retries = max_retries
self.use_optimization_flags = use_optimization_flags
self._use_process_pool = use_process_pool
# Find Julia executable in PATH
self.julia_path = shutil.which("julia")
if not self.julia_path:
# Try common installation paths
common_paths = [
os.path.expanduser("~/.juliaup/bin/julia"),
os.path.expanduser("~/.julia/bin/julia"),
"/usr/local/bin/julia",
"/usr/bin/julia",
]
for path in common_paths:
if os.path.isfile(path) and os.access(path, os.X_OK):
self.julia_path = path
break
if not self.julia_path:
logger.warning(
"Julia executable not found in PATH or common locations. "
"Please install Julia: https://julialang.org/downloads/"
)
# Build optimized Julia command with performance flags
self.base_cmd = [self.julia_path] if self.julia_path else ["julia"]
if self.use_optimization_flags:
self.base_cmd.extend(
[
"--compile=min",
"--optimize=2",
"--startup-file=no",
"--history-file=no",
]
)
logger.debug(f"JuliaExecutor initialized with Julia at: {self.julia_path}")
logger.debug(f"Timeout: {self.timeout}s, Max retries: {self.max_retries}")
def _kill_process_tree(
self, proc: subprocess.Popen, script_file: Optional[str] = None
) -> None:
"""
Terminate a process and all its children.
This prevents zombie processes by ensuring complete cleanup.
Args:
proc: The subprocess.Popen instance to terminate
script_file: Optional script file path (for logging)
"""
if proc.poll() is None: # Process is still running
try:
# Try graceful termination first
logger.warning(f"Terminating process {proc.pid} gracefully...")
proc.terminate()
# Wait up to 2 seconds for graceful termination
try:
proc.wait(timeout=2.0)
logger.debug(f"Process {proc.pid} terminated gracefully")
return
except subprocess.TimeoutExpired:
logger.warning(
f"Process {proc.pid} did not terminate, forcing kill..."
)
# Force kill if still running
proc.kill()
try:
proc.wait(timeout=2.0)
logger.debug(f"Process {proc.pid} killed forcefully")
except subprocess.TimeoutExpired:
pass
except Exception as e:
logger.error(f"Error killing process {proc.pid}: {e}")
# Last resort: try killing via process group
try:
if hasattr(os, "killpg"):
os.killpg(os.getpgid(proc.pid), signal.SIGKILL)
logger.debug(f"Killed process group for {proc.pid}")
except Exception as pg_error:
logger.error(f"Failed to kill process group: {pg_error}")
def run(self, code: str, timeout: Optional[int] = None) -> CodeExecResult:
"""
Execute Julia code and return the result with robust error handling.
This method provides:
- Automatic retry on transient failures
- Proper timeout handling without zombie processes
- Process group cleanup for nested processes
- Comprehensive error logging
- Optional process pool for 50-100x speedup
Args:
code: Julia code string to execute
timeout: Override default timeout (seconds). If None, uses pool's
configured timeout (when using pool) or instance timeout.
Returns:
CodeExecResult containing stdout, stderr, and exit_code
"""
# Use process pool if enabled and available
# Pass timeout as-is (None means use pool's configured default)
if self._use_process_pool and JuliaExecutor._shared_pool is not None:
try:
return JuliaExecutor._shared_pool.execute(code, timeout=timeout)
except Exception as e:
logger.warning(
f"Process pool execution failed: {e}, falling back to subprocess"
)
# Fall through to standard execution
# For subprocess fallback, apply instance default if timeout not specified
if timeout is None:
timeout = self.timeout
# Check if Julia is available
if not self.julia_path:
return CodeExecResult(
stdout="",
stderr="Julia not found in PATH. Please install Julia.",
exit_code=127,
)
code_file = None
for attempt in range(self.max_retries + 1):
proc = None
try:
# Create temporary file for Julia code
with tempfile.NamedTemporaryFile(
mode="w", suffix=".jl", delete=False, encoding="utf-8"
) as f:
f.write(code)
code_file = f.name
script_name = Path(code_file).name
logger.debug(
f"[Attempt {attempt + 1}/{self.max_retries + 1}] Executing: {script_name}"
)
# Start process with Popen for better control
start_time = time.time()
# On Unix systems, use process groups for better cleanup
kwargs = {
"stdout": subprocess.PIPE,
"stderr": subprocess.PIPE,
"text": True,
}
# Create new process group on Unix systems
if hasattr(os, "setpgrp"):
kwargs["preexec_fn"] = os.setpgrp
proc = subprocess.Popen(self.base_cmd + [code_file], **kwargs)
logger.debug(f"Started Julia process {proc.pid}")
# Wait for process with timeout
try:
stdout, stderr = proc.communicate(timeout=timeout)
exit_code = proc.returncode
elapsed = time.time() - start_time
logger.debug(
f"Julia execution completed in {elapsed:.2f}s (exit: {exit_code})"
)
# Clean up temp file
self._cleanup_temp_file(code_file)
return CodeExecResult(
stdout=stdout,
stderr=stderr,
exit_code=exit_code,
)
except subprocess.TimeoutExpired:
logger.error(
f"Julia execution timed out after {timeout}s "
f"(attempt {attempt + 1}/{self.max_retries + 1})"
)
# CRITICAL: Kill the process AND all its children
self._kill_process_tree(proc, code_file)
# If this was our last retry, return timeout error
if attempt >= self.max_retries:
self._cleanup_temp_file(code_file)
return CodeExecResult(
stdout="",
stderr=f"Execution timed out after {timeout}s",
exit_code=124, # Standard timeout exit code
)
# Wait before retry
time.sleep(1.0)
continue
except FileNotFoundError:
logger.error(f"Julia executable not found at {self.julia_path}")
return CodeExecResult(
stdout="",
stderr=f"Julia executable not found: {self.julia_path}",
exit_code=127,
)
except Exception as e:
logger.error(
f"Error executing Julia (attempt {attempt + 1}/{self.max_retries + 1}): {e}"
)
# Try to kill process if it exists
if proc is not None and proc.poll() is None:
self._kill_process_tree(proc, code_file)
# If this was our last retry, return error
if attempt >= self.max_retries:
self._cleanup_temp_file(code_file)
return CodeExecResult(
stdout="",
stderr=f"Error executing Julia code: {str(e)}",
exit_code=1,
)
# Wait before retry
time.sleep(1.0)
continue
finally:
# Always ensure temp file is cleaned up
self._cleanup_temp_file(code_file)
# Should never reach here
return CodeExecResult(
stdout="",
stderr="Unexpected error: all retries exhausted",
exit_code=1,
)
def _cleanup_temp_file(self, code_file: Optional[str]) -> None:
"""Clean up temporary file safely."""
if code_file and Path(code_file).exists():
try:
Path(code_file).unlink()
except Exception as e:
logger.debug(f"Could not delete temp file {code_file}: {e}")
@staticmethod
def enable_process_pool(size: int = 4, timeout: Optional[int] = None) -> bool:
"""
Enable the shared Julia process pool for all JuliaExecutor instances.
This provides 50-100x speedup for repeated code executions by reusing
persistent Julia processes instead of spawning new ones.
Args:
size: Number of worker processes to create (default: 4)
timeout: Default timeout for code execution in seconds.
If None, reads from JULIA_EXECUTION_TIMEOUT env var (default: 120)
Returns:
True if pool was created successfully, False otherwise
"""
if not POOL_AVAILABLE:
logger.warning(
"Process pool not available (julia_process_pool module not found). "
"Falling back to subprocess execution."
)
return False
# Read timeout from env var if not explicitly provided
if timeout is None:
timeout = int(os.getenv("JULIA_EXECUTION_TIMEOUT", "120"))
with JuliaExecutor._pool_lock:
if JuliaExecutor._shared_pool is not None:
logger.debug("Process pool already enabled")
return True
try:
logger.info(f"Enabling Julia process pool with {size} workers")
JuliaExecutor._shared_pool = JuliaProcessPool(
size=size, timeout=timeout
)
JuliaExecutor._pool_size = size
JuliaExecutor._pool_timeout = timeout
logger.info("Julia process pool enabled successfully")
return True
except Exception as e:
logger.error(f"Failed to enable process pool: {e}")
return False
@staticmethod
def shutdown_pool() -> None:
"""
Shutdown the shared Julia process pool.
This should be called when you're done with all Julia executions
to properly clean up worker processes.
"""
with JuliaExecutor._pool_lock:
if JuliaExecutor._shared_pool is not None:
logger.info("Shutting down Julia process pool")
try:
JuliaExecutor._shared_pool.shutdown()
except Exception as e:
logger.error(f"Error shutting down pool: {e}")
finally:
JuliaExecutor._shared_pool = None
@staticmethod
def is_pool_enabled() -> bool:
"""Check if the process pool is currently enabled."""
with JuliaExecutor._pool_lock:
return JuliaExecutor._shared_pool is not None
@staticmethod
def get_pool_metrics() -> dict:
"""Get metrics about the process pool."""
if JuliaExecutor._shared_pool is None:
return {
"enabled": False,
"pool_size": 0,
"available_workers": 0,
}
return {
"enabled": True,
"pool_size": JuliaExecutor._pool_size,
"timeout": JuliaExecutor._pool_timeout,
"available_workers": JuliaExecutor._pool_size,
}
|