Skip to content

Commit 65647db

Browse files
jamieknvidiaaraza008
authored andcommitted
fix(mxc-etw): open ETW trace on caller thread so start_session reports real status
Review item #4 (Shailendra): start_session previously returned Ok(EtwSession) as soon as the pump thread was spawned, but OpenTraceW ran later inside that thread; if it failed we still handed back a live-looking session and logged 'consumer started' (silent failure = false audit coverage). Split the two Win32 calls instead of adding a channel handshake (avoids any lost-wakeup/hang risk): the quick, synchronous OpenTraceW now runs on the caller thread (open_trace), and only the blocking ProcessTrace runs on the pump thread (run_trace). start_session returns Err if OpenTraceW fails (reclaiming the boxed Sender so the consumer disconnects, stopping the session, joining the consumer) and returns Ok/logs 'started' only once capture is genuinely open. Opened handle + LoggerName buffer + boxed Sender are carried to the pump via a Send OpenedTrace so they outlive ProcessTrace. Box-verified on 7F203-MXC-001: consumer started=True, failed-to-start=False, 50 OCSF rows / 50 JSONL, BuffersLost=0 (no regression to capture/emit). Signed-off-by: Akber Raza <[email protected]>
1 parent 416be51 commit 65647db

1 file changed

Lines changed: 81 additions & 23 deletions

File tree

‎crates/openshell-driver-mxc/src/etw_consumer.rs‎

Lines changed: 81 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,9 @@ use windows::Win32::System::Diagnostics::Etw::{
6262
CONTROLTRACE_HANDLE, CloseTrace, ControlTraceW, EVENT_HEADER, EVENT_HEADER_EXTENDED_DATA_ITEM,
6363
EVENT_PROPERTY_INFO, EVENT_RECORD, EVENT_TRACE_CONTROL_STOP, EVENT_TRACE_LOGFILEW,
6464
EVENT_TRACE_PROPERTIES, EVENT_TRACE_REAL_TIME_MODE, EnableTraceEx2, OpenTraceW,
65-
PROCESS_TRACE_MODE_EVENT_RECORD, PROCESS_TRACE_MODE_REAL_TIME, ProcessTrace, StartTraceW,
66-
TRACE_EVENT_INFO, TRACE_LEVEL_VERBOSE, TdhGetEventInformation, WNODE_FLAG_TRACED_GUID,
65+
PROCESS_TRACE_MODE_EVENT_RECORD, PROCESS_TRACE_MODE_REAL_TIME, PROCESSTRACE_HANDLE,
66+
ProcessTrace, StartTraceW, TRACE_EVENT_INFO, TRACE_LEVEL_VERBOSE, TdhGetEventInformation,
67+
WNODE_FLAG_TRACED_GUID,
6768
};
6869
use windows::core::{GUID, PCWSTR, PWSTR};
6970

@@ -247,11 +248,22 @@ impl Drop for EtwSession {
247248
}
248249
}
249250

250-
/// Wrapper to move a raw `Sender` pointer across the thread boundary into the
251-
/// blocking `ProcessTrace` worker. SAFETY: the boxed `Sender` lives until the
252-
/// worker reclaims it after `ProcessTrace` returns.
253-
struct SendPtr(*mut mpsc::Sender<RawEtwEvent>);
254-
unsafe impl Send for SendPtr {}
251+
/// A successfully-opened real-time trace, handed to the pump thread to run the
252+
/// blocking `ProcessTrace`. Produced by [`open_trace`] on the *caller* thread so
253+
/// an `OpenTraceW` failure is surfaced synchronously (review #4) rather than
254+
/// dying silently on the worker after `start_session` already returned `Ok`.
255+
///
256+
/// SAFETY (`Send`): the contained raw `Sender` pointer and trace handle are only
257+
/// ever touched by the single pump thread that takes ownership of this struct;
258+
/// the boxed `Sender` lives until that thread reclaims it after `ProcessTrace`
259+
/// returns, and `name` (the `LoggerName` buffer `OpenTraceW` referenced) is kept
260+
/// alive for the whole `ProcessTrace` duration.
261+
struct OpenedTrace {
262+
handle: PROCESSTRACE_HANDLE,
263+
name: Vec<u16>,
264+
tx_ptr: *mut mpsc::Sender<RawEtwEvent>,
265+
}
266+
unsafe impl Send for OpenedTrace {}
255267

256268
// ---------------------------------------------------------------------------
257269
// Public API
@@ -307,14 +319,37 @@ pub(crate) fn start_session(index: Arc<Mutex<AttributionIndex>>) -> Result<EtwSe
307319
format!("failed to spawn ETW consumer thread: {e}")
308320
})?;
309321

