Exception: A socket operation was attempted to an unreachable host
See original GitHub issueWe have some systems that show the A socket operation was attempted to an unreachable host
message and crash the application. It starts with the following exception:
System.Net.Sockets.SocketException (0x80004005): A socket operation was attempted to an unreachable host
at System.Net.Sockets.Socket.SendTo(Byte[] buffer, Int32 offset, Int32 size, SocketFlags socketFlags, EndPoint remoteEP)
at Rssdp.UdpSocket.SendTo(Byte[] messageData, UdpEndPoint endPoint)
at Rssdp.Infrastructure.SsdpCommunicationsServer.<>c__DisplayClass22_0.<SendMulticastMessage>b__0()
at Rssdp.Infrastructure.SsdpCommunicationsServer.Repeat(Int32 repetitions, TimeSpan delay, Action work)
at Rssdp.Infrastructure.SsdpCommunicationsServer.SendMulticastMessage(Byte[] messageData)
at Rssdp.Infrastructure.SsdpDevicePublisherBase.SendAliveNotification(SsdpDevice device, String notificationType, String uniqueServiceName)
at Rssdp.Infrastructure.SsdpDevicePublisherBase.SendAliveNotifications(SsdpDevice device, Boolean isRoot)
at Rssdp.Infrastructure.SsdpDevicePublisherBase.SendAllAliveNotifications(Object state)
at System.Threading.TimerQueueTimer.CallCallbackInContext(Object state)
at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.TimerQueueTimer.CallCallback()
at System.Threading.TimerQueueTimer.Fire()
at System.Threading.TimerQueue.FireNextTimers()
at System.Threading.TimerQueue.AppDomainTimerCallback()
This exception cannot be handled by user code, because it is running from a timer. Our application is running as a service that automatically restarts (after 90 seconds). When the service restarts, then the following exception occurs when the SsdpDevicePublisherBase
constructor is invoked:
System.Net.Sockets.SocketException (0x80004005): A socket operation was attempted to an unreachable host
at System.Net.Sockets.Socket.setMulticastOption(SocketOptionName optionName, MulticastOption MR)
at System.Net.Sockets.Socket.SetSocketOption(SocketOptionLevel optionLevel, SocketOptionName optionName, Object optionValue)
at Rssdp.SocketFactory.CreateUdpMulticastSocket(String ipAddress, Int32 multicastTimeToLive, Int32 localPort)
at Rssdp.Infrastructure.SsdpCommunicationsServer.ListenForBroadcastsAsync()
at Rssdp.Infrastructure.SsdpCommunicationsServer.BeginListeningForBroadcasts()
at Rssdp.Infrastructure.SsdpDevicePublisherBase..ctor(ISsdpCommunicationsServer communicationsServer, String osName, String osVersion)
at Rssdp.SsdpDevicePublisher..ctor()
We always invoke the SsdpDevicePublisherBase
with a ISsdpCommunicationsServer
that is initialized like this:
new SsdpCommunicationsServer(new SocketFactory("0.0.0.0"), 2869);
Our service keeps restarting, but after a while it starts working again and everything seems to be fine again.
There are some environmental issues that might have impact:
- There are multiple systems in the network that advertise using RSSDP.
- Our systems are equiped with two network adapters and a virtual TUN/TAP adapter that is bridged to one of the network adapters.
Unfortunately, I don’t have an exact reproduction scenario, but it happens on multiple systems (that are in different LANs).
Issue Analytics
- State:
- Created 6 years ago
- Comments:8 (5 by maintainers)
Top GitHub Comments
Hi,
Thanks for following up on this and reporting back, that’s really great. I’ve been thinking about it, but I agree with you that this sort of problem seems best handled by the application code rather than the library. The application is in a better place to know how long it wants to wait/retry, what dependencies there might be, or if it even runs shortly after boot (not everything using RSSDP will). I’d like for the library to work as well as possible out of the box and ‘lead people into the pit of success’ , but I don’t think this particular case is something the library should handle itself.
Great to have the issue understood and documented though. Are you ok if we close this issue now?
Thanks again.
I have found that this issue only pops up directly after a reboot. Our service is registered to start during boot with start-type “automatic”. When I set the startup type of the service to “automatic (delayed)” then it works fine. This is the log that shows when the socket cannot be created:
My service currently depends on the Http service and LanmanServer service. I need the HTTP service, because I have an HttpListener in my application and I was hoping that the network layer would have been complete initialized afther the LanmanServer was ready. When I also add the Network Store Interface Service as a dependency, then this issue is gone.
I don’t think this is an actual bug in RSSDP, but the network is simply not available yet. I also notice some other network glitches directly after startup. My services runs on the 192.168.87.0/24 network and I have also seen messages that this network isn’t available yet. After 1-3 seconds, then this message disappears and the connection works fine.
PS: Error handling is fine, because I get an exception during the construction of the device publisher…