@@ -1775,8 +1775,8 @@ static void dd_clean_globals(void) {
17751775#endif
17761776}
17771777
1778- static void dd_shutdown_hooks_and_observer (void ) {
1779- zai_hook_clean ();
1778+ static void dd_shutdown_hooks_and_observer (bool fast_shutdown ) {
1779+ zai_hook_clean (fast_shutdown );
17801780
17811781#if PHP_VERSION_ID >= 80000 && PHP_VERSION_ID < 80200
17821782#if PHP_VERSION_ID < 80100
@@ -1797,7 +1797,7 @@ static void dd_shutdown_hooks_and_observer(void) {
17971797#endif
17981798}
17991799
1800- void dd_force_shutdown_tracing (void ) {
1800+ void dd_force_shutdown_tracing (bool fast_shutdown ) {
18011801 DDTRACE_G (in_shutdown ) = true;
18021802
18031803 zend_try {
@@ -1807,7 +1807,7 @@ void dd_force_shutdown_tracing(void) {
18071807 } zend_end_try ();
18081808
18091809 zend_try {
1810- if (ddtrace_flush_tracer (false, true) == FAILURE ) {
1810+ if (ddtrace_flush_tracer (false, true, fast_shutdown ) == FAILURE ) {
18111811 LOG (WARN , "Unable to flush the tracer" );
18121812 }
18131813 } zend_catch {
@@ -1818,7 +1818,7 @@ void dd_force_shutdown_tracing(void) {
18181818 ddtrace_disable_tracing_in_current_request (); // implicitly calling dd_clean_globals
18191819
18201820 // The hooks shall not be reset, just disabled at runtime.
1821- dd_shutdown_hooks_and_observer ();
1821+ dd_shutdown_hooks_and_observer (fast_shutdown );
18221822
18231823 DDTRACE_G (in_shutdown ) = false;
18241824}
@@ -1832,16 +1832,31 @@ static void dd_finalize_sidecar_lifecycle(bool clear_id) {
18321832static PHP_RSHUTDOWN_FUNCTION (ddtrace ) {
18331833 UNUSED (module_number , type );
18341834
1835+ // We deliberately select to not free some data structures, as to avoid the overhead of freeing them.
1836+ // Just proper destruction can have significant and easily measurable overhead on applications.
1837+ // Prior to PHP 7.2 fast shutdown was an opcache only feature
1838+ #if ZEND_DEBUG || PHP_VERSION_ID < 70200
1839+ bool fast_shutdown = 0 ;
1840+ #elif defined(__SANITIZE_ADDRESS__ )
1841+ char * force_fast_shutdown = getenv ("ZEND_ASAN_FORCE_FAST_SHUTDOWN" );
1842+ bool fast_shutdown = (
1843+ is_zend_mm ()
1844+ || (force_fast_shutdown && ZEND_ATOL (force_fast_shutdown ))
1845+ ) && !EG (full_tables_cleanup );
1846+ #else
1847+ bool fast_shutdown = is_zend_mm () && !EG (full_tables_cleanup );
1848+ #endif
1849+
18351850 zend_hash_destroy (& DDTRACE_G (traced_spans ));
18361851
18371852 // this needs to be done before dropping the spans
18381853 // run unconditionally because ddtrace may've been disabled mid-request
18391854 ddtrace_exec_handlers_rshutdown ();
18401855
18411856 if (get_DD_TRACE_ENABLED ()) {
1842- dd_force_shutdown_tracing ();
1857+ dd_force_shutdown_tracing (fast_shutdown );
18431858 } else if (!ddtrace_disable ) {
1844- dd_shutdown_hooks_and_observer ();
1859+ dd_shutdown_hooks_and_observer (fast_shutdown );
18451860 }
18461861
18471862 if (DDTRACE_G (remote_config_state )) {
@@ -1851,7 +1866,9 @@ static PHP_RSHUTDOWN_FUNCTION(ddtrace) {
18511866 if (!ddtrace_disable ) {
18521867 ddtrace_autoload_rshutdown ();
18531868
1854- OBJ_RELEASE (& DDTRACE_G (active_stack )-> std );
1869+ if (!fast_shutdown ) {
1870+ OBJ_RELEASE (& DDTRACE_G (active_stack )-> std );
1871+ }
18551872 DDTRACE_G (active_stack ) = NULL ;
18561873 }
18571874
@@ -2344,7 +2361,7 @@ PHP_FUNCTION(dd_trace_serialize_closed_spans) {
23442361 ddtrace_mark_all_span_stacks_flushable ();
23452362
23462363 ddog_TracesBytes * traces = ddog_get_traces ();
2347- ddtrace_serialize_closed_spans_with_cycle (traces );
2364+ ddtrace_serialize_closed_spans_with_cycle (traces , false );
23482365
23492366 zval traces_zv = dd_serialize_rust_traces_to_zval (traces );
23502367
@@ -3221,7 +3238,7 @@ PHP_FUNCTION(DDTrace_flush) {
32213238 if (get_DD_AUTOFINISH_SPANS ()) {
32223239 ddtrace_close_userland_spans_until (NULL );
32233240 }
3224- if (ddtrace_flush_tracer (false, get_DD_TRACE_FLUSH_COLLECT_CYCLES ()) == FAILURE ) {
3241+ if (ddtrace_flush_tracer (false, get_DD_TRACE_FLUSH_COLLECT_CYCLES (), false ) == FAILURE ) {
32253242 LOG_LINE (WARN , "Unable to flush the tracer" );
32263243 }
32273244 RETURN_NULL ();
0 commit comments