fix(usm): key ssl_sock_by_ctx by process to stop leaking entries in multi-threaded TLS clients #59740
All Vouch review confidence checks passed
Residual risk 0.30 = Change Risk (0.75) × (1 − Review Confidence (0.60)).
Review depth and reviewer attention state are adequate.
Model 1 · Change Risk (XGBoost)
Score: 0.75 · 650 lines diff across 8 files
Model 2 · Review Depth (DistilBERT)
Depth Score: 0.17 · 1 substantive comment(s)
Model 3 · Review Timing & Attention
Session adequacy: 100% · Reviewer familiarity: 35%
Bedrock Claude 3 · Risk Narration
(disconnected)
Groq Risk Narration
AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
Model 1 · Change Risk Attribution
SHAP Weights
Diff Size
diff_size
0.388
Shallow Review Depth
shallow_review_depth
0.289
Sensitive Paths
sensitive_paths
0.075
Real PR Conversation Timeline
1 comments
"Review active on #59740"
general
weight: 0.35
brycekahle
What does this PR do?
Builds every
ssl_sock_by_ctxkey through one helper,ssl_sock_by_ctx_key(), which clears the thread ID bits ofpid_tgidand keeps the TGID in the upper 32 bits. All threads of a process now share one entry per SSL context.ssl_ctx_by_pid_tgidis unchanged and stays per thread, because it is the per-thread handoff betweenSSL_read/SSL_writeandtcp_sendmsg.There are no Go changes. The key struct and its Go mirror are unchanged, and userspace keeps reading the TGID with
key.Tgid >> 32.Motivation
#43379 (7.74.0) added
pid_tgidto the key to stop SSL context pointers from colliding across processes. The key stores the rawbpf_get_current_pid_tgid()value, so it also includes the thread ID.In a client where several threads use the same TLS connection:
tup_from_ssl_ctx. It goes through thetcp_sendmsgfallback and inserts a new{tid, ctx}entry, often with the tuple of another socket.SSL_shutdown,gnutls_goodbyeandtcp_closeonly delete the entry of the thread that closes the connection.As a result, entries pile up until the process exits. In production this filled
ssl_sock_by_ctx(max_tracked_connections, 65,536 by default) within hours on nodes running a long-lived multi-threaded OpenSSL client. After that USM stopped seeing new native TLS connections. Restarting system-probe only helps until the map fills again.The same mechanism also hurts accuracy before the map is full. Requests issued from a thread that resolves the connection through the fallback get attributed to the wrong connection tuple.
Nothing in #43379 or USMON-1491 relies on per-thread keys. The PR describes the problem as multi-process, and its userspace cleaner already treats the field as a TGID. A per-process key keeps that isolation.
Escalation: APMS-20835.
Describe how you validated your changes
New test.
TestSSLSockByCtxMultiThreadedLeak(pkg/network/usm/ebpf_ssl_test.go) runs a helper (testdata/ssl_threadpool_test) through docker-compose, following the pattern ofTestFdBySSLBioMapLeak.SSL_shutdown, and stays alive.ssl_sock_by_ctxentry is left for the helper's PID, and that every captured request sits on the connection whose client port is in its path.main:| Objects | Entries left | Requests captured on the right / wrong connection |
|---|---|---|
| this branch | 0 | 8,000 / 0, PASS |
| main | 741 | 63 / 7,003, FAIL |
End-to-end with system-probe. I rebuilt the CO-RE
usm.oand swapped it into the 7.83.1 agent image, forcing CO-RE and disabling all fallbacks. The four eBPF headers are byte-identical between 7.83.1 andmain, with and without this patch./debug/http_monitoring, using the client port encoded in each request path.| Scenario | 7.73.0 (stock) | 7.83.1 | 7.83.1 + this PR |
|---|---|---|---|
| Entries while 20 connections are open (8 threads) | 20 | 180 | 20 |
| Thread pool, 8 threads: leftover / right connection | 0 / 100% | 234 / 0.1% | 0 / 100% |
| Thread pool, 32 threads | 0 / 100% | 1,085 / 0.3% | 0 / 100% |
| One short-lived thread per request | 0 / 100% | 0 / 0% (nothing captured) | 0 / 100% |
| Close without
SSL_shutdown| 0 / 98.4% | 247 / 0.1% | 0 / 98.3% || 4 forked workers, same SSL addresses (#43379's case) | 0 / 12.9–20.5% | 60–246 / 0.1–1.2% | 0 / 100% |
| GnuTLS thread pool | 0 / 100% | 160 / 0.1% | 0 / 100% |
| Soak: 32 threads, 640k requests, 5k rotations | 0 / 100% | 2,339 / 0.2% | 0 / 100% |
| After killing all clients | 0 | 306 | 0 |
Other checks:
-Werror.go vetandgofmtare clean.- in
pkg/network/usm:TestSSLMapsCleaner,TestSSLMapsCleanup,TestFdBySSLBioMapLeakandTestTLSSuite(OpenSSL versions, curl, wget, musl, the OpenSSL container, Node.js);- in
pkg/network/usm/tests:TestUSMSuite(218 subtests) andTestFullMonitorWithTracer.Everything passes in CO-RE mode, and the only difference between the two runs is the new test.
- every
runtime_compiledsubtest (no kernel headers in my VM);-
TestNodeJSTLSWithLibnode(its image needs internal registry access).Additional Notes
- A connection set up through
SSL_set_fdand closed withoutSSL_shutdownkeeps its entry until the SSL pointer is reused or the process exits.- The
tcp_sendmsgfallback is still a best-effort guess.tup_from_ssl_ctx(bpf_memcpyinto the map value) can in theory run from two CPUs for the same SSL. OpenSSL already requires callers to serialise access to an SSL object, and this is the same sharing model as before 7.74.0 (key = ctx only).pid: hostso its PID matches the TGID in the key. The new compose file is picked up by_test_docker_image_list, and it uses the same image asbio_leak_test.team/ebpf-platform(orteam/universal-service-monitoring),component/system-probe,qa/done, and backport to the active 7.8x branch.🤖 Generated with Claude Code