fix: prevent double-free of controlChannelRequest on connection timeout#342
Open
bdasnevesKP wants to merge 1 commit intoYlianst:masterfrom
Open
fix: prevent double-free of controlChannelRequest on connection timeout#342bdasnevesKP wants to merge 1 commit intoYlianst:masterfrom
bdasnevesKP wants to merge 1 commit intoYlianst:masterfrom
Conversation
When MeshServer_ConnectEx_NetworkError fires (20s connection timeout), it frees j (==agent->controlChannelRequest) but does not set the pointer to NULL. ILibWebClient_CancelRequest, called immediately after, executes synchronously on the chain thread (ILibChain_RunOnMicrostackThread macro calls the handler inline when already on the chain thread). This triggers MeshServer_OnResponse(ReceiveStatus_Complete) which sees a non-NULL controlChannelRequest and calls ILibMemory_Free on the already-freed pointer, causing a double-free and heap corruption on every retry cycle. Fix: set agent->controlChannelRequest = NULL right after ILibMemory_Free(j) so MeshServer_OnResponse safely skips the cleanup block. Relates to: Ylianst#110, Ylianst#151, Ylianst#281 Relates to: Ylianst/MeshCentral#7407 Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Collaborator
|
this doesnt appear to fix the issue 😢 |
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.
Background
I'm not a C developer. I came across issue Ylianst/MeshCentral#7407 and the related issues #110, #151, and #281 about the agent leaking memory during connection retries. I used GitHub Copilot to help me trace through the code and identify the root cause.
Root Cause
In \meshcore/agentcore.c, \MeshServer_ConnectEx_NetworkError\ frees the \j\ pointer (which is the same as \agent->controlChannelRequest) but does not set \agent->controlChannelRequest = NULL\ afterward.
The issue is that \ILibWebClient_CancelRequest\ — called immediately after — executes synchronously on the chain thread (see the \ILibChain_RunOnMicrostackThread\ macro in \ILibParsers.h):
This synchronous cancel internally calls \MeshServer_OnResponse\ with \ReceiveStatus_Complete. That function checks \agent->controlChannelRequest != NULL\ and calls \ILibMemory_Free(agent->controlChannelRequest)\ — on a pointer that was already freed moments earlier. This is a double-free, which corrupts the heap allocator's internal state and causes memory usage to grow continuously with every retry cycle (~every 20 seconds when the server is unreachable).
The Fix
One line added in \MeshServer_ConnectEx_NetworkError, right after \ILibMemory_Free(j):
This ensures \MeshServer_OnResponse\ safely skips the cleanup block (it checks for != NULL\ first), preventing the double-free.
Caveats