Skip to content

Fix server CallData leak by not pinning lifetime on AsyncNotifyWhenDone - #526

Open
twaltersp wants to merge 2 commits into
ni:masterfrom
twaltersp:fix/server-calldata-lifetime
Open

Fix server CallData leak by not pinning lifetime on AsyncNotifyWhenDone#526
twaltersp wants to merge 2 commits into
ni:masterfrom
twaltersp:fix/server-calldata-lifetime

Conversation

@twaltersp

@twaltersp twaltersp commented Aug 28, 2026

Copy link
Copy Markdown

Description:

Purpose: Fix a server-side memory leak on NI Linux RT (#525). A connected client making unary RPCs in a polling loop leaked memory on every call, independent of payload. After Complete Call, CallData stayed alive because it was pinned on the AsyncNotifyWhenDone completion-queue tag, and that tag was not being delivered for AsyncGenericService.

Changes Made: Stop registering AsyncNotifyWhenDone and remove CallFinishedTag. CallData is now kept alive by the existing CompletionQueueTag / pointer manager and is freed when stream.Finish() completes on the CQ, matching the official gRPC async server examples. IsCancelled() no longer calls ServerContext::IsCancelled() (unsafe without a completed notify tag) and returns false; client cancel is observed as a failed Read/Write. IsActive() no longer depends on IsCancelled().

Related Issues: #525

Testing: Built liblabview_grpc_server.so and ran it on a Linux RT target. With a connected client polling unary RPCs, memory was observed for two hours and did not grow. The same test showed the leak immediately with the previous .so. Reviewers should focus on CallData lifetime in src/event_data.cc after Complete Call, and on any LabVIEW code that calls the IsCancelled export during an RPC.

A memory leak was also found in the client call. A memory leak in client will occur if an error occurs during the call. This causes CompleteClientUnaryCall to not be executed. Fix applied in Client Unary Call and Client Complete Client Streaming Call. Similar issue can occur in server if error is wired to the Response. Template updated to handle this case by removing case structure.

PR update
Restored a distinct cancel error so a client timeout or cancel is not reported as generic stream failure -2.

Client cancel, deadline, or disconnect is detected as a failed Read/Write (ok=false) and recorded in a local _cancelled flag. IsCancelled() returns that flag instead of calling ServerContext::IsCancelled() (unsafe without a completed notify tag). Get Request / Set Response then return -1001 (CANCELLED). SetCallStatus(INVALID_ARGUMENT) is still -1003. IsActive() treats a cancelled call as inactive.

Signed-off-by: DOME_WIN1\toddw <todd.walter@gmail.com>

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

Fixes a server-side memory leak on NI Linux RT by removing the AsyncNotifyWhenDone-based lifetime pinning for server CallData, and instead relying on existing completion-queue tags to govern CallData lifetime through stream.Finish() completion (aligning with common gRPC async server patterns).

Changes:

  • Removed AsyncNotifyWhenDone registration and the associated CallFinishedTag path that could retain CallData indefinitely.
  • Simplified CallData completion handling so CallData lifetime is tied to Finish completion on the server CQ.
  • Adjusted cancellation/active semantics: IsCancelled() no longer queries ServerContext::IsCancelled(), and IsActive() no longer depends on IsCancelled().

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.

File Description
src/grpc_server.h Removes CallFinishedTag and related members/methods used to pin CallData via AsyncNotifyWhenDone.
src/event_data.cc Stops registering AsyncNotifyWhenDone, removes completion handling tied to that tag, and updates IsCancelled/IsActive behavior.

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment thread src/event_data.cc
Comment thread src/event_data.cc
Comment on lines +19 to +23
// Do not call AsyncNotifyWhenDone. Holding CallData on that CQ tag
// leaked one object per RPC because the tag is not reliably delivered
// for AsyncGenericService. Free CallData when Finish completes on the
// CQ instead (gRPC async example pattern). IsCancelled() therefore
// cannot use ServerContext::IsCancelled(); client cancel is a failed
… to not be executed. Fix applied in Client Unary Call and Client Complete Client Streaming Call. Similar issue can occur in server if error is wired to the Response. Template updated to handle this case by removing case structure.
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.

3 participants