From b528cd33b3d44b348863f9ba99450a5682e5ea05 Mon Sep 17 00:00:00 2001 From: Florian Engelhardt Date: Thu, 20 Feb 2025 14:33:11 +0100 Subject: [PATCH] better saveguard non-PHP threads sampling --- profiling/src/io.rs | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/profiling/src/io.rs b/profiling/src/io.rs index baf50b3e1a..60952392c5 100644 --- a/profiling/src/io.rs +++ b/profiling/src/io.rs @@ -211,14 +211,6 @@ unsafe extern "C" fn callback(info: *mut dl_phdr_info, _size: usize, data: *mut return 0; } - // `curl_exec()` will spawn a new thread for name resolution. GOT hooking follows threads and - // as such we might sample from another thread even in a NTS build of PHP, which we should not. - // So instead of crashing (or risking a crash) we currently refrain from collection I/O from - // name resolution. - if name.contains("resolve") { - return 0; - } - override_got_entry(info, overwrites); 0 @@ -563,6 +555,17 @@ impl IOProfilingStats { } fn track(&mut self, value: u64) { + let zend_thread = REQUEST_LOCALS.with(|cell| { + let locals = cell.borrow(); + locals.vm_interrupt_addr.is_null() + }); + if !zend_thread { + // `curl_exec()` for example will spawn a new thread for name resolution. GOT hooking + // follows threads and as such we might sample from another (non PHP) thread even in a + // NTS build of PHP. We have observed crashes for these cases, so instead of crashing + // (or risking a crash) we refrain from collection I/O. + return; + } if let Some(next_sample) = self.next_sample.checked_sub(value) { self.next_sample = next_sample; return;