🐛 (API): Fix file descriptor leak from defer inside loop in external plugin handler#5688
🐛 (API): Fix file descriptor leak from defer inside loop in external plugin handler#5688SebTardif wants to merge 1 commit into
Conversation
Signed-off-by: Sebastien Tardif <sebtardif@ncf.ca>
|
Hi @SebTardif. Thanks for your PR. I'm waiting for a kubernetes-sigs member to verify that this patch is reasonable to test. If it is, they should reply with Regular contributors should join the org to skip this step. Once the patch is verified, the new status will be reflected by the I understand the commands that are listed here. DetailsInstructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes-sigs/prow repository. |
|
Thanks for the improvement. I think the code change is directionally correct, but the PR description should be adjusted. The defer f.Close() inside the loop does keep all file descriptors open until handlePluginResponse returns, so the FD-retention issue is real and this patch addresses it. However, I do not think this part is accurate:
If we take a look at the assembly. In the deferred version, the loop body registers a deferred closure instead of calling Close directly: The actual close happens in the deferred closure: And deferred calls are run from function return paths via: By contrast, the explicit-close version has Close directly in the loop body: So I would suggest rewording the problem statement 😺 |
|
Thank you for the detailed explanation and the assembly analysis! You're absolutely right - I incorrectly claimed "only the last file is actually closed." Each deferred closure does capture its own file correctly. The actual problem is simpler: file descriptor retention - all N files stay open simultaneously until the function returns, not that only one gets closed. I'll update the PR description to be accurate. |
|
[APPROVALNOTIFIER] This PR is APPROVED This pull-request has been approved by: camilamacedo86, SebTardif The full list of commands accepted by this bot can be found here. The pull request process is described here DetailsNeeds approval from an approver in each of these files:
Approvers can indicate their approval by writing |
|
@SebTardif: The following test failed, say
Full PR test history. Your PR dashboard. Please help us cut down on flakes by linking to an open issue when you hit one in your PR. DetailsInstructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes-sigs/prow repository. I understand the commands that are listed here. |
|
/retest |
|
@SebTardif: Cannot trigger testing until a trusted user reviews the PR and leaves an DetailsIn response to this:
Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes-sigs/prow repository. |
Problem
In
pkg/plugins/external/helpers.go, thehandlePluginResponsefunction usesdefer f.Close()inside aforloop (line 203). This causes file descriptor retention: all deferred close calls stack up and only run when the enclosing function returns, not at each loop iteration.If
res.Universecontains N files, all N file descriptors remain open simultaneously untilhandlePluginResponsecompletes. For large responses or long-running operations, this can exhaust file descriptors.This bug was introduced in #2338 (2021-08-10) and has been present for nearly 5 years.
Fix
Replace the
deferblock with explicitf.Close()calls: one on the write error path (before returning) and one on the success path (after the write). This ensures each file is closed within its own loop iteration, releasing the file descriptor immediately after use.Verification
make lint-fix: 0 issuesgo test ./pkg/plugins/external/...: all pass