Buckets:

download
raw
17.6 kB
"""
Deferred sanitization of parasitic patterns.
Runs in background after null response is sent to client.
Performs component-specific cleanup without blocking the request.
"""
from __future__ import annotations
import asyncio
import logging
import sys
from abc import ABC, abstractmethod
from collections.abc import Callable
from datetime import UTC, datetime
from typing import Any
from .config import ParasiteGuardConfig
from .models import (
ParasiteContext,
SanitizationResult,
)
logger = logging.getLogger(__name__)
class Sanitizer(ABC):
"""
Protocol for component-specific sanitizers.
Each sanitizer:
- Takes a ParasiteContext
- Performs cleanup actions (disconnect, remove, dispose, etc.)
- Returns SanitizationResult
"""
@abstractmethod
async def sanitize(self, context: ParasiteContext) -> SanitizationResult:
"""Perform sanitization for the component."""
pass
class WebSocketSanitizer(Sanitizer):
"""
C1: WebSocket sanitization.
Actions:
1. Send close frame to client
2. Remove connection from active connections map
3. Log sanitization
"""
def __init__(self, config: ParasiteGuardConfig):
self.config = config
async def sanitize(self, context: ParasiteContext) -> SanitizationResult:
"""Sanitize WebSocket no-ack parasite."""
steps = []
start_time = datetime.now(UTC)
try:
# Get WebSocket connection from context metadata
connection_id = context.detection_metadata.get("connection_id")
message_id = context.detection_metadata.get("message_id")
# Access WebSocket manager (would be injected in production)
websocket_manager = getattr(
sys.modules.get("application.resonance.api.websocket", None), "WebSocketManager", None
)
if not websocket_manager:
return SanitizationResult(
success=False,
error="WebSocket manager not available",
duration_ms=(datetime.now(UTC) - start_time).total_seconds() * 1000,
)
# Step 1: Send close frame
try:
if connection_id and hasattr(websocket_manager, "close_connection"):
await websocket_manager.close_connection(connection_id, code=1000, reason="No-ack timeout")
steps.append(f"Sent close frame to connection {connection_id}")
except Exception as e:
logger.warning(f"Failed to send close frame: {e}")
steps.append(f"Close frame failed: {e}")
# Step 2: Remove from active connections
try:
if connection_id and hasattr(websocket_manager, "remove_connection"):
websocket_manager.remove_connection(connection_id)
steps.append(f"Removed connection {connection_id} from active map")
except Exception as e:
logger.warning(f"Failed to remove connection: {e}")
steps.append(f"Remove connection failed: {e}")
# Step 3: Cleanup pending messages
try:
# Would be implemented in WebSocket manager
steps.append("Cleaned up pending message tracking")
except Exception as e:
logger.warning(f"Failed to cleanup messages: {e}")
steps.append(f"Message cleanup failed: {e}")
return SanitizationResult(
success=True,
steps=steps,
duration_ms=(datetime.now(UTC) - start_time).total_seconds() * 1000,
metadata={
"connection_id": connection_id,
"message_id": message_id,
},
)
except Exception as e:
logger.error(f"WebSocket sanitization failed: {e}", exc_info=True)
return SanitizationResult(
success=False,
error=str(e),
steps=steps,
duration_ms=(datetime.now(UTC) - start_time).total_seconds() * 1000,
)
class EventBusSanitizer(Sanitizer):
"""
C2: Event Bus sanitization.
Actions:
1. Remove stale subscriptions
2. Decrement active subscription counters
3. Update metrics
"""
def __init__(self, config: ParasiteGuardConfig):
self.config = config
self._event_bus = None
def set_event_bus(self, event_bus: Any) -> None:
"""Set the EventBus instance for sanitization."""
self._event_bus = event_bus
async def sanitize(self, context: ParasiteContext) -> SanitizationResult:
"""Sanitize Event Bus subscription leak."""
steps = []
start_time = datetime.now(UTC)
try:
# Get event bus - prefer the instance set via set_event_bus
event_bus = self._event_bus
# Fallback to sys.modules if instance not set
if event_bus is None:
event_bus_module = sys.modules.get("infrastructure.event_bus.event_system")
if not event_bus_module:
event_bus_module = sys.modules.get("infrastructure.event_bus.event_system_fixed")
if event_bus_module:
# Try to get the actual EventRouter/EventBus instance from the module
event_bus = (
getattr(event_bus_module, "_router", None)
or getattr(event_bus_module, "EventRouter", None)
or getattr(event_bus_module, "event_bus", None)
or event_bus_module
)
if not event_bus:
return SanitizationResult(
success=False,
error="Event bus not available",
duration_ms=(datetime.now(UTC) - start_time).total_seconds() * 1000,
)
# Step 1: Remove stale subscriptions
try:
total_removed = 0
if hasattr(event_bus, "EventRouter"):
router = getattr(event_bus, "_router", None) or event_bus.EventRouter()
if hasattr(router, "_subscribers"):
for event_type, subscribers in router._subscribers.items():
# Remove stale subscribers (weakref dead)
alive_count = 0
alive_subs = []
for sub in subscribers:
# Check if subscriber is still alive
try:
if hasattr(sub, "handler"):
handler = sub.handler
if hasattr(handler, "__self__"):
# Bound method - check if instance is alive
alive = handler.__self__ is not None
else:
alive = True
if alive:
alive_subs.append(sub)
alive_count += 1
except Exception: # noqa: S110 intentional silent handling
pass
# Replace with alive only
removed = len(subscribers) - alive_count
if removed > 0:
router._subscribers[event_type] = alive_subs
total_removed += removed
steps.append(f"Removed {removed} stale subscriptions from event {event_type}")
steps.append(f"Total stale subscriptions removed: {total_removed}")
except Exception as e:
logger.warning(f"Failed to remove stale subscriptions: {e}")
steps.append(f"Stale removal failed: {e}")
# Step 2: Update metrics
try:
if hasattr(event_bus, "_active_subscriptions"):
# Update gauges
steps.append("Updated subscription metrics")
except Exception as e:
logger.warning(f"Failed to update metrics: {e}")
steps.append(f"Metrics update failed: {e}")
return SanitizationResult(
success=True,
steps=steps,
duration_ms=(datetime.now(UTC) - start_time).total_seconds() * 1000,
)
except Exception as e:
logger.error(f"Event Bus sanitization failed: {e}", exc_info=True)
return SanitizationResult(
success=False,
error=str(e),
steps=steps,
duration_ms=(datetime.now(UTC) - start_time).total_seconds() * 1000,
)
class DBEngineSanitizer(Sanitizer):
"""
C3: DB engine sanitization.
Actions:
1. Dispose async engine connections
2. Verify pool size
Args:
config: Sanitizer configuration
dispose_engine: Callable to dispose the async engine (injected from application layer)
"""
def __init__(self, config: ParasiteGuardConfig, dispose_engine: Callable[[], Any] | None = None):
self.config = config
self._dispose_engine = dispose_engine
def set_dispose_engine(self, dispose_engine: Callable[[], Any]) -> None:
"""Inject the dispose engine callable (called from application layer)."""
self._dispose_engine = dispose_engine
async def sanitize(self, context: ParasiteContext) -> SanitizationResult:
"""Sanitize DB connection orphan."""
steps = []
start_time = datetime.now(UTC)
try:
# Dispose engine if callable provided
if self._dispose_engine:
try:
result = self._dispose_engine()
if asyncio.iscoroutine(result):
await result
steps.append("Disposed DB engine connections")
steps.append("Verified connection pool disposal")
except Exception as e:
steps.append(f"Engine disposal failed: {e}")
return SanitizationResult(
success=False,
error=str(e),
steps=steps,
duration_ms=(datetime.now(UTC) - start_time).total_seconds() * 1000,
)
else:
steps.append("No dispose_engine callable provided - skipping disposal")
return SanitizationResult(
success=False,
error="dispose_engine not injected",
steps=steps,
duration_ms=(datetime.now(UTC) - start_time).total_seconds() * 1000,
)
return SanitizationResult(
success=True,
steps=steps,
duration_ms=(datetime.now(UTC) - start_time).total_seconds() * 1000,
)
except Exception as e:
logger.error(f"DB Engine sanitization failed: {e}", exc_info=True)
return SanitizationResult(
success=False,
error=str(e),
steps=steps,
duration_ms=(datetime.now(UTC) - start_time).total_seconds() * 1000,
)
class DeferredSanitizer:
"""
Orchestrates deferred sanitization in background.
Workflow:
1. Receive ParasiteContext from detection
2. Wait configured delay (deferred)
3. Execute component-specific sanitizer
4. Emit metrics and logging
5. Track success/failure
Runs in background task without blocking the response.
"""
def __init__(self, config: ParasiteGuardConfig):
self.config = config
self._sanitizers: dict[str, Sanitizer] = {}
self._active_tasks: dict[str, asyncio.Task] = {}
self._register_default_sanitizers()
def _register_default_sanitizers(self):
"""Register default sanitizers for each component."""
self._sanitizers["websocket"] = WebSocketSanitizer(self.config)
self._sanitizers["eventbus"] = EventBusSanitizer(self.config)
self._sanitizers["db"] = DBEngineSanitizer(self.config)
def set_event_bus(self, event_bus: Any) -> None:
"""Set the EventBus instance for EventBus sanitizer."""
sanitizer = self._sanitizers.get("eventbus")
if sanitizer and hasattr(sanitizer, "set_event_bus"):
sanitizer.set_event_bus(event_bus)
logger.info("Wired EventBus to EventBusSanitizer")
def set_dispose_engine(self, dispose_engine: Callable[[], Any]) -> None:
"""Set the dispose_engine callable for DBEngineSanitizer (injected from application layer)."""
sanitizer = self._sanitizers.get("db")
if sanitizer and hasattr(sanitizer, "set_dispose_engine"):
sanitizer.set_dispose_engine(dispose_engine)
logger.info("Wired dispose_engine to DBEngineSanitizer")
def register_sanitizer(self, component: str, sanitizer: Sanitizer):
"""Register a custom sanitizer for a component."""
self._sanitizers[component] = sanitizer
async def sanitize_async(self, context: ParasiteContext) -> None:
"""
Trigger deferred sanitization for a parasite.
Args:
context: ParasiteContext from detection
This method creates a background task that:
1. Waits for the configured delay
2. Executes the appropriate sanitizer
3. Logs and emits metrics
Does not block the calling task.
"""
if not self.config.should_sanitize(context.component):
# Sanitization not enabled for this component
logger.debug(
f"Sanitization not enabled for component {context.component}",
extra={"parasite_id": str(context.id)},
)
return
# Create background task
task = asyncio.create_task(
self._execute_sanitization(context),
name=f"parasite_sanitize_{context.component}_{context.id}",
)
self._active_tasks[str(context.id)] = task
logger.info(
f"Deferred sanitization scheduled for parasite {context.id}",
extra={
"parasite_id": str(context.id),
"component": context.component,
"delay_seconds": self.config.components[context.component].async_delay_seconds,
},
)
async def _execute_sanitization(self, context: ParasiteContext) -> None:
"""
Execute sanitization after delay.
This runs in a background task.
"""
delay = self.config.components.get(context.component, {}).async_delay_seconds
# Wait for delay (deferred sanitization)
await asyncio.sleep(delay)
# Get sanitizer for component
sanitizer = self._sanitizers.get(context.component)
if not sanitizer:
logger.warning(
f"No sanitizer registered for component {context.component}",
extra={"parasite_id": str(context.id)},
)
return
# Execute sanitization
try:
result = await sanitizer.sanitize(context)
# Update context with result
context.complete(sanitized=result.success, result=result)
# Emit profiling
from .profiler import ParasiteProfiler
profiler = ParasiteProfiler(self.config)
await profiler.record_sanitization(context, result)
# Log result
if result.success:
logger.info(
f"Sanitization succeeded for parasite {context.id}",
extra={
"parasite_id": str(context.id),
"component": context.component,
"steps": result.steps,
"duration_ms": result.duration_ms,
},
)
else:
logger.error(
f"Sanitization failed for parasite {context.id}",
extra={
"parasite_id": str(context.id),
"component": context.component,
"error": result.error,
"steps": result.steps,
},
)
except Exception as e:
logger.error(
f"Sanitization execution failed: {e}",
exc_info=True,
extra={"parasite_id": str(context.id)},
)
finally:
# Clean up task reference
self._active_tasks.pop(str(context.id), None)
async def wait_all(self, timeout: float | None = None) -> None: # noqa: ASYNC109 timeout parameter is handled by caller
"""Wait for all active sanitization tasks to complete."""
if not self._active_tasks:
return
tasks = list(self._active_tasks.values())
async with asyncio.timeout(timeout):
await asyncio.gather(*tasks, return_exceptions=True)
def get_active_count(self) -> int:
"""Get count of active sanitization tasks."""
return len(self._active_tasks)

Xet Storage Details

Size:
17.6 kB
·
Xet hash:
adbe382266b807f171f97f1b955d2d3956dff71ea4ad17839d9188029cb124d5

Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.