Repository navigation
Fix memory leak in sd_lldp_stop() - #11163
filbranden wants to merge 2 commits into
Conversation
There was a problem hiding this comment.
Hmm, should this be sd_lldp_neighbor_unref()? Otherwise, if sd_lldp_neighbor objects are referenced and sd_lldp is freed, then sd_lldp_neighbor objects are also freed even if they are referenced.
There was a problem hiding this comment.
So I added assert(n->n_ref <= 0); here, in the assumption that sd_lldp_stop() will only be called once all references to all sd_lldp_neighbors will have been unref'd already...
(Is that really a valid assumption? The assert() will validate it, but potentially with a crash...)
(Calling sd_lldp_neighbor_unref() doesn't work, since typically n_ref is zero here, so the ref/unref logic is meant for all references outside the one from the LLDP itself.)
Not really sure what you mean by your other comment ("reference counter of sd_lldp_neighbor objects seems to be borked, ...), can you please expand on that?
There was a problem hiding this comment.
Hmm? Sorry, I may misunderstand your point or code, but I guess with your PR the following code may not work, as the ref counters of neighbors are not 1 or 0.
n = sd_lldp_get_neighbors(lldp, &neighbors);
(void) sd_lldp_stop(lldp);
for (i = 0; i < n; i++)
sd_lldp_neighbor_unref(neighbors[i]);
free(neighbors);
|
BTW, reference counter of |
|
I'll have to rebase this one... but I'll wait until #11172 is in first, then we have time to have further discussion on this one... Thanks! |
The root cause of the issue was calling hashmap_remove() from function lldp_neighbor_unlink(), which was registered as destructor of the Hashmap and would get called from hashmap_clear(). But this would potentially shuffle the elements in the hashmap, therefore potentially missing destruction of some of the elements. Fix this by using a separate lldp_neighbor_destroy() function that cleans up an sd_lldp_neighbor entry but does not remove it from the hashmap and use it exclusively as a destructor for the Hashmap. Tested: Ran test case that exposed this (test_multiple_neighbors_sorted) under Valgrind and confirmed memory leaks were no longer reported.
…ghbors In particular, check that the order of the results is consistent. This test coverage will be useful in order to refactor the compare_func used while sorting the results. When introduced, this test also uncovered a memory leak in sd_lldp_stop(), which was then fixed by a separate commit using a specialized function as destructor of the LLDP Hashmap. Tested: $ ninja -C build/ test $ valgrind --leak-check=full build/test-lldp
d1240d8 to
539470c
Compare
|
Hmm, defining a special cleanup function just to be called from hashmap_clear is pretty ugly. I'd prefer to have a scheme where we just call the normal unref function but in a safe way. What about changing hashmap_clear to use Using the "safe" public API will be slower then using the internal API. It would be great to quantify this. My hope is that the cost is not significant enough to matter. |
|
I was actually thinking in the other direction... Having Either approach is fine with me, let me know which you prefer and I can try to come up with a PR for it. Cheers, |
|
Such a check could be useful, but I think it's orthogonal to the issue at hand: if it is implemented, it'd apply equally well to either of the solutions (the one as implemented in the patch and my alternative proposal). |
|
Yeah, I too would say we should make hashmap_clear() safe to the destroy callback unregistering from the same hashmap. That sounds like a generally useful change to me, as this is likely to explode elsewhere too. |
| static void lldp_neighbor_destroy(sd_lldp_neighbor *n) { | ||
| assert(n); | ||
| assert(n->lldp); | ||
| assert(n->n_ref <= 0); |
There was a problem hiding this comment.
this is assumption is wrong. it should be OK for callers to keep a ref on a neighbor object, and it should stay around then, though in unlinked state
|
I prepped an alternative to this in #11191, that incorporates @filbranden's test case. ptal! |
|
Let's go with #11191 instead... I'll take a look at it shortly. |
Fix memory leak in
sd_lldp_stop()The root cause of the issue was calling
hashmap_remove()from functionlldp_neighbor_unlink(), which was registered as destructor of the Hashmap and would get called fromhashmap_clear(). But this would potentially shuffle the elements in the hashmap, therefore potentially missing destruction of some of the elements.Fix this by using a separate
lldp_neighbor_destroy()function that cleans up ansd_lldp_neighborentry but does not remove it from the hashmap and use it exclusively as a destructor for the Hashmap.Tested:
valgrind --leak-check=full build/test-lldp(including test casetest_multiple_neighbors_sorted(), which uncovered this memory leak and would have shown the problem if it was not fixed.)ninja -C build/ test, all tests passed.