@@ -46,6 +46,7 @@ def wrapped_function(wrapped, instance, args, kwargs):
4646_IAST_TO_BE_LOADED = True
4747_iast_propagation_enabled = False
4848_fork_handler_registered = False
49+ _iast_in_pytest_mode = False
4950
5051
5152def _disable_iast_after_fork ():
@@ -87,6 +88,15 @@ def _disable_iast_after_fork():
8788 from ddtrace .appsec ._iast ._iast_request_context_base import is_iast_request_enabled
8889 from ddtrace .appsec ._iast ._taint_tracking ._context import clear_all_request_context_slots
8990
91+ # In pytest mode, always disable IAST in child processes to avoid segfaults
92+ # when tests create multiprocesses (e.g., for testing fork behavior)
93+ if _iast_in_pytest_mode :
94+ log .debug ("IAST fork handler: Pytest mode detected, disabling IAST in child process" )
95+ clear_all_request_context_slots ()
96+ IAST_CONTEXT .set (None )
97+ asm_config ._iast_enabled = False
98+ return
99+
90100 if not is_iast_request_enabled ():
91101 # No active context - this is an early fork (web framework worker)
92102 # IAST can be safely initialized fresh in this child process
@@ -168,6 +178,7 @@ def enable_iast_propagation():
168178 """Add IAST AST patching in the ModuleWatchdog"""
169179 # DEV: These imports are here to avoid _ast.ast_patching import in the top level
170180 # because they are slow and affect serverless startup time
181+
171182 if asm_config ._iast_propagation_enabled :
172183 from ddtrace .appsec ._iast ._ast .ast_patching import _should_iast_patch
173184 from ddtrace .appsec ._iast ._loader import _exec_iast_patched_module
@@ -183,19 +194,30 @@ def enable_iast_propagation():
183194
184195
185196def _iast_pytest_activation ():
186- global _iast_propagation_enabled
187- if _iast_propagation_enabled :
197+ """Configure IAST settings for pytest execution.
198+
199+ This function sets up IAST configuration but does NOT create a request context.
200+ Request contexts should be created per-test or per-request to avoid threading issues.
201+
202+ Also sets a global flag to indicate we're in pytest mode, which ensures IAST is
203+ disabled in forked child processes to prevent segfaults when tests use multiprocessing.
204+ """
205+ global _iast_in_pytest_mode
206+
207+ if not asm_config ._iast_enabled :
188208 return
189- os .environ ["DD_IAST_ENABLED" ] = os .environ .get ("DD_IAST_ENABLED" ) or "1"
209+
210+ # Mark that we're running in pytest mode
211+ # This flag is checked by the fork handler to disable IAST in child processes
212+ _iast_in_pytest_mode = True
213+
190214 os .environ ["DD_IAST_REQUEST_SAMPLING" ] = os .environ .get ("DD_IAST_REQUEST_SAMPLING" ) or "100.0"
191215 os .environ ["_DD_APPSEC_DEDUPLICATION_ENABLED" ] = os .environ .get ("_DD_APPSEC_DEDUPLICATION_ENABLED" ) or "false"
192216 os .environ ["DD_IAST_VULNERABILITIES_PER_REQUEST" ] = os .environ .get ("DD_IAST_VULNERABILITIES_PER_REQUEST" ) or "1000"
193- os .environ ["DD_IAST_MAX_CONCURRENT_REQUESTS" ] = os .environ .get ("DD_IAST_MAX_CONCURRENT_REQUESTS" ) or "1000"
194217
195218 asm_config ._iast_request_sampling = 100.0
196219 asm_config ._deduplication_enabled = False
197220 asm_config ._iast_max_vulnerabilities_per_requests = 1000
198- asm_config ._iast_max_concurrent_requests = 1000
199221 oce .reconfigure ()
200222
201223
0 commit comments