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(windows): memory leaks #53

Merged
merged 2 commits into from
Apr 21, 2024
Merged

fix(windows): memory leaks #53

merged 2 commits into from
Apr 21, 2024

Conversation

njeisecke
Copy link
Contributor

Hi Esteban!

This fixes two leaks in the Windows implementation.

Thank you!

The windows function works on a pointer of known size,
so no allocation is required. This also fixes the leak.

Also use the winapi type (which actually is u32).
@njeisecke
Copy link
Contributor Author

Ok, this should be the more idiomatic fix. Sorry for the noise.

@scanban
Copy link
Contributor

scanban commented Apr 17, 2024

about allocation retry count, in my use case it's possible that new interfaces appears in 5-10 per second. So one retry might not be enough tbh.

@njeisecke
Copy link
Contributor Author

Mmh, the documentation https://docs.microsoft.com/en-us/windows/win32/api/iphlpapi/nf-iphlpapi-getadaptersaddresses#return-value is slightly vague. What does "multiple times" mean? Maybe the counter should actually be kept. In my environment, the initial call fails (probably due the various VPN virtual adapters on that machine), the second one works.

@LeoBorai LeoBorai changed the title Fix windows memory leaks fix(windows): memory leaks Apr 17, 2024
@LeoBorai
Copy link
Owner

Hi guys! Currently I dont have a Windows Environment I could test against.

Changes seems okay so far, have you had any issues with this version?

@LeoBorai
Copy link
Owner

It would be nice te have any memory usage audition tool integrated in CI! Im curious on the fact that both PR went up almost at same time 😅.

If its possible to share how you guys found these leaks, and if theres a way to automate such auditing, I am happy to integrate as part of existing Windows workflows!

@scanban
Copy link
Contributor

scanban commented Apr 18, 2024

Mmh, the documentation https://docs.microsoft.com/en-us/windows/win32/api/iphlpapi/nf-iphlpapi-getadaptersaddresses#return-value is slightly vague. What does "multiple times" mean? Maybe the counter should actually be kept. In my environment, the initial call fails (probably due the various VPN virtual adapters on that machine), the second one works.

In my use case, it's possible that new interface 'll be added after return from the 1st call to the GetAdaptersAddresses and second call to it. Ofc it's quite rare, but possible. I have multiple devices connected via USB and using RNDIS network interfaces and they are being powered up all at once. So I am getting multiple new interfaces in system in a very short time frame.

@njeisecke
Copy link
Contributor Author

In my use case, it's possible that new interface 'll be added after return from the 1st call to the GetAdaptersAddresses and second call to it. Ofc it's quite rare, but possible. I have multiple devices connected via USB and using RNDIS network interfaces and they are being powered up all at once. So I am getting multiple new interfaces in system in a very short time frame.

There is no delay between the calls so it would be a rare incident for a third call to return a diifferent list than the second.

To me this sound more like you should provide some mechanism to query the adapter list in some interval und your control.

@scanban
Copy link
Contributor

scanban commented Apr 18, 2024

I am not sure, but I guess that windows are locking its device database during new device instantiation.
Google guys think so too: https://chromium.googlesource.com/chromium/src/+/HEAD/net/base/network_interfaces_win.cc#210

The buffer must finally be released, so use a RAAI allocation wrapper to
handle this (credits to [email protected] for providing a generic
implementation).

Also handle errors in a uniform way and use the winapi constants.
@njeisecke
Copy link
Contributor Author

This now also contains the @scanban changes relevant to the leak fix.

Copy link
Owner

@LeoBorai LeoBorai left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great set of improvements @njeisecke and @scanban!

Thanks!

@LeoBorai LeoBorai merged commit aa7f50d into LeoBorai:main Apr 21, 2024
12 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants