Skip to content

Resend C lookup requests unchanged after reconnecting - #19

Draft
stelfrag wants to merge 1 commit into
netdata:mainfrom
stelfrag:c-lookup-retry-request-buf
Draft

stelfrag wants to merge 1 commit into
netdata:mainfrom
stelfrag:c-lookup-retry-request-buf

Conversation

@stelfrag

@stelfrag stelfrag commented Nov 24, 2026 •

Copy link
Copy Markdown

Problem

The C APPS_LOOKUP and CGROUPS_LOOKUP clients encoded each request into send_buf and reused that pointer on retry. SHM framing overwrote the encoded request in place, so a retry could send framed bytes as the payload and get NIPC_ERR_BAD_LAYOUT. If reconnect moved send_buf, the retry could read freed memory. Both violate the resend contract in docs/level2-typed-api.md section 5.

A server restart can trigger this when a live client calls through its old session. Overflow recovery can also trigger it when even the PAYLOAD_EXCEEDED markers cannot fit within the server’s fixed response limit. Rust and Go already keep request and frame storage separate.

Change

On POSIX and Windows, C lookups encode into a reusable request_buf and copy the payload into send_buf for SHM framing. nipc_client_close frees both buffers. The lookup allocation tests use the appended *_CLIENT_REQUEST_BUF_REALLOC fault sites.

Compatibility

The wire format and C function signatures are unchanged. Two appended internal fields change the size of the public nipc_client_ctx_t struct, so C consumers must rebuild against the new header. A short SHM A/B for both lookup types at 16 and 256 items found throughput differences within run-to-run variation.

Testing

  • Five N12 cases cover SHM-to-SHM and baseline-to-SHM restarts for both lookups, plus an apps lookup against a fixed 1 KiB response limit. Each restart verifies one reconnect and delivery of the original items. The limit case verifies two reconnects, two deliveries of the same 16 PIDs, and a final NIPC_ERR_OVERFLOW.
  • Against 1ff116f, all five cases failed in the recorded plain Linux runs. Linux ASan caught a heap-use-after-free on baseline-to-SHM reconnect. On Windows, the SHM-to-SHM and limit cases failed consistently, while baseline-to-SHM failures depended on whether realloc moved the buffer. All five pass with the fix on Linux, Linux under ASan, and Windows.
  • Linux CTest passed 31/31 before the final N12 fixture updates, and all six Windows MSYS service suites passed with those updates. test_uds was excluded because it also fails to link on the base commit on 64-bit glibc 2.41 (SOW-0074).
  • tests/run-sanitizer-asan.sh now includes test_robustness. The seven buildable Linux binaries ran individually with ASan, UBSan, and leak detection and produced no reports. Validation covered Linux and Windows.

Summary by cubic

Fixes C APPS_LOOKUP and CGROUPS_LOOKUP retries resending overwritten or freed request bytes after a reconnect.

The lookups encoded each request into send_buf; SHM framing rewrote it in place, so an in-call retry resent the framed bytes as the payload (server replied NIPC_ERR_BAD_LAYOUT), and a reconnect that resized send_buf left the retry reading through a freed pointer. Lookups now encode into a new request_buf and copy into send_buf for framing, so the retry resends the exact request, matching the Rust and Go clients. This is hit by a server restart under a live client or by a response that exceeds the server's fixed response limit.

  • nipc_client_ctx_t gains two internal fields, so C consumers must rebuild against the new header; wire format and function signatures are unchanged.
  • Added N12 POSIX and Windows robustness tests covering SHM→SHM and baseline→SHM restarts plus the fixed-response-limit overflow path; test_robustness now runs in the ASAN suite.

Written for commit 74c870a. Summary will update on new commits.

View guided diff

Summary by CodeRabbit

  • Bug Fixes
    • Lookup requests are now resent unchanged when a connection drops and reconnects, including when the connection switches transport types.
    • Requests remain intact when the client reconnects and resizes its communication buffer.
    • Lookups still report an overflow error when the server’s response exceeds its configured limit.
  • Tests
    • Added coverage for lookup retries, server restarts, transport changes, and response-limit overflow on Linux, FreeBSD, and Windows.

The C apps and cgroups lookup clients encoded each request into
send_buf, which SHM framing rewrites in place and a reconnect may
resize. A lookup that reconnected inside the call (a server restart,
or a response over the server's limit) resent the framed bytes, so the
server rejected it, or read the request through a pointer the
reconnect had freed.

Encode lookup requests into a separate request_buf, as the Rust and Go
clients already do; send_buf only holds the SHM frame. Add N12
robustness tests on POSIX and Windows and run test_robustness under
ASAN. SOW-0073; SOW-0074 tracks a test_uds link failure on 64-bit
glibc 2.41 found during validation.
@CLAassistant

CLAassistant commented Nov 24, 2026 •

Copy link
Copy Markdown

CLA assistant check
All committers have signed the CLA.

@coderabbitai

coderabbitai Bot commented Nov 24, 2026 •

Copy link
Copy Markdown

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

Lookup clients now encode apps and cgroups requests into a separate buffer for retries. POSIX and Windows robustness tests cover retries after server restarts and response-limit overflows. The PR also adds a completed SOW for the buffer change and a pending SOW for a separate test_uds link failure.

Changes: Lookup retry request buffer

Layer / File(s) Summary
Request buffer contract and allocation
src/libnetdata/netipc/include/netipc/netipc_service.h, src/libnetdata/netipc/src/service/netipc_service_platform.h, src/libnetdata/netipc/src/service/netipc_service.c, src/libnetdata/netipc/src/service/netipc_service_win.c, src/libnetdata/netipc/src/service/*_internal.h
nipc_client_ctx_t adds request-buffer state. POSIX and Windows helpers ensure its capacity and use request-buffer allocation fault sites.
Lookup encoding and buffer lifecycle
src/libnetdata/netipc/src/service/netipc_service_apps_lookup.c, src/libnetdata/netipc/src/service/netipc_service_cgroups_lookup.c, src/libnetdata/netipc/src/service/netipc_service_common.c
Apps and cgroups lookups encode and send requests from request_buf. Client cleanup frees that buffer. SHM preparation copies the external request payload into the send frame.
Retry behavior and platform validation
tests/fixtures/c/test_robustness.c, tests/fixtures/c/test_win_robustness.c, tests/fixtures/c/test_service.c, tests/fixtures/c/test_win_service_extra.c, tests/run-sanitizer-asan.sh, .agents/sow/done/SOW-0073-...md
POSIX and Windows tests check request items across server restarts and response-limit overflows. Fault-injection tests target request-buffer allocation, and the sanitizer run includes test_robustness. The completed SOW records implementation and validation details.

UDS link-failure tracking

Layer / File(s) Summary
Pending test_uds link-failure plan
.agents/sow/pending/SOW-0074-...md
The pending SOW records a glibc 2.41 test_uds link failure, a proposed guard adjustment, and target-platform acceptance criteria. Implementation and validation remain pending.

Estimated code review effort: 3 (Moderate) | ~25 minutes

Change: Bug fix

Suggested reviewers: ktsaou

Merge Risk: Minimal · up to 74c87

The change makes C lookup retries resend the original request bytes after a reconnect, and no concrete defect was found in it. The separate test_uds link failure on 64-bit glibc 2.41 is pre-existing and tracked separately.

Pre-merge checks: Passed 4

Check name Status Explanation
Description Check Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check Passed The title "Resend C lookup requests unchanged after reconnecting" matches the main change. The PR separates request encoding into a reusable request_buf so that retries after reconnect resend the orig…
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
Full details: Title check

Explanation

The title "Resend C lookup requests unchanged after reconnecting" matches the main change. The PR separates request encoding into a reusable request_buf so that retries after reconnect resend the original encoded request instead of framed or overwritten bytes. The raw summary and objectives both describe this fix for the C APPS_LOOKUP and CGROUPS_LOOKUP clients, and the new N12 tests check the same behavior. The title is specific, concise, and does not use vague terms.

Finishing Touches

Generate unit tests (beta)

  • Create a new PR
Fix failing CI checks

  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

Recent review info

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: defaults
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 4ef6d4a4-9cff-4f55-a7ea-2657b3463a44
Commits

Reviewing files that changed from the base of the PR and between 1ff116f and 74c870a.

Files selected for processing: 16

  • .agents/sow/done/SOW-0073-20261123-c-lookup-retry-request-buffer.md
  • .agents/sow/pending/SOW-0074-20261124-uds-eintr-wrap-glibc-time64.md
  • src/libnetdata/netipc/include/netipc/netipc_service.h
  • src/libnetdata/netipc/src/service/netipc_service.c
  • src/libnetdata/netipc/src/service/netipc_service_apps_lookup.c
  • src/libnetdata/netipc/src/service/netipc_service_cgroups_lookup.c
  • src/libnetdata/netipc/src/service/netipc_service_common.c
  • src/libnetdata/netipc/src/service/netipc_service_platform.h
  • src/libnetdata/netipc/src/service/netipc_service_posix_internal.h
  • src/libnetdata/netipc/src/service/netipc_service_win.c
  • src/libnetdata/netipc/src/service/netipc_service_win_internal.h
  • tests/fixtures/c/test_robustness.c
  • tests/fixtures/c/test_service.c
  • tests/fixtures/c/test_win_robustness.c
  • tests/fixtures/c/test_win_service_extra.c
  • tests/run-sanitizer-asan.sh

Included review availability: This review used your included allowance. Your plan provides up to 1 included review per hour; 0 remain after this review.

@codacy-production

Copy link
Copy Markdown

Up to standards ✅

🟢 Issues 0 issues

Results:
0 new issues

View in Codacy

🟢 Metrics 0 complexity · 0 duplication

Metric Results
Complexity 0
Duplication 0

View in Codacy

NEW Get contextual insights on your PRs based on Codacy's metrics, along with PR and Jira context, without leaving GitHub. Enable AI reviewer
TIP This summary will be updated as you push new changes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants