Lapiniot / Upnp.Control.Web

0 stars 0 forks source link

Application crashes on startup #3

Closed Lapiniot closed 3 days ago

Lapiniot commented 3 days ago

[!NOTE] The issue has no relation to OS per se. See comment bellow

After update to macOS Sequoia application crashes:

crit: Microsoft.Extensions.Hosting.Internal.Host[10] The HostOptions.BackgroundServiceExceptionBehavior is configured to StopHost. A BackgroundService has thrown an unhandled exception, and the IHost instance is stopping. To avoid this behavior, configure this to Ignore; however the BackgroundService will not be restarted. System.ArgumentOutOfRangeException: newAddress ('18446744072468146368') must be less than or equal to '4294967295'. (Parameter 'newAddress') Actual value was 18446744072468146368. at System.ArgumentOutOfRangeException.ThrowGreater[T](T value, T other, String paramName) at System.ArgumentOutOfRangeException.ThrowIfGreaterThan[T](T value, T other, String paramName) at System.Net.IPAddress..ctor(Int64 newAddress) at Upnp.Control.Infrastructure.UpnpDiscovery.Configuration.ConfigureServicesExtensions.<>c__DisplayClass2_0.b__0(Socket socket, IPEndPoint groupEndPoint) in ./Upnp.Control.Infrastructure.UpnpDiscovery/Configuration/ConfigureServicesExtensions.cs:line 49 at IoT.Protocol.Upnp.SsdpSearchEnumerator.ConfigureSocket(Socket socket, IPEndPoint& receiveEndPoint) in ./IoT.Protocol.Upnp/SsdpSearchEnumerator.cs:line 46 at IoT.Protocol.UdpEnumerator1.GetAsyncEnumerator(CancellationToken cancellationToken)+MoveNext() in ./IoT.Protocol/UdpEnumerator.cs:line 31 at IoT.Protocol.UdpEnumerator1.GetAsyncEnumerator(CancellationToken cancellationToken)+System.Threading.Tasks.Sources.IValueTaskSource.GetResult() at Upnp.Control.Infrastructure.UpnpDiscovery.UpnpDiscoveryService.ExecuteAsync(CancellationToken stoppingToken) in ./Upnp.Control.Infrastructure.UpnpDiscovery/UpnpDiscoveryService.cs:line 30 at Upnp.Control.Infrastructure.UpnpDiscovery.UpnpDiscoveryService.ExecuteAsync(CancellationToken stoppingToken) in ./Upnp.Control.Infrastructure.UpnpDiscovery/UpnpDiscoveryService.cs:line 30 at Microsoft.Extensions.Hosting.Internal.Host.TryExecuteBackgroundServiceAsync(BackgroundService backgroundService)

Lapiniot commented 3 days ago

Update: After brief investigation it appeared this issue is not connected to macOS Sequoia itself. This code is guilty:

// Query MulticastInterface option from the configured socket directly to get effectively applied value
var mcint = (int)socket.GetSocketOption(socket.AddressFamily is AddressFamily.InterNetwork ? SocketOptionLevel.IP : SocketOptionLevel.IPv6, SocketOptionName.MulticastInterface);
LogMulticastConfiguration(logger, groupEndPoint, new IPAddress(mcint));

Dev machine IP address somehow weirdly changed and this helped to discover serious issue. GetSocketOption returns boxed object representing System.Int32 value for SocketOptionName.MulticastInterface which must be reinterpreted as unsigned System.UInt32 before up-casting to wider signed System.Int64 as argument for IPAddress constructor, otherwise we may get negative value in the range of Int32 for some IP addresses (what actually happened :worried: ).

Lapiniot commented 3 days ago

[!IMPORTANT] We should also interpret mcint differently depending on socket.AddressFamily: AddressFamily.InterNetwork - this value contains IPv4 address bytes and could be used as-is AddressFamily.InterNetworkV6 - just represents scopeId (effective interface index) and address must be queried from the system additionally