o
    Á¿i—  ã                   @   sf   d Z ddlZddlmZ ddlmZ eeƒZdd„ Z	dd„ Z
d	d
„ Zddd„Zddd„Zddd„ZdS )a&  
IAST (Interactive Application Security Testing) Product Entry Point

This module serves as the main entry point for IAST instrumentation and addresses critical
compatibility issues with Gevent-based applications.

=== GEVENT COMPATIBILITY ===

Applications using Gunicorn with the Gevent worker class may experience random worker timeouts
during shutdown sequences when IAST is enabled. This occurs because IAST's dynamic code
instrumentation interferes with Gevent's monkey patching mechanism.

Root Cause:
-----------
IAST relies on modules like `importlib.metadata`, `importlib`, and `subprocess`
which, when loaded at module level, cannot be properly released from memory. This creates
conflicts between the in-memory versions of these modules and Gevent's monkey patching,
leading to sporadic blocking operations that can cause worker timeouts.


Caveat:
Adding incorrect top-level imports (especially `importlib.metadata` or `subprocess`)
could reintroduce the flaky gevent timeout errors. Always import these modules locally
within functions when needed.

Note on inspect module:
While the `inspect` module can also cause gevent conflicts, we cannot drop it from
sys.modules as it breaks pytest's test collection phase. Pytest creates inspect.Signature
objects that must remain valid throughout its lifecycle.
é    N)Ú
get_logger)Úconfigc                 C   s0   zt j| = W dS  ty   t d| ¡ Y dS w )aÐ  
    Safely remove a module from sys.modules to prevent gevent conflicts.

    Modules like `importlib.metadata` and `inspect` must be removed from memory
    after IAST initialization to avoid conflicts with Gevent's monkey patching.
    If these modules remain loaded, they can interfere with Gevent's concurrency
    model and cause sporadic worker timeouts in Gunicorn applications.

    Args:
        module: Name of the module to remove from sys.modules
    z?IAST: %s module wasn't loaded, drop from sys.modules not neededN)ÚsysÚmodulesÚKeyErrorÚlogÚdebug)Úmodule© r
   úQ/home/ubuntu/.local/lib/python3.10/site-packages/ddtrace/internal/iast/product.pyÚ
_drop_safe)   s
   ÿr   c                  C   sD   t jr ddlm}  ddlm} t d¡ | ƒ  |ƒ  tdƒ dS dS )a³  
    Initialize IAST instrumentation during the preload phase.

    This function runs early in the application lifecycle (before Gevent's
    cleanup_loaded_modules if present) to ensure IAST instrumentation is
    properly established without interfering with Gevent's monkey patching.

    The initialization includes:
    1. Enabling IAST propagation (AST-based taint tracking)
    2. Patching taint sink points for vulnerability detection
    3. Cleaning up problematic modules from memory

    This early initialization is critical for Gevent compatibility and prevents
    random worker timeouts that can occur when IAST modules conflict with
    Gevent's concurrency mechanisms.
    r   )Úenable_iast_propagation)Ú
patch_iastzEnabling IAST by auto importzimportlib.metadataN)	Ú
asm_configÚ_iast_enabledÚddtrace.appsec._iastr   Úddtrace.appsec._iast.mainr   r   r   r   )r   r   r
   r
   r   Úpost_preload;   s   
ôr   c                  C   s"   t jrddlm}  |  ¡  dS dS )z!
    Start the IAST product.
    r   ©ÚAppSecIastSpanProcessorN)r   r   Úddtrace.appsec._iast.processorr   Úenabler   r
   r
   r   Ústart^   s   ýr   Fc                 C   ó   dS )z#
    Restart the IAST product.
    Nr
   ©Újoinr
   r
   r   Úrestarth   ó   r   c                 C   r   )z 
    Stop the IAST product.
    Nr
   r   r
   r
   r   Ústopo   r   r   c                 C   r   )z4
    Clean up IAST product at application exit.
    Nr
   r   r
   r
   r   Úat_exitv   r   r   )F)Ú__doc__r   Úddtrace.internal.loggerr   Úddtrace.internal.settings.asmr   r   Ú__name__r   r   r   r   r   r   r   r
   r
   r
   r   Ú<module>   s    #


