Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix memory leak when a JSON of model fails to parse #47

Merged

Conversation

ityuhui
Copy link
Member

@ityuhui ityuhui commented Jan 11, 2021

The valgrind test reports a memory leak for the example delete_pod

valgrind --tool=memcheck --leak-check=full ./delete_pod_bin
==32029== Memcheck, a memory error detector
==32029== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.
==32029== Using Valgrind-3.13.0 and LibVEX; rerun with -h for copyright info
==32029== Command: ./delete_pod_bin
==32029==
OK
The return code of HTTP request=200
The pod is deleted successfully.
==32029==
==32029== HEAP SUMMARY:
==32029==     in use at exit: 82 bytes in 3 blocks
==32029==   total heap usage: 8,728 allocs, 8,725 frees, 1,057,554 bytes allocated
==32029==
==32029== 82 (32 direct, 50 indirect) bytes in 1 blocks are definitely lost in loss record 3 of 3
==32029==    at 0x4C31B0F: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==32029==    by 0x4EEBFA1: v1_list_meta_create (v1_list_meta.c:14)
==32029==    by 0x4EEC2F2: v1_list_meta_parseFromJSON (v1_list_meta.c:130)
==32029==    by 0x4F1D1B2: v1_status_parseFromJSON (v1_status.c:206)
==32029==    by 0x5044D9C: CoreV1API_deleteNamespacedPod (CoreV1API.c:14789)
==32029==    by 0x108B45: delete_a_pod (main.c:10)
==32029==    by 0x108C94: main (main.c:53)
==32029==
==32029== LEAK SUMMARY:
==32029==    definitely lost: 32 bytes in 1 blocks
==32029==    indirectly lost: 50 bytes in 2 blocks
==32029==      possibly lost: 0 bytes in 0 blocks
==32029==    still reachable: 0 bytes in 0 blocks
==32029==         suppressed: 0 bytes in 0 blocks
==32029==
==32029== For counts of detected and suppressed errors, rerun with: -v
==32029== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 0 from 0)

The reason is:
Deleting a pod successfully will return a v1_pod_t other than a v1_status_t from Kubernetes API server, but the generated client code still wants to parse the JSON string to create a v1_status_t in the function v1_status_parseFromJSON, the detail can refer to #31 (comment)

v1_status_t *v1_status_parseFromJSON(cJSON *v1_statusJSON) will return NULL because the parameter v1_statusJSON is not a valid v1_status_t JSON, but metadata_local_nonprim (member of v1_status_t) is created and its memory is not released before the function returns, this will cause memory leak.

I fixed the issue by this PR in the openapi-generator repo, now re-generate the C client to merge the fix.

After this fix, valgrind does not reports memory leak:

==28375== All heap blocks were freed -- no leaks are possible

@k8s-ci-robot k8s-ci-robot added cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. approved Indicates a PR has been approved by an approver from all required OWNERS files. size/XXL Denotes a PR that changes 1000+ lines, ignoring generated files. labels Jan 11, 2021
@ityuhui
Copy link
Member Author

ityuhui commented Jan 11, 2021

/cc @brendandburns

@ityuhui
Copy link
Member Author

ityuhui commented Jan 19, 2021

Hi @brendandburns

Could you please review this PR if you have time ?

@brendandburns
Copy link
Contributor

/lgtm
/approve

(many apologies for the delay!)

@k8s-ci-robot k8s-ci-robot added the lgtm "Looks good to me", indicates that a PR is ready to be merged. label Jan 27, 2021
@k8s-ci-robot
Copy link
Contributor

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: brendandburns, ityuhui

The full list of commands accepted by this bot can be found here.

The pull request process is described here

Needs approval from an approver in each of these files:
  • OWNERS [brendandburns,ityuhui]

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@k8s-ci-robot k8s-ci-robot merged commit f9bdecd into kubernetes-client:master Jan 27, 2021
@ityuhui
Copy link
Member Author

ityuhui commented Jan 27, 2021

It doesn’t matter. Thank you @brendandburns

@ityuhui ityuhui deleted the yh-fix-memleak-on-deleting-pod branch January 27, 2021 05:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
approved Indicates a PR has been approved by an approver from all required OWNERS files. cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. lgtm "Looks good to me", indicates that a PR is ready to be merged. size/XXL Denotes a PR that changes 1000+ lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants