Resend C lookup requests unchanged after reconnecting

Draft
ST
8d ago
+1086-26

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.

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.