[BUG] Exception escaping noexcept Session::SendRequest in curl HTTP client calls std::terminate, killing the host process
維護者通常 1 天內回覆
還沒有人認領這個 Issue。
評估
- 難度
- 4/5
- 預估耗時
- 3-5 天
- 新手友好度
- 45/100
- Issue 類型
- 缺陷
- 描述清晰度
- 描述清楚
- 活躍度
- 活躍
- 技術堆疊
- cpp
研究方向
Start with ext/src/http/client/curl/http_client_curl.cc, especially Session::SendRequest and HttpClient::MaybeSpawnBackgroundThread; read the SendRequest contract in ext/include/opentelemetry/ext/http/client/http_client.h and the existing cleanup path in exporters/otlp/src/otlp_http_client.cc. Trace how a failed send releases the session and wakes synchronous exports, then check the curl client tests for coverage. Done means exceptions from these noexcept paths are handled without terminating the host process, with session cleanup preserved and failures reported through the error handler.
由索引模型根據 Issue 內容生成。
描述
Describe your environment
- opentelemetry-cpp v1.22.0. Also verified present on
mainas of 2026-11-16 — see "Additional context". - Linux x86-64, Clang, libstdc++, static libs.
- Build options:
WITH_OTLP_HTTP=ON,OPENTELEMETRY_ABI_VERSION_NO=2,OPENTELEMETRY_STL_VERSION=2023,OPENTELEMETRY_NO_DEPRECATED_CODE,HAVE_ABSEIL.ENABLE_ASYNC_EXPORTnot defined, so the synchronousOtlpHttpClient::Export(message)path is used. - Exporter: OTLP/HTTP metric exporter behind
PeriodicExportingMetricReader.
Steps to reproduce
Session::SendRequest is declared noexcept but performs several operations that can throw.
Any of them terminates the process. The most deterministic to demonstrate is the lazy creation
of the curl background thread, which throws std::system_error when pthread_create fails.
repro.cc:
#include <opentelemetry/exporters/otlp/otlp_http_metric_exporter_factory.h>
#include <opentelemetry/exporters/otlp/otlp_http_metric_exporter_options.h>
#include <opentelemetry/sdk/metrics/export/periodic_exporting_metric_reader.h>
#include <opentelemetry/sdk/metrics/export/periodic_exporting_metric_reader_options.h>
#include <opentelemetry/sdk/metrics/meter_provider.h>
#include <opentelemetry/sdk/metrics/view/view_registry.h>
#include <chrono>
#include <memory>
#include <thread>
int main()
{
namespace otlp = opentelemetry::exporter::otlp;
namespace metrics = opentelemetry::sdk::metrics;
// Default endpoint; nothing needs to be listening for this repro.
otlp::OtlpHttpMetricExporterOptions exporter_options;
auto exporter = otlp::OtlpHttpMetricExporterFactory::Create(exporter_options);
metrics::PeriodicExportingMetricReaderOptions reader_options;
reader_options.export_interval_millis = std::chrono::milliseconds(10000);
reader_options.export_timeout_millis = std::chrono::milliseconds(5000);
auto reader = std::make_shared<metrics::PeriodicExportingMetricReader>(
std::move(exporter), reader_options);
auto provider = std::make_shared<metrics::MeterProvider>(
std::make_unique<metrics::ViewRegistry>());
provider->AddMetricReader(reader);
auto counter = provider->GetMeter("repro")->CreateUInt64Counter("repro_counter");
counter->Add(1);
std::this_thread::sleep_for(std::chrono::seconds(20)); // allow one export
return 0;
}
fail_pthread_create.c — makes pthread_create fail once the SDK's own threads are up, so
only the curl client's lazily-created background thread is affected:
#define _GNU_SOURCE
#include <dlfcn.h>
#include <errno.h>
#include <pthread.h>
#include <time.h>
static time_t start;
__attribute__((constructor)) static void init(void) { start = time(NULL); }
int pthread_create(pthread_t *t, const pthread_attr_t *a,
void *(*fn)(void *), void *arg)
{
static int (*real)(pthread_t *, const pthread_attr_t *, void *(*)(void *), void *);
if (!real) real = dlsym(RTLD_NEXT, "pthread_create");
/* SDK reader threads are created during startup; the curl background thread is
created lazily on the first export, at ~10s. Fail anything after 5s. */
if (time(NULL) - start >= 5)
return EAGAIN;
return real(t, a, fn, arg);
}
cc -shared -fPIC -o fail_pthread_create.so fail_pthread_create.c -ldl
LD_PRELOAD=./fail_pthread_create.so ./repro
The process aborts at the first export, roughly 10 seconds in.
The same crash is reachable without any fault injection under genuine memory pressure, via the
allocating operations listed in "Additional context".
What is the expected behavior?
The export fails, the failure is reported through GlobalLogHandler, and the application keeps
running.
The error-handling specification
requires this. Four of its requirements apply:
- Implementations must not throw unhandled exceptions at runtime.
- The SDK must not throw unhandled exceptions for failures in its own operations — the spec's own
illustrating example is an exporter that cannot reach the endpoint it sends telemetry to. - The API or SDK may fail fast at initialization, but must not cause the application to fail later
at runtime. - Background tasks should run in the context of a global error handler so their exceptions do not
affect the end user application.
PeriodicExportingMetricReader already implements this correctly:
sdk/src/metrics/export/periodic_exporting_metric_reader.cc:160-206 wraps the whole
collect-and-export call in try { ... } catch (std::exception &e) { OTEL_INTERNAL_LOG_ERROR(...); return false; } catch (...) { ... }.
What is the actual behavior?
std::terminate is called and the host process dies.
Frames #21 → #16 below are exactly the region inside that SDK try block. Because
Session::SendRequest at frame #11 is noexcept, the runtime terminates instead of unwinding,
so the SDK's handler never runs. The recovery code exists and is unreachable.
#8 __cxxabiv1::__terminate (handler=<optimized out>) at libsupc++/eh_terminate.cc:48
#9 std::terminate () at libsupc++/eh_terminate.cc:58
#10 __clang_call_terminate ()
#11 opentelemetry::v2::ext::http::client::curl::Session::SendRequest (this=0x7f4693211210, callback=...)
at ext/src/http/client/curl/http_client_curl.cc:145
#12 opentelemetry::v2::exporter::otlp::OtlpHttpClient::addSession (this=0x7f46a4ee1280, session_data=...)
at exporters/otlp/src/otlp_http_client.cc:1028
#13 opentelemetry::v2::exporter::otlp::OtlpHttpClient::Export (this=0x7f46a4ee1280, message=...,
result_callback=..., max_running_requests=0)
at exporters/otlp/src/otlp_http_client.cc:759
#14 opentelemetry::v2::exporter::otlp::OtlpHttpClient::Export (this=0x7f46a4ee1280, message=...)
at exporters/otlp/src/otlp_http_client.cc:725
#15 opentelemetry::v2::exporter::otlp::OtlpHttpMetricExporter::Export (this=0x7f46a4e0c780, data=...)
at exporters/otlp/src/otlp_http_metric_exporter.cc:204
#16 opentelemetry::v2::sdk::metrics::PeriodicExportingMetricReader::CollectAndExportOnce()::$_0::operator()(
opentelemetry::v2::sdk::metrics::ResourceMetrics&) const (this=0x7f46a057a120, metric_data=...)
at sdk/src/metrics/export/periodic_exporting_metric_reader.cc:181
#17 opentelemetry::v2::nostd::function_ref<...>::BindTo<...>(...)::{lambda(void*, ResourceMetrics&)#1}::operator()
at api/include/opentelemetry/nostd/function_ref.h:41
#18 opentelemetry::v2::nostd::function_ref<...>::BindTo<...>(...)::{lambda(void*, ResourceMetrics&)#1}::__invoke
at api/include/opentelemetry/nostd/function_ref.h:40
#19 opentelemetry::v2::nostd::function_ref<bool(ResourceMetrics&)>::operator() (args=..., this=<optimized out>)
at api/include/opentelemetry/nostd/function_ref.h:89
#20 opentelemetry::v2::sdk::metrics::MetricReader::Collect (this=<optimized out>, callback=...)
at sdk/src/metrics/metric_reader.cc:46
#21 opentelemetry::v2::sdk::metrics::PeriodicExportingMetricReader::CollectAndExportOnce (this=0x7f46a4e65510)
at sdk/src/metrics/export/periodic_exporting_metric_reader.cc:172
#22 opentelemetry::v2::sdk::metrics::PeriodicExportingMetricReader::DoBackgroundWork (this=0x7f46a4e65510)
at sdk/src/metrics/export/periodic_exporting_metric_reader.cc:107
#23 std::execute_native_thread_routine (__p=<optimized out>) at libstdc++-v3/src/c++11/thread.cc:104
#24 start_thread () from /lib64/libpthread.so.0
#25 clone () from /lib64/libc.so.6
Frames #0-#7 were the host application's own std::set_terminate handler writing a core dump;
they are omitted as they carry no information about this bug.
Additional context
Note on the line attribution in frame #11. It reads http_client_curl.cc:145, which is
const auto &url = host_ + http_request_->uri_;. That is misleading. Clang emits one shared
__clang_call_terminate landing pad per noexcept function and its line-table entry points into
the function body rather than the throw site; line 145 is just the first potentially-throwing
expression. In our dump curl_operation_ was already non-null and session_id_ == 1, proving
new HttpOperation(...) at line 227 had completed and the throw came from line 232 or later.
Unguarded operations in Session::SendRequest (ext/src/http/client/curl/http_client_curl.cc:141-242).
None of these is wrapped, and the function is noexcept:
| Operation | Location | Throws |
|---|---|---|
std::function constructed from the lambda capturing this + a shared_ptr |
http_client_curl.cc:233 |
std::bad_alloc — the capture holds a non-trivially-copyable shared_ptr, so libstdc++ cannot apply its small-object optimization and must heap-allocate |
async_data_.reset(new AsyncData()); AsyncData holds a std::promise and std::future |
http_operation_curl.cc:1286, type at http_operation_curl.h:331-340 |
std::bad_alloc; std::promise construction allocates shared state and can also throw std::system_error |
pending_to_add_session_ids_.insert(...) under std::lock_guard<std::recursive_mutex> |
http_client_curl.cc:635-645 |
std::bad_alloc from the node allocation; std::system_error from lock() |
background_thread_.reset(new std::thread(...)) |
http_client_curl.cc:438-446 |
std::system_error when pthread_create fails (EAGAIN at a thread or RLIMIT_NPROC limit); also std::bad_alloc |
There is no try or catch anywhere in ext/src/http/client/curl/http_client_curl.cc or
ext/src/http/client/curl/http_operation_curl.cc, on v1.22.0 or on main.
The same defect on the trace path. We have a second occurrence from an earlier version, ABI v1,
with an identical shape: BatchSpanProcessor::DoBackgroundWork → BatchSpanProcessor::Export →
OtlpHttpExporter::Export → OtlpHttpClient::Export → OtlpHttpClient::addSession →
Session::SendRequest → __clang_call_terminate → std::terminate. So this is not specific to
the metrics exporter; it is the shared curl HTTP client.
Suggested fix, and why it is not a breaking change. noexcept is part of the public
pure-virtual contract:
// ext/include/opentelemetry/ext/http/client/http_client.h:359
virtual void SendRequest(std::shared_ptr<EventHandler>) noexcept = 0;
So the fix should honour the noexcept by catching internally, not remove it. No signature
change, no ABI impact, no change required of any implementor of the interface.
The correct recovery already exists inside the function. Lines 234-241 handle SendAsync
returning non-OK by emitting SessionState::CreateFailed through the callback and clearing
is_session_active_. That is a complete cleanup rather than just a log: ResponseHandler::OnEvent
sets need_stop (exporters/otlp/src/otlp_http_client.cc:155-163), which at :310-317 calls
Unbind(kFailure) (:320-340), which calls OtlpHttpClient::ReleaseSession (:858-882), moving
the session out of running_sessions_ and calling session_waker_.notify_all() to release the
synchronous export waiting at :772-777. A catch handler doing exactly what that else branch
does would restore spec-compliant behaviour.
Two cautions for whoever implements it:
- Calling
OTEL_INTERNAL_LOG_ERRORfrom the handler is risky. It dispatches to a user-supplied
LogHandlerand allocates, so in abad_alloc-triggered handler it can throw again and return
straight toterminate. The same applies to thestd::stringstreambuilt in
ResponseHandler::OnEvent'sCreateFailedcase. - The handler must leave
curl_operation_andasync_data_in a state theSessiondestructor
andHttpClient::CleanupSessioncan handle, since the throw may land partway through
SendAsyncwith a live curl easy handle that was never added to the multi handle. This is
adjacent to #4397.
HttpClient::MaybeSpawnBackgroundThread (http_client_curl.cc:438-446) should also guard its own
thread creation and return false on failure, mirroring what was done for the file exporter in
#3012.
Other unguarded noexcept functions on the same path. Same defect class, worth fixing together:
OtlpHttpClient::Export(exporters/otlp/src/otlp_http_client.cc:748-787) — best single outer
net, since it has a naturalreturn ExportResult::kFailure. It is not a substitute for the
SendRequestfix, though: catching only here would strand a session inrunning_sessions_and
hangForceFlush(:789-812waits forrunning_sessions_.empty()).OtlpHttpClient::addSession(:1008-1029) — therunning_sessions_[session.get()]insert at
:1022allocates.HttpClient::CreateSession(ext/src/http/client/curl/http_client_curl.cc:323-341) —
std::make_shared<Session>at:329/:332andsessions_.insertat:337. This function
already carries a// FIXME: Session may leak if it does not call SendRequestat line 339.
Related:
- #4397 — an asynchronous curl request whose
Setupfails leaks its easy handle and header list.
Same function family, resource cleanup rather than thenoexceptescape, but a fix for
SendRequestshould account for it. - #3012 — "Ignore exception when create thread in OTLP file exporter", the precedent for this
change, applied toexporters/otlp/src/otlp_file_client.cc:1546-1551only. The curl client was
missed.
- 主要語言
- C++
- 星號
- 1.4k
- 分支
- 663
- 平均合併
- 1 天 10 小時
- 30 天內合併 PR
- 44
環境準備
在瀏覽器裡用你自己的 GitHub 帳號啟動這個專案的開發容器。
- 沒有 Dockerfile 或 Docker Compose 檔案
- 有 Pull Request 範本
- 閱讀貢獻指南
從這裡開始
- 先讀完整個 Issue,再讀專案的貢獻指南。
- 在 Issue 下留言說明你要接手 —— 這能避免兩個人做同樣的事。
- Fork 儲存庫,在一個分支上完成修改。
- 送出 Pull Request,並在描述裡引用這個 Issue 編號。
open-telemetry/opentelemetry-cpp 的其他 Issue
-
Baggage::ToHeader/FromHeader use form-style `+` for space, which other W3C Baggage implementations read as a literal plus可能已有人在做 關聯的 PR 仍在進行中或已合併。 未關閉bug triage/accepted
難度 2/5 1-3 小時 新手友好度 88/100
open-telemetry/opentelemetry-cpp#4745 · 1 則留言 ·
維護者通常 1 天內回覆
-
needs-triage
難度 2/5 1-3 小時 新手友好度 68/100
open-telemetry/opentelemetry-cpp#4697 · 1 則留言 ·
維護者通常 1 天內回覆
-
TracerContext construction might result in a terminate可能重新可做 @akshat-lakhera 於 37 天前認領,目前沒有進行中的 PR。 未關閉triage/accepted
難度 2/5 1-3 小時 新手友好度 62/100
open-telemetry/opentelemetry-cpp#4684 · 2 則留言 · 2 個 reaction ·
維護者通常 1 天內回覆
-
[CI] Add Ubuntu 26.04 runners to the CI workflow可能重新可做 關聯的 PR 已關閉且未合併。 未關閉triage/accepted
難度 2/5 1-3 小時 新手友好度 72/100
open-telemetry/opentelemetry-cpp#4596 · 2 則留言 · 1 個 reaction ·
維護者通常 1 天內回覆
-
[BUG] Resource::Create() throws bad_variant_access if process.executable.name isn't a string可能重新可做 關聯的 PR 已關閉且未合併。 未關閉bug help wanted Stale triage/accepted
難度 2/5 1-3 小時 新手友好度 84/100
open-telemetry/opentelemetry-cpp#4535 · 2 則留言 · 2 個 reaction ·
維護者通常 1 天內回覆
查看 open-telemetry/opentelemetry-cpp 的全部 Issue
相似的 Issue
-
Issue-Docs Needs-Triage
難度 2/5 1-3 小時 新手友好度 70/100
microsoft/winget-cli#6692 ·
維護者通常 2 天內回覆
-
難度 2/5 1-3 小時 新手友好度 75/100
stratum-mining/sv2-tp#167 ·
維護者通常 1 天內回覆
-
Sign up button opens the Sign in page instead of the Sign up page for Enterprise edition servers未關閉auth bug ui/ux
難度 2/5 1-3 小時 新手友好度 72/100
MerginMaps/mobile#4795 ·
維護者通常 2 天內回覆
-
難度 2/5 1-3 小時 新手友好度 68/100
HarperFast/rocksdb-js#1118 ·
維護者通常 1 天內回覆
-
bug triage
難度 2/5 1-3 小時 新手友好度 75/100
維護者通常 1 天內回覆