Skip to content

Commit a453e6e

Browse files
Add missing "in-preloading" check to RSHUTDOWN (#3037)
1 parent 46258ca commit a453e6e

File tree

2 files changed

+25
-20
lines changed

2 files changed

+25
-20
lines changed

profiling/src/lib.rs

Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -491,6 +491,23 @@ extern "C" fn rinit(_type: c_int, _module_number: c_int) -> ZendResult {
491491
locals.system_settings = system_settings;
492492
});
493493

494+
// Preloading happens before zend_post_startup_cb is called for the first
495+
// time. When preloading is enabled and a non-root user is used for
496+
// php-fpm, there is fork that happens. In the past, having the profiler
497+
// enabled at this time would cause php-fpm eventually hang once the
498+
// Profiler's channels were full; this has been fixed. See:
499+
// https://github.com/DataDog/dd-trace-php/issues/1919
500+
//
501+
// There are a few ways to handle this preloading scenario with the fork,
502+
// but the simplest is to not enable the profiler until the engine's
503+
// startup is complete. This means the preloading will not be profiled,
504+
// but this should be okay.
505+
#[cfg(php_preload)]
506+
if !unsafe { bindings::ddog_php_prof_is_post_startup() } {
507+
debug!("zend_post_startup_cb hasn't happened yet; not enabling profiler.");
508+
return ZendResult::Success;
509+
}
510+
494511
// SAFETY: still safe to access in rinit after first_rinit.
495512
let system_settings = unsafe { system_settings.as_mut() };
496513

@@ -531,23 +548,6 @@ extern "C" fn rinit(_type: c_int, _module_number: c_int) -> ZendResult {
531548
exception::exception_profiling_first_rinit();
532549
});
533550

534-
// Preloading happens before zend_post_startup_cb is called for the first
535-
// time. When preloading is enabled and a non-root user is used for
536-
// php-fpm, there is fork that happens. In the past, having the profiler
537-
// enabled at this time would cause php-fpm eventually hang once the
538-
// Profiler's channels were full; this has been fixed. See:
539-
// https://github.com/DataDog/dd-trace-php/issues/1919
540-
//
541-
// There are a few ways to handle this preloading scenario with the fork,
542-
// but the simplest is to not enable the profiler until the engine's
543-
// startup is complete. This means the preloading will not be profiled,
544-
// but this should be okay.
545-
#[cfg(php_preload)]
546-
if !unsafe { bindings::ddog_php_prof_is_post_startup() } {
547-
debug!("zend_post_startup_cb hasn't happened yet; not enabling profiler.");
548-
return ZendResult::Success;
549-
}
550-
551551
Profiler::init(system_settings);
552552

553553
if system_settings.profiling_enabled {
@@ -631,6 +631,11 @@ extern "C" fn rshutdown(_type: c_int, _module_number: c_int) -> ZendResult {
631631
#[cfg(debug_assertions)]
632632
trace!("RSHUTDOWN({_type}, {_module_number})");
633633

634+
#[cfg(php_preload)]
635+
if !unsafe { bindings::ddog_php_prof_is_post_startup() } {
636+
return ZendResult::Success;
637+
}
638+
634639
profiling::stack_walking::rshutdown();
635640

636641
REQUEST_LOCALS.with(|cell| {

profiling/tests/phpt/preload_01.phpt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,18 +12,18 @@ See: https://github.com/DataDog/dd-trace-php/issues/1919
1212
Due to limitations of PHPT, this test does not test PHP-FPM or processes run
1313
with different permissions, but it makes sure, that profiling is started after
1414
preloading is done.
15+
--ENV--
16+
USE_ZEND_ALLOC=0
1517
--SKIPIF--
1618
<?php
1719
if (PHP_VERSION_ID < 70400)
18-
echo "skip: need preloading and therefore PHP", PHP_EOL;
20+
echo "skip: need preloading and therefore PHP >= 7.4.0", PHP_EOL;
1921
if (!extension_loaded('datadog-profiling'))
2022
echo "skip: test requires datadog-profiling", PHP_EOL;
2123
?>
2224
--INI--
2325
datadog.profiling.enabled=yes
2426
datadog.profiling.log_level=debug
25-
datadog.profiling.allocation_enabled=no
26-
datadog.profiling.experimental_cpu_time_enabled=no
2727
zend_extension=opcache
2828
opcache.enable_cli=1
2929
opcache.preload={PWD}/preload_01_preload.php

0 commit comments

Comments
 (0)