Skip to content

Commit ce6a239

Browse files
committed
Clean up comments for clarity
1 parent 06b0046 commit ce6a239

File tree

6 files changed

+17
-40
lines changed

6 files changed

+17
-40
lines changed

bin_tests/src/modes/unix/test_010_runtime_callback_frame.rs

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -46,17 +46,13 @@ unsafe extern "C" fn test_runtime_callback_frame(
4646
emit_frame: unsafe extern "C" fn(*const RuntimeStackFrame),
4747
_emit_stacktrace_string: unsafe extern "C" fn(*const c_char),
4848
) {
49-
// Use static null-terminated strings to avoid allocation in signal context
50-
// In a real runtime, these would come from the runtime's managed string pool
51-
// Using fully qualified function names that include module/class hierarchy
5249
static FUNCTION_NAME_1: &[u8] = b"test_module.TestClass.runtime_function_1\0";
5350
static FUNCTION_NAME_2: &[u8] = b"my_package.submodule.MyModule.runtime_function_2\0";
5451
static FUNCTION_NAME_3: &[u8] = b"__main__.runtime_main\0";
5552
static FILE_NAME_1: &[u8] = b"script.py\0";
5653
static FILE_NAME_2: &[u8] = b"module.py\0";
5754
static FILE_NAME_3: &[u8] = b"main.py\0";
5855

59-
// Frame 1: test_module.TestClass.runtime_function_1 in script.py
6056
let frame1 = RuntimeStackFrame {
6157
function_name: FUNCTION_NAME_1.as_ptr() as *const c_char,
6258
file_name: FILE_NAME_1.as_ptr() as *const c_char,
@@ -65,7 +61,6 @@ unsafe extern "C" fn test_runtime_callback_frame(
6561
};
6662
emit_frame(&frame1);
6763

68-
// Frame 2: my_package.submodule.MyModule.runtime_function_2 in module.py
6964
let frame2 = RuntimeStackFrame {
7065
function_name: FUNCTION_NAME_2.as_ptr() as *const c_char,
7166
file_name: FILE_NAME_2.as_ptr() as *const c_char,
@@ -74,7 +69,6 @@ unsafe extern "C" fn test_runtime_callback_frame(
7469
};
7570
emit_frame(&frame2);
7671

77-
// Frame 3: __main__.runtime_main in main.py
7872
let frame3 = RuntimeStackFrame {
7973
function_name: FUNCTION_NAME_3.as_ptr() as *const c_char,
8074
file_name: FILE_NAME_3.as_ptr() as *const c_char,
@@ -96,10 +90,8 @@ mod tests {
9690
clear_runtime_callback();
9791
}
9892

99-
// Test that no callback is initially registered
10093
assert!(!is_runtime_callback_registered());
10194

102-
// Test frame mode registration
10395
let result =
10496
register_runtime_stack_callback(test_runtime_callback_frame, CallbackType::Frame);
10597
assert!(result.is_ok(), "Frame callback registration should succeed");
@@ -108,7 +100,6 @@ mod tests {
108100
"Callback should be registered"
109101
);
110102

111-
// Clean up
112103
unsafe {
113104
clear_runtime_callback();
114105
}

bin_tests/src/modes/unix/test_011_runtime_callback_string.rs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -60,15 +60,12 @@ mod tests {
6060

6161
#[test]
6262
fn test_runtime_callback_string_registration() {
63-
// Ensure clean state
6463
unsafe {
6564
clear_runtime_callback();
6665
}
6766

68-
// Test that no callback is initially registered
6967
assert!(!is_runtime_callback_registered());
7068

71-
// Test string mode registration
7269
let result = register_runtime_stack_callback(
7370
test_runtime_callback_string,
7471
CallbackType::StacktraceString,
@@ -82,7 +79,6 @@ mod tests {
8279
"Callback should be registered"
8380
);
8481

85-
// Clean up
8682
unsafe {
8783
clear_runtime_callback();
8884
}

bin_tests/tests/crashtracker_bin_test.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -281,7 +281,6 @@ fn test_crash_tracking_bin_runtime_callback_frame_impl(
281281
p.wait().unwrap()
282282
});
283283

284-
// Runtime callback tests should crash like normal tests
285284
assert!(!exit_status.success());
286285

287286
let stderr_path = format!("{0}/out.stderr", fixtures.output_dir.display());
@@ -369,8 +368,8 @@ fn validate_runtime_callback_frame_data(crash_payload: &Value) {
369368

370369
let frames = frames.unwrap();
371370
assert!(
372-
frames.len() >= 3,
373-
"Should have at least 3 runtime frames, got {}",
371+
frames.len() == 3,
372+
"Should have 3 runtime frames, got {}",
374373
frames.len()
375374
);
376375

@@ -384,7 +383,7 @@ fn validate_runtime_callback_frame_data(crash_payload: &Value) {
384383
let expected_lines = [42, 100, 10];
385384
let expected_columns = [15, 8, 1];
386385

387-
for (i, frame) in frames.iter().take(3).enumerate() {
386+
for (i, frame) in frames.iter().enumerate() {
388387
if let Some(function) = frame.get("function") {
389388
assert_eq!(
390389
function.as_str().unwrap(),

datadog-crashtracker-ffi/src/runtime_callback.rs

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -141,8 +141,6 @@ mod tests {
141141
let function_name = CString::new("TestModule.TestClass.test_function").unwrap();
142142
let file_name = CString::new("test.rb").unwrap();
143143

144-
// Create the internal RuntimeStackFrame directly; no conversion needed
145-
// since both RuntimeStackFrame and ddog_RuntimeStackFrame have identical layouts
146144
let frame = RuntimeStackFrame {
147145
function_name: function_name.as_ptr(),
148146
file_name: file_name.as_ptr(),
@@ -157,34 +155,24 @@ mod tests {
157155
fn test_ffi_callback_registration() {
158156
let _guard = TEST_MUTEX.lock().unwrap();
159157
unsafe {
160-
// Ensure clean state at start
161158
clear_runtime_callback();
162-
163-
// Test that no callback is initially registered
164159
assert!(!ddog_crasht_is_runtime_callback_registered());
165160

166-
// Test successful registration using type-safe enums
167161
let result = ddog_crasht_register_runtime_stack_callback(
168162
test_runtime_callback,
169163
CallbackType::Frame,
170164
);
171165

172166
assert_eq!(result, CallbackResult::Ok);
173-
174-
// Verify callback is now registered
175167
assert!(ddog_crasht_is_runtime_callback_registered());
176168

177-
// Test duplicate registration fails
178169
let result = ddog_crasht_register_runtime_stack_callback(
179170
test_runtime_callback,
180171
CallbackType::Frame,
181172
);
182173
assert_eq!(result, CallbackResult::Ok);
183-
184-
// Callback should still be registered after successful re-registration
185174
assert!(ddog_crasht_is_runtime_callback_registered());
186175

187-
// Clean up - clear the registered callback for subsequent tests
188176
clear_runtime_callback();
189177
}
190178
}
@@ -195,10 +183,8 @@ mod tests {
195183
unsafe {
196184
clear_runtime_callback();
197185

198-
// Test that no callback is initially registered
199186
assert!(!ddog_crasht_is_runtime_callback_registered());
200187

201-
// Test registration with enum values - Python + StacktraceString
202188
let result = ddog_crasht_register_runtime_stack_callback(
203189
test_runtime_callback,
204190
CallbackType::StacktraceString,
@@ -207,15 +193,13 @@ mod tests {
207193
assert_eq!(result, CallbackResult::Ok);
208194
assert!(ddog_crasht_is_runtime_callback_registered());
209195

210-
// Verify callback type
211196
let callback_type_ptr = ddog_crasht_get_registered_callback_type();
212197
assert!(!callback_type_ptr.is_null());
213198
let callback_type_str = std::ffi::CStr::from_ptr(callback_type_ptr)
214199
.to_str()
215200
.unwrap();
216201
assert_eq!(callback_type_str, "stacktrace_string");
217202

218-
// Test re-registration with different values - Ruby + Frame
219203
let result = ddog_crasht_register_runtime_stack_callback(
220204
test_runtime_callback,
221205
CallbackType::Frame,

datadog-crashtracker/src/receiver/receive_report.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,13 +151,20 @@ fn process_line(
151151
};
152152
builder.with_experimental_runtime_stack(runtime_stack)?;
153153
}
154+
} else {
155+
builder.with_log_message(
156+
format!("Unable to parse runtime stack frames: {line}"),
157+
true,
158+
)?;
154159
}
155160
StdinState::RuntimeStackFrame
156161
}
157162
StdinState::RuntimeStackString(lines)
158163
if line.starts_with(DD_CRASHTRACK_END_RUNTIME_STACK_STRING) =>
159164
{
160165
// Join all accumulated lines with newlines to reconstruct the full stack trace
166+
// This is necessary because although the stacktrace string is sent as a single string,
167+
// there may be newlines in the string
161168
let stacktrace_string = lines.join("\n");
162169
let runtime_stack = RuntimeStack {
163170
format: "Datadog Runtime Callback 1.0".to_string(),

datadog-crashtracker/src/runtime_callback.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,6 @@ impl std::str::FromStr for CallbackType {
5555
}
5656

5757
/// Global storage for the runtime callback
58-
///
59-
/// Uses atomic pointer to ensure safe access from signal handlers
6058
static RUNTIME_CALLBACK: AtomicPtr<(RuntimeStackCallback, CallbackType)> =
6159
AtomicPtr::new(ptr::null_mut());
6260

@@ -76,7 +74,8 @@ pub struct RuntimeStackFrame {
7674

7775
/// Function signature for runtime stack collection callbacks
7876
///
79-
/// This callback is invoked during crash handling in a signal context, so it must be signal-safe:
77+
/// This callback is invoked during crash handling in a signal context, so it must be best effort
78+
/// signal-safe:
8079
/// - No dynamic memory allocation
8180
/// - No mutex operations
8281
/// - No I/O operations
@@ -148,7 +147,7 @@ pub fn register_runtime_stack_callback(
148147
let previous = RUNTIME_CALLBACK.swap(callback_data, Ordering::SeqCst);
149148

150149
if !previous.is_null() {
151-
// Safety: previous was returned by Box::into_raw() above or in a previous call,
150+
// Safety: previous was returned by Box::into_raw() above,
152151
// so it's guaranteed to be a valid Box pointer. We reconstruct the Box to drop it.
153152
let _ = unsafe { Box::from_raw(previous) };
154153
}
@@ -180,8 +179,7 @@ pub unsafe fn get_registered_callback_type_enum() -> Option<CallbackType> {
180179

181180
// Safety: callback_ptr was checked to be non-null above, and was created by
182181
// Box::into_raw() in register_runtime_stack_callback(), so it's a valid pointer
183-
// to a properly aligned, initialized tuple. The atomic load with SeqCst ordering
184-
// ensures we see the pointer after it was stored.
182+
// to a properly aligned, initialized tuple.
185183
let (_, callback_type) = &*callback_ptr;
186184
Some(*callback_type)
187185
}
@@ -256,7 +254,9 @@ pub(crate) unsafe fn invoke_runtime_callback_with_writer<W: std::io::Write>(
256254
use std::sync::atomic::{AtomicPtr, Ordering};
257255

258256
// Thread-safe storage for the current callback context
259-
// Store as raw data and meta pointers
257+
// Store as raw data and vtable pointers
258+
// We do this because trait objects are stored as raw pointers to its definition and also
259+
// the vtable for its impls, so we need to store both.
260260
static CURRENT_WRITER_DATA: AtomicPtr<()> = AtomicPtr::new(ptr::null_mut());
261261
static CURRENT_WRITER_VTABLE: AtomicPtr<()> = AtomicPtr::new(ptr::null_mut());
262262
static CURRENT_FRAME_COUNT: AtomicPtr<usize> = AtomicPtr::new(ptr::null_mut());

0 commit comments

Comments
 (0)