Fix file descriptor leak on connection failure#21
Conversation
When ::connect() fails, the socket file descriptor was not closed before throwing an exception, leading to a file descriptor leak. This fix ensures the socket is properly closed on connection failure. The issue occurs in the Connection::with() factory method where a socket is created with ::socket() but not cleaned up if the subsequent ::connect() call fails.
4f72fc1 to
397a169
Compare
|
@Mazurel @MightyPiggie @alessandromrc Here is a bugfix for a file descriptor leak that is affecting my application. Please review at your earliest convenience and let me know if there is anything else I should do. Thanks! |
|
LGTM |
Does make sense as it's throwing a runtime_error but not freeing the socket... The ctor won't create the actual Connection instance, and so the dtor would never ever get called. |
Great - are you able to approve? @alessandromrc |
Sadly not, I only contributed to the repo, I have no power in merging or anything of that sort. Also this repo seems pretty stale, so I doubt that it will ever get merged. |
Mazurel
left a comment
There was a problem hiding this comment.
Looks nice, thank you a lot for this, and sorry for late response.
When ::connect() fails, the socket file descriptor was not closed before throwing an exception, leading to a file descriptor leak. This fix ensures the socket is properly closed on connection failure.
We had code that was retrying opening a modbus connection on an interval. When there was no device available, it would fail and create a new file descriptor every time. I tested this change and confirmed the leak resolved.