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

[Android] Prevent exceptions with a 'Success' message in System.Net.NetworkInformation #78555

Merged

Conversation

simonrozsival
Copy link
Member

There is a problem with permissions in new Android API levels (target SDK version 30+) causing NetworkChange.NetworkAddressChanged and NetworkInterface.GetAllNetworkInterfaces to throw an exception. The exception message would simply say "Success" which is quite confusing:

System.Net.NetworkInformation.NetworkInformationException (0x80004005): Success
   at System.Net.NetworkInformation.NetworkChange.CreateSocket()
   at System.Net.NetworkInformation.NetworkChange.add_NetworkAvailabilityChanged(NetworkAvailabilityChangedEventHandler )
   at Program.<Main>$(String[] ) in /.../dotnet/runtime/src/mono/sample/Android/Program.cs:line 10

The message is obtained by calling Interop.Sys.GetLastErrorInfo().GetErrorMessage() and the error code from Marshal.GetLastPInvokeError(), but the last pinvoke error code isn't preserved. This PR doesn't fix the underlying problem with accessing the network information but it fixes the exception message to reduce developer confusion.

The fixed exception stack trace:

System.Net.NetworkInformation.NetworkInformationException (13): Permission denied
   at System.Net.NetworkInformation.NetworkChange.CreateSocket()
   at System.Net.NetworkInformation.NetworkChange.add_NetworkAddressChanged(NetworkAddressChangedEventHandler )
   at Program.<Main>$(String[] ) in /.../dotnet/runtime/src/mono/sample/Android/Program.cs:line 9

Ref #77822
Ref #77938

@ghost
Copy link

ghost commented Nov 18, 2022

Tagging subscribers to this area: @dotnet/ncl
See info in area-owners.md if you want to be subscribed.

Issue Details

There is a problem with permissions in new Android API levels (target SDK version 30+) causing NetworkChange.NetworkAddressChanged and NetworkInterface.GetAllNetworkInterfaces to throw an exception. The exception message would simply say "Success" which is quite confusing:

System.Net.NetworkInformation.NetworkInformationException (0x80004005): Success
   at System.Net.NetworkInformation.NetworkChange.CreateSocket()
   at System.Net.NetworkInformation.NetworkChange.add_NetworkAvailabilityChanged(NetworkAvailabilityChangedEventHandler )
   at Program.<Main>$(String[] ) in /.../dotnet/runtime/src/mono/sample/Android/Program.cs:line 10

The message is obtained by calling Interop.Sys.GetLastErrorInfo().GetErrorMessage() and the error code from Marshal.GetLastPInvokeError(), but the last pinvoke error code isn't preserved. This PR doesn't fix the underlying problem with accessing the network information but it fixes the exception message to reduce developer confusion.

The fixed exception stack trace:

System.Net.NetworkInformation.NetworkInformationException (13): Permission denied
   at System.Net.NetworkInformation.NetworkChange.CreateSocket()
   at System.Net.NetworkInformation.NetworkChange.add_NetworkAddressChanged(NetworkAddressChangedEventHandler )
   at Program.<Main>$(String[] ) in /.../dotnet/runtime/src/mono/sample/Android/Program.cs:line 9

Ref #77822
Ref #77938

Author: simonrozsival
Assignees: -
Labels:

area-System.Net, os-android

Milestone: -

@ghost ghost assigned simonrozsival Nov 18, 2022
@simonrozsival
Copy link
Member Author

/azp run runtime-extra-platforms

@azure-pipelines
Copy link

Azure Pipelines successfully started running 1 pipeline(s).

@simonrozsival simonrozsival merged commit 320528e into dotnet:main Nov 30, 2022
@simonrozsival simonrozsival deleted the fix-network-change-address-exception branch November 30, 2022 14:20
@ghost ghost locked as resolved and limited conversation to collaborators Dec 30, 2022
@karelz karelz added this to the 8.0.0 milestone Mar 22, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants