Fix client file descriptor leaks#2034
Conversation
Close stream buffer fd in `iperf_client_end()`. Close operation is protected by checking if the fd is valid. This prevents double close in case there's a code path calling iperf_free_stream() before iperf_client_end(). Protect stream buffer fd close in `iperf_free_stream()` with fd validity check. This prevent double close in normal test success. Double close is probably fine for close() call but valgrind will nag about it. Close /dev/urandom file in `readentropy()` after reading it. This prevents fd leaks in cases where libiperf is dlopen()'ed, a test is executed and the lib is dlclose()'ed repeatedly.
|
Thanks for the PR! We'll take a look at it. |
|
I'm start to play with this bug and fixes. Just FYI to invoke the server you can just run |
bmah888
left a comment
There was a problem hiding this comment.
Thanks again for the PR!
Just one comment in the code about another place where you might want to make your changes to close the buffer file descriptor.
Other than that, I think we're looking good on this, I confirm that your patch seems to fix the file descriptor leaks that valgrind found.
There was a problem hiding this comment.
Should this line of code also change to a check for a valid value of sp->buffer_fd, closing the file descriptor, and then setting sp->buffer_fd to -1, the same as you did earlier in the file?
There was a problem hiding this comment.
Checking sp->buffer_fd isn't necessary here because the descriptor is opened in the same function and if we get to this line the value will be valid.
But I did notice that error handling code in the function before this line does not close sp->buffer_fd. sp->diskfile_fd will also leak if iperf_init_stream() call fails. I have a commit fixing these and will push it shortly.
Also, I'd like to suggest moving error cleanups to the and of the function in error exit labels and jumping there using goto. I'll do this in a separate commit and add it to this PR. If it's not wanted we can just revert it or I can force push the fix branch without it.
|
I pushed two additional commits addressing leaks in |
PLEASE NOTE the following text from the iperf3 license. Submitting a
pull request to the iperf3 repository constitutes "[making]
Enhancements available...publicly":
The complete iperf3 license is available in the
LICENSEfile in thetop directory of the iperf3 source tree.
Version of iperf3 (or development branch, such as
masteror3.1-STABLE) to which this pull request applies:masterand at least releases 3.20 and 3.21Issues fixed (if any): -
Brief description of code changes (suitable for use as a commit message):
Close stream buffer fd in
iperf_client_end(). Close operation is protected by checking if the fd is valid. This prevents double close in case there's a code path callingiperf_free_stream()beforeiperf_client_end().Protect stream buffer fd close in
iperf_free_stream()with fd validity check. This prevent double close in normal test success. Double close is probably fine for close() call but valgrind will nag about it.Close /dev/urandom file in
readentropy()after reading it. This prevents fd leaks in cases where libiperf is dlopen()'ed, a test is executed and the lib is dlclose()'ed repeatedly.Background
We're using iperf as a dynamically loaded library (via dart's foreign function interface) in a long running process. When not in use the library is unloaded. With enough repeats client will run out of file descriptors due to leaks. In normal success case where iperf client ends after given duration (-t option) the
/dev/urandomfile inreadentropy()leaks. This happens because we "forget" the static file in library unloading. "Network unreachable" errors also cause this leak.Another leak happens if the connection to server is lost during the test: The stream buffer fd is not closed in this case.
There may be other leaks as we didn't do extensive testing but these are the ones we're currently running into.
Reproducers
Luckily these leaks are also reproducible in normal command line environment. We're using
valgrindto detect fd leaks and running iperf in Fedora 44 KDE.Version (the current master) and building:
Server command line:
Client command line and valgrind output when test finishes after given duration:
Client command line and valgrind output when test stops because the server is killed (with ctrl-c), equal to our "connection to server is lost" case:
These client leaks get fixed with this PR.