Repository navigation
[Core] Fix use-after-free race condition in OpenTelemetry gauge metric callback during shutdown - #60048
Conversation
There was a problem hiding this comment.
Code Review
This pull request addresses a use-after-free race condition that can occur in the OpenTelemetry gauge metric callback during shutdown. The fix introduces an IsShutdown() method and an early exit in the callback if the metric recorder is shutting down. This prevents further processing when the system is in a shutdown state, which could lead to using invalid objects. The changes are well-implemented, using an atomic flag for thread-safe state checking. The approach is sound and effectively mitigates the reported issue.
| @@ -72,6 +72,9 @@ class OpenTelemetryMetricRecorder { | |||
| // shutdown, so it can be consistent with the shutdown behavior of stats.h. | |||
| void Shutdown(); | |||
|
|
|||
| // Check if the recorder has been shut down. | |||
There was a problem hiding this comment.
nit: don't need this comment when the code is explicit.
| @@ -41,9 +41,13 @@ using ray::observability::OpenTelemetryMetricRecorder; | |||
|
|
|||
| static void _DoubleGaugeCallback(opentelemetry::metrics::ObserverResult observer, | |||
There was a problem hiding this comment.
Not part of your PR but
nit: we don't use _ prefix in C++ to signify private.
There was a problem hiding this comment.
I can make it as a good first issue, thank you
Signed-off-by: yicheng <[email protected]>
d1562ce to
6f9d97c
Compare
|
Just to mention, without this PR, some metrics could be lost because |
israbbani
left a comment
There was a problem hiding this comment.
Thanks for doing this!
…ge metric callback during shutdown (ray-project#60048)" This reverts commit c9ff164. Signed-off-by: joshlee <[email protected]>
Description
When a Ray worker process shuts down (e.g., during
ray.shutdown()or node termination), the OpenTelemetryPeriodicExportingMetricReader's background thread may still be invoking the gauge callback (_DoubleGaugeCallback), which then accesses already-destroyed member data, resulting in a use-after-free crash.The error message:
I looked further into this, and ideally, at the OpenTelemetry code level, shutdown should be handled correctly.
PeriodicExportingMetricReader's shutdown waits for
worker_thread_to finish.And callback(
worker_thread_) is in a while (IsShutdown() != true) loop.Therefore, there should be no use-after-free race condition at the OpenTelemetry code level, and it should be safe to call
meter_provider_->Shutdown().However, the issue is that the last callback appears to access member data that has already been destroyed during ForceFlush, which is called before Shutdown. This member data belongs to the OpenTelemetry SDK itself.
The more I look into it, the more it feels like this is actually a bug in the OpenTelemetry SDK.
And even further, I found this:[SDK] Use shared_ptr internally for AttributesProcessor to prevent use-after-free
Which is exactly the issue I encountered!
This PR upgrade the OpenTelemetry C++ SDK version to include this fix.
Related issues
Additional information
It is quit easy to reproduced, For example, if we manually running the
test_placement_group_reschedule_node_deadinpython/ray/autoscaler/v2/tests/test_e2e.py.after this pr, no such error message: