Skip to content

Commit 82688ab

Browse files
Add missing "in-preloading" check to RSHUTDOWN (#3037)
1 parent 46ec759 commit 82688ab

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
@@ -490,6 +490,23 @@ extern "C" fn rinit(_type: c_int, _module_number: c_int) -> ZendResult {
490490
locals.system_settings = system_settings;
491491
});
492492

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

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

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

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

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

635640
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)