-
Notifications
You must be signed in to change notification settings - Fork 5.2k
[Android] Fix accessing network interfaces information #62780
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
[Android] Fix accessing network interfaces information #62780
Conversation
|
Tagging subscribers to this area: @dotnet/ncl Issue DetailsThis PR is still WIP. On (old) Android we need to load The code is based on the Xamarin Android implementation: https://github.com/xamarin/xamarin-android/blob/main/src/monodroid/jni/xamarin_getifaddrs.cc This PR re-enables some tests that were disabled on Android in #50567
|
|
/azp run runtime-manual |
|
Azure Pipelines successfully started running 1 pipeline(s). |
|
/azp run runtime-manual |
|
Azure Pipelines successfully started running 1 pipeline(s). |
|
All the failing tests are unrelated to this PR. |
|
/azp run runtime-manual |
|
Azure Pipelines successfully started running 1 pipeline(s). |
|
/azp run runtime-manual |
|
Azure Pipelines successfully started running 1 pipeline(s). |
…oid-get-network-interfaces-bug
|
/azp run runtime-manual |
|
Azure Pipelines successfully started running 1 pipeline(s). |
| static atomic_bool initialized = false; | ||
| static atomic_bool access_guard = true; | ||
|
|
||
| while (!atomic_load(&initialized)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is pthread_once available on Android? It would be better to use it here instead of the busy waiting loop.
If two threads happen to try to initialize this at the same time, one of them may end up busy waiting for multiple miliseconds here in this implementation that's not efficient. pthread_once does not have this problem.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
pthread_once is available and it also uses atomic_load_explicit in a loop + futex syscalls, so it will most likely be heavier than this simple loop. The likelihood of two threads hitting this code at exactly the same time is so low that, IMO, it's not worth worrying about.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, pthread_once has a loop similar to this one, except that it also handles the case that I have pointed out. The extra cost of pthread_once is negligible given what this whole method does.
The #1 rule for lock-free programming: Do not write lock-free algorithm yourselves unless you absolutely have to.
…oid-get-network-interfaces-bug
|
/azp run runtime-manual |
|
Azure Pipelines successfully started running 1 pipeline(s). |
|
The failing |
|
/backport to release/6.0-maui |
|
Started backporting to release/6.0-maui: https://github.com/dotnet/runtime/actions/runs/1683046431 |
|
@simonrozsival backporting to release/6.0-maui failed, the patch most likely resulted in conflicts: $ git am --3way --ignore-whitespace --keep-non-patch changes.patch
Applying: Re-enable tests
Applying: Dynamically load getifaddrs if necessary
Using index info to reconstruct a base tree...
A src/native/libs/System.Native/pal_interfaceaddresses.c
Falling back to patching base and 3-way merge...
Auto-merging src/libraries/Native/Unix/System.Native/pal_interfaceaddresses.c
Applying: Prevent redeclaration of the ifaddrs struct
Using index info to reconstruct a base tree...
M src/mono/cmake/config.h.in
A src/native/libs/Common/pal_config.h.in
A src/native/libs/System.Native/pal_interfaceaddresses.c
A src/native/libs/configure.cmake
Falling back to patching base and 3-way merge...
Auto-merging src/mono/cmake/config.h.in
Auto-merging src/libraries/Native/Unix/configure.cmake
Auto-merging src/libraries/Native/Unix/System.Native/pal_interfaceaddresses.c
Auto-merging src/libraries/Native/Unix/Common/pal_config.h.in
Applying: Fix typo
Using index info to reconstruct a base tree...
A src/native/libs/System.Native/pal_interfaceaddresses.c
Falling back to patching base and 3-way merge...
Auto-merging src/libraries/Native/Unix/System.Native/pal_interfaceaddresses.c
Applying: Do not close the dynamic library
Using index info to reconstruct a base tree...
A src/native/libs/System.Native/pal_interfaceaddresses.c
Falling back to patching base and 3-way merge...
Auto-merging src/libraries/Native/Unix/System.Native/pal_interfaceaddresses.c
Applying: Enable the fixed functional tests in CI
error: sha1 information is lacking or useless (src/libraries/System.Net.NetworkInformation/tests/FunctionalTests/IPGlobalPropertiesTest.cs).
error: could not build fake ancestor
hint: Use 'git am --show-current-patch=diff' to see the failed patch
Patch failed at 0006 Enable the fixed functional tests in CI
When you have resolved this problem, run "git am --continue".
If you prefer to skip this patch, run "git am --skip" instead.
To restore the original branch and stop patching, run "git am --abort".
Error: The process '/usr/bin/git' failed with exit code 128Please backport manually! |
* Re-enable tests * Dynamically load getifaddrs if necessary * Prevent redeclaration of the ifaddrs struct * Fix typo * Do not close the dynamic library * Enable the fixed functional tests in CI * Reduce code duplication for obtaining libc file name * Simplify usage of the function pointers * Move typedefs * Rename the _ensure_ function * Remove fptr typedefs * Update comment * Add missing include * Update comment * Remove unnecessary comment * Move static variable * Remove unnecessary change * Move LIBC_FILENAME to the utilities header * Avoid error if constant is undefined * Conditionally include ifaddrs * Try to fix cmake_symbol_exists issue for browser * Minor tweaks * Try different way of detecting getifaddrs * Use the hack only for Android builds * Revert "Move LIBC_FILENAME to the utilities header" This reverts commit 4e67687. * Revert "Reduce code duplication for obtaining libc file name" This reverts commit aca15d1. * Simplify opening libc * Update code style * Fix race condition * Prevent race condition * Switch locking implementation for a lock-free implementation * Enable unit test for Android * Try using weak symbols * Fix function name * Revert "Fix function name" This reverts commit f927aae. * Revert "Try using weak symbols" This reverts commit 46d3ede. * Refactor code to use pthread_once
…tion (#63628) * [Android] Fix accessing network interfaces information (#62780) * Re-enable tests * Dynamically load getifaddrs if necessary * Prevent redeclaration of the ifaddrs struct * Fix typo * Do not close the dynamic library * Enable the fixed functional tests in CI * Reduce code duplication for obtaining libc file name * Simplify usage of the function pointers * Move typedefs * Rename the _ensure_ function * Remove fptr typedefs * Update comment * Add missing include * Update comment * Remove unnecessary comment * Move static variable * Remove unnecessary change * Move LIBC_FILENAME to the utilities header * Avoid error if constant is undefined * Conditionally include ifaddrs * Try to fix cmake_symbol_exists issue for browser * Minor tweaks * Try different way of detecting getifaddrs * Use the hack only for Android builds * Revert "Move LIBC_FILENAME to the utilities header" This reverts commit 4e67687. * Revert "Reduce code duplication for obtaining libc file name" This reverts commit aca15d1. * Simplify opening libc * Update code style * Fix race condition * Prevent race condition * Switch locking implementation for a lock-free implementation * Enable unit test for Android * Try using weak symbols * Fix function name * Revert "Fix function name" This reverts commit f927aae. * Revert "Try using weak symbols" This reverts commit 46d3ede. * Refactor code to use pthread_once * [Android] Throw PNSE for unavailable network information (#63633) * Update tests * Add android specific implementation * Add UnsupportedOSPlatform attributes * Fix typo * Remove unnecessary file reference * Clean-up code * Minor code clean-up * Remove dictionary * Refactoring * Revert comment change * Fix usage of Interop.Sys.GetNetworkInterfaces
getifaddrsandfreeifaddrsmanually because theifaddrs.hheader is missing in older Android releases (before API 24) but the functions are availabe in libc.check_symbol_existswasn't reliable for wasm builds so I changed it tocheck_function_existswhich we already use to detectgetifaddrsinsrc/mono/cmake/configure.cmakeThe code is based on the Xamarin Android implementation: https://github.com/xamarin/xamarin-android/blob/main/src/monodroid/jni/xamarin_getifaddrs.cc
This PR re-enables some tests that were disabled on Android in #50567
Ref #51303