You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
feat(data-pipeline)!: add fork safety hooks and cancellation token for trace exporter FFI (#2051)
# What does this PR do?
Add fork safety and cooperative cancellation support to the trace exporter FFI:
1. **Fork hooks**: Expose `ddog_trace_exporter_before_fork`, `_after_fork_in_parent`,
and `_after_fork_in_child` that delegate to the underlying `SharedRuntime`. These
allow C callers to coordinate the tokio runtime lifecycle around process forks.
2. **Cancellation token**: Introduce `ddog_trace_exporter_cancel_token_new`, `_cancel`,
and `_drop` for managing cancellation tokens. Thread the token into
`ddog_trace_exporter_send_trace_chunks` via `tokio::select!` so callers can
cooperatively abort in-flight HTTP requests.
# Motivation
FUP to #1952.
Ruby application servers (Puma, Unicorn, Passenger) fork worker processes. The tokio
runtime does not survive `fork()` — its threads are not carried over. Without the fork
hooks, the trace exporter is dead in child processes.
The existing `RUBY_UBF_IO` unblock function on the dd-trace-rb side sends a signal
that cannot actually cancel an in-flight Rust HTTP request. The cancellation token
enables cooperative abort: when Ruby interrupts a thread (shutdown, `Thread#kill`),
the UBF cancels the token, which causes `tokio::select!` to abort the send and return
promptly.
# Additional Notes
- Added a `pub fn shared_runtime()` accessor to `TraceExporter` since the field was private.
- The cancellation token uses `Handle<CancellationToken>` from `libdd-common-ffi`, same
pattern as profiling-ffi, with a `ddog_TraceExporterCancelToken` cbindgen rename to
avoid symbol conflicts.
- The `cancel` parameter on `send_trace_chunks` is nullable; passing `NULL` preserves
existing behavior.
AI was used to accelerate implementation; all code was reviewed and understood.
# How to test the change?
```
cargo test -p libdd-data-pipeline-ffi --lib
```
46 tests pass (40 existing + 6 new for fork hooks and cancellation token).
Co-authored-by: dd-octo-sts[bot] <200755185+dd-octo-sts[bot]@users.noreply.github.com>
Co-authored-by: VianneyRuhlmann <vianney.ruhlmann@datadoghq.com>
Copy file name to clipboardExpand all lines: libdd-data-pipeline-ffi/README.md
+4Lines changed: 4 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -5,3 +5,7 @@ C FFI bindings for the libdd-data-pipeline library.
5
5
## Overview
6
6
7
7
`libdd-data-pipeline-ffi` provides C-compatible FFI bindings for `libdd-data-pipeline`, enabling high-performance trace processing from C, C++, PHP, Ruby, Python, and other languages.
8
+
9
+
## Dependencies
10
+
11
+
This crate depends on `tokio-util` for its `CancellationToken` type. The cancellation token created by `ddog_trace_exporter_cancel_token_new` and passed to `ddog_trace_exporter_send_trace_chunks` is a `tokio_util::sync::CancellationToken`, which the data pipeline uses to cooperatively abort an in-flight send. The token is exposed opaquely to C, so callers never need to depend on `tokio-util` themselves.
0 commit comments