310-
let send_ptr = SendPtr(Box::into_raw(Box::new(tx)));
311-
let pump_thread = std::thread::Builder::new()
322+
// Open the trace on THIS thread (review #4): `OpenTraceW` is a quick,
323+
// synchronous call, so we can return its failure to the caller instead of
324+
// reporting the session "started" and then having the worker die silently.
325+
// Only the *blocking* `ProcessTrace` runs on the pump thread. On failure we
326+
// reclaim the boxed Sender (which disconnects the consumer's channel so it
327+
// exits), stop the session, and join the consumer before returning `Err`.
328+
let tx_ptr = Box::into_raw(Box::new(tx));
329+
let opened = match open_trace(tx_ptr) {
330+
Ok(o) => o,
331+
Err(e) => {
332+
unsafe { drop(Box::from_raw(tx_ptr)) };
333+
stop_session(handle);
334+
let _ = consumer_thread.join();
335+
return Err(e);
336+
}
337+
};
338+
339+
let pump_thread = match std::thread::Builder::new()
312340
.name("etw-ocsf-pump".into())
313-
.spawn(move || process_trace_loop(send_ptr))
314-
.map_err(|e| {
341+
.spawn(move || run_trace(opened))
342+
{
343+
Ok(t) => t,
344+
Err(e) => {
345+
// The trace is open but we couldn't spawn the pump. Stop the session,
346+
// reclaim the boxed Sender so the consumer disconnects, and join it.
347+
unsafe { drop(Box::from_raw(tx_ptr)) };
315348
stop_session(handle);
316-
format!("failed to spawn ETW pump thread: {e}")
317-
})?;
349+
let _ = consumer_thread.join();
350+
return Err(format!("failed to spawn ETW pump thread: {e}"));
351+
}
352+
};
318353

319354
tracing::info!(
320355
session = SESSION_NAME,
@@ -473,9 +508,13 @@ fn cleanup_stale_session() {
473508
// ProcessTrace loop (dedicated blocking thread)
474509
// ---------------------------------------------------------------------------
475510

511+
/// Open the real-time consumer with `OpenTraceW` on the **caller** thread so the
512+
/// result is synchronous (review #4). `tx_ptr` is the boxed event `Sender`; on
513+
/// failure the caller reclaims it (we do not drop it here). On success the boxed
514+
/// `Sender` and the `LoggerName` buffer are handed to the returned [`OpenedTrace`]
515+
/// so they outlive the subsequent blocking `ProcessTrace`.
476516
#[allow(clippy::field_reassign_with_default)]
477-
fn process_trace_loop(send_ptr: SendPtr) {
478-
let tx_ptr = send_ptr.0;
517+
fn open_trace(tx_ptr: *mut mpsc::Sender<RawEtwEvent>) -> Result<OpenedTrace, String> {
479518
let mut name = session_name_wide();
480519

481520
let mut logfile = EVENT_TRACE_LOGFILEW::default();
@@ -485,20 +524,39 @@ fn process_trace_loop(send_ptr: SendPtr) {
485524
logfile.Anonymous2.EventRecordCallback = Some(event_record_callback);
486525
logfile.Context = tx_ptr.cast::<c_void>();
487526

488-
let trace_handle = unsafe { OpenTraceW(&mut logfile) };
489-
if trace_handle.Value == u64::MAX {
490-
tracing::error!(err = %std::io::Error::last_os_error(), "ETW OpenTraceW failed");
491-
// Reclaim the boxed Sender so the consumer thread's channel closes.
492-
unsafe { drop(Box::from_raw(tx_ptr)) };
493-
return;
527+
let handle = unsafe { OpenTraceW(&mut logfile) };
528+
if handle.Value == u64::MAX {
529+
return Err(format!(
530+
"ETW OpenTraceW failed: {}",
531+
std::io::Error::last_os_error()
532+
));
494533
}
495534

496-
let _ = unsafe { ProcessTrace(&[trace_handle], None, None) };
535+
Ok(OpenedTrace {
536+
handle,
537+
name,
538+
tx_ptr,
539+
})
540+
}
541+
542+
/// Run the blocking `ProcessTrace` pump for an already-opened trace, then clean
543+
/// up. Owns [`OpenedTrace`] for its whole lifetime so the `LoggerName` buffer and
544+
/// boxed `Sender` stay valid until `ProcessTrace` returns.
545+
fn run_trace(opened: OpenedTrace) {
546+
let OpenedTrace {
547+
handle,
548+
name,
549+
tx_ptr,
550+
} = opened;
551+
552+
let _ = unsafe { ProcessTrace(&[handle], None, None) };
497553

498554
unsafe {
499-
let _ = CloseTrace(trace_handle);
555+
let _ = CloseTrace(handle);
500556
drop(Box::from_raw(tx_ptr));
501557
}
558+
// Keep the LoggerName buffer alive until ProcessTrace has fully returned.
559+
drop(name);
502560
}
503561

504562
unsafe extern "system" fn event_record_callback(event_record: *mut EVENT_RECORD) {

0 commit comments

Comments
 (0)