Skip to content

fix(ext): enable TCP keepalive for curl HTTP client connections - #4266

Closed
Babul422 wants to merge 3 commits into
open-telemetry:mainfrom
Babul422:fix/http-exporter-tcp-keepalive
Closed

Babul422 wants to merge 3 commits into
open-telemetry:mainfrom
Babul422:fix/http-exporter-tcp-keepalive

Conversation

@Babul422

@Babul422 Babul422 commented Jul 17, 2026 •

Copy link
Copy Markdown

Fixes #3333

Description

This PR fixes #3333 by enabling TCP keepalive on libcurl HTTP client connections.

Root Cause

When a remote endpoint silently disappears without closing the TCP connection, the client can remain connected to a half-open socket for an extended period. Since TCP keepalive is currently disabled, the exporter may remain blocked waiting on the connection.

Solution

Enable CURLOPT_TCP_KEEPALIVE during HttpOperation::Setup(). This allows libcurl and the underlying operating system to detect stale TCP connections using TCP keepalive mechanisms.

Testing

  • Successfully built the project.
  • curl_http_test passes.
  • otlp_http_exporter_test passes.

@Babul422
Babul422 requested a review from a team as a code owner July 17, 2026 14:31

@marcalff marcalff left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, thanks for the fix.

@codecov

codecov Bot commented Jul 17, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 98.86364% with 1 line in your changes missing coverage. Please review.
✅ Project coverage is 82.66%. Comparing base (57253cd) to head (e2ad5ab).
⚠️ Report is 60 commits behind head on main.

Files with missing lines Patch % Lines
ext/src/http/client/curl/http_operation_curl.cc 66.67% 1 Missing ⚠️
Additional details and impacted files

Impacted file tree graph

@@            Coverage Diff             @@
##             main    #4266      +/-   ##
==========================================
+ Coverage   82.61%   82.66%   +0.05%     
==========================================
  Files         511      511              
  Lines       20132    20192      +60     
==========================================
+ Hits        16631    16690      +59     
- Misses       3501     3502       +1     
Files with missing lines Coverage Δ
...de/opentelemetry/exporters/otlp/otlp_http_client.h 100.00% <100.00%> (ø)
...emetry/exporters/otlp/otlp_http_exporter_options.h 100.00% <ø> (ø)
...rters/otlp/otlp_http_log_record_exporter_options.h 100.00% <ø> (ø)
...exporters/otlp/otlp_http_metric_exporter_options.h 100.00% <ø> (ø)
exporters/otlp/src/otlp_http_exporter.cc 99.21% <100.00%> (+0.06%) ⬆️
exporters/otlp/src/otlp_http_exporter_options.cc 96.16% <100.00%> (+0.51%) ⬆️
...xporters/otlp/src/otlp_http_log_record_exporter.cc 98.42% <100.00%> (+0.10%) ⬆️
.../otlp/src/otlp_http_log_record_exporter_options.cc 96.16% <100.00%> (+0.51%) ⬆️
exporters/otlp/src/otlp_http_metric_exporter.cc 98.47% <100.00%> (+0.10%) ⬆️
...ters/otlp/src/otlp_http_metric_exporter_options.cc 96.16% <100.00%> (+0.51%) ⬆️
... and 1 more
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

return rc;
}

rc = SetCurlLongOption(CURLOPT_TCP_KEEPALIVE, 1L);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it be possible to expose this as a configuration option for HttpClient, OtlpHttpClient, and all HTTP-based exporters? That way users can opt-in rather than having it unconditionally enabled.
Also, I noticed CURLOPT_TCP_KEEPIDLE and CURLOPT_TCP_KEEPINTVL are being set without CURLOPT_TCP_KEEPALIVE. These two options are normally only effective when keep-alive is enabled, so we should probably set all three together.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @owent,

Thanks for the review.
My understanding is that the preferred direction is to make TCP keepalive opt-in (disabled by default) and expose it through HttpClient, OtlpHttpClient, and the HTTP-based exporters.

I was planning to expose a single boolean configuration (for CURLOPT_TCP_KEEPALIVE) while enabling the related libcurl behavior, rather than exposing additional public configuration for CURLOPT_TCP_KEEPIDLE and CURLOPT_TCP_KEEPINTVL unless you think those should also be configurable.

Does that match what you had in mind?

Thanks!

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that will be helpful to let CURLOPT_TCP_KEEPIDLE and CURLOPT_TCP_KEEPINTVL also be configurable.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1. I think these should be configurable together. If we only enable CURLOPT_TCP_KEEPALIVE, the timing still depends on libcurl/OS defaults, so it may not help users who need faster detection for half-open connections like in #3333.

@lalitb lalitb left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As discussed here - The keepalive option and timing knobs should be configurable together, otherwise this may not solve #3333 for users who need faster half-open detection.

…rs (#4266)

- Make TCP keepalive opt-in ( default )
- Add  and  options
- Propagate options through Request, OtlpHttpClient, and all OTLP HTTP exporters
- Add unit tests for keepalive option propagation and curl request settings
@Babul422
Babul422 force-pushed the fix/http-exporter-tcp-keepalive branch from af1e7f7 to 765872f Compare August 19, 2026 04:52
@lalitb

lalitb commented Aug 19, 2026 •

Copy link
Copy Markdown
Member

Thanks for working on this. Enabling TCP keepalive can be useful, but I don’t think we can say yet that it fixes #3333.

TCP keepalive helps when a connection was established and the other side disappears without closing it. It does not help a connection still being created, such as the SYN_SENT connection shown in the issue. We also already set a timeout for the complete HTTP request, so we should first understand why a five-second timeout can still lead to a much longer wait.

I’m also unsure about adding this setting to the common Request interface. Keepalive controls the underlying TCP connection, which may be reused for many requests. It is therefore better treated as a curl/client setting than as a property of one HTTP request. Changing the common virtual interface may also affect external custom HTTP clients.

I suggest keeping this PR in draft for now. Let’s first add a small test that reproduces the long wait, identify whether the problem is in curl timeout handling or our session/shutdown logic, and discuss the right place for transport settings as part of #4448.

@dbarker dbarker added the pr:fix-merge-conflicts Please fix merge conflicts for this pr label Sep 11, 2026
@Babul422 Babul422 closed this by deleting the head repository Oct 22, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

pr:fix-merge-conflicts Please fix merge conflicts for this pr

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Crash in http exporter, for tcp half open connections

5 participants