Fix server CallData leak by not pinning lifetime on AsyncNotifyWhenDone - #526
Open
twaltersp wants to merge 2 commits into
Open
Fix server CallData leak by not pinning lifetime on AsyncNotifyWhenDone#526twaltersp wants to merge 2 commits into
twaltersp wants to merge 2 commits into
Conversation
Signed-off-by: DOME_WIN1\toddw <todd.walter@gmail.com>
Contributor
There was a problem hiding this comment.
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 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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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.