titanium-web-proxy icon indicating copy to clipboard operation
titanium-web-proxy copied to clipboard

Socket Invalid Argument...

Open larrybeall opened this issue 6 years ago • 3 comments

Hello, I am using the proxy in an automation tool I wrote for my client. He had to setup a new pc and has been having trouble starting the tool. This is a fresh install of Windows 10 rel 1809. I was able to capture a log of the error he is experiencing and have included it and a sample of the code below. Has anyone experienced this issue? I have been unable to reproduce this issue, so I'm hoping that someone has experienced a similar situation. Thanks in advance.

Error Log (sanitized): System.AggregateException: One or more errors occurred. ---> System.Net.Sockets.SocketException: An invalid argument was supplied at System.Net.Sockets.Socket..ctor(AddressFamily addressFamily, SocketType socketType, ProtocolType protocolType) at System.Net.Sockets.TcpListener..ctor(IPAddress localaddr, Int32 port) at Titanium.Web.Proxy.ProxyServer.listen(ProxyEndPoint endPoint) at Titanium.Web.Proxy.ProxyServer.Start() at AutomatedGrabber.Core.Infrastructure.AutomatedGrabberTransparentHttpObserver.StartProxy() in C:\Users\larryb\Documents\Projects\AutomatedGrabber\AutomatedGrabber.Core\Infrastructure\AutomatedGrabberTransparentHttpObserver.cs:line 106 at AutomatedGrabber.Core.Infrastructure.AutomatedGrabberTransparentHttpObserver.GetObservationsInternal() in C:\Users\larryb\Documents\Projects\AutomatedGrabber\AutomatedGrabber.Core\Infrastructure\AutomatedGrabberTransparentHttpObserver.cs:line 36 at AutomatedGrabber.Core.Infrastructure.BaseTransparentHttpObserver.<GetObservations>d__2.MoveNext() in C:\Users\larryb\Documents\Projects\AutomatedGrabber\AutomatedGrabber.Core\Infrastructure\BaseTransparentHttpObserver.cs:line 21 --- End of inner exception stack trace --- at System.Threading.Tasks.Task.ThrowIfExceptional(Boolean includeTaskCanceledExceptions) at System.Threading.Tasks.Task1.GetResultCore(Boolean waitCompletionNotification) at System.Threading.Tasks.Task1.get_Result() at AutomatedGrabber.Core.Infrastructure.AutomatedGrabberApiClient.Logon() in C:\Users\larryb\Documents\Projects\AutomatedGrabber\AutomatedGrabber.Core\Infrastructure\AutomatedGrabberApiClient.cs:line 50 ---> (Inner Exception #0) System.Net.Sockets.SocketException (0x80004005): An invalid argument was supplied at System.Net.Sockets.Socket..ctor(AddressFamily addressFamily, SocketType socketType, ProtocolType protocolType) at System.Net.Sockets.TcpListener..ctor(IPAddress localaddr, Int32 port) at Titanium.Web.Proxy.ProxyServer.listen(ProxyEndPoint endPoint) at Titanium.Web.Proxy.ProxyServer.Start() at AutomatedGrabber.Core.Infrastructure.AutomatedGrabberTransparentHttpObserver.StartProxy() in C:\Users\larryb\Documents\Projects\AutomatedGrabber\AutomatedGrabber.Core\Infrastructure\AutomatedGrabberTransparentHttpObserver.cs:line 106 at AutomatedGrabber.Core.Infrastructure.AutomatedGrabberTransparentHttpObserver.GetObservationsInternal() in C:\Users\larryb\Documents\Projects\AutomatedGrabber\AutomatedGrabber.Core\Infrastructure\AutomatedGrabberTransparentHttpObserver.cs:line 36 at AutomatedGrabber.Core.Infrastructure.BaseTransparentHttpObserver.<GetObservations>d__2.MoveNext() in C:\Users\larryb\Documents\Projects\AutomatedGrabber\AutomatedGrabber.Core\Infrastructure\BaseTransparentHttpObserver.cs:line 21<---

Code In Question (sanitized):

public AutomatedGrabberTransparentHttpObserver()
        {
            _proxyServer = new ProxyServer();
            _observedValues = new ConcurrentBag<ObservedValue>();
            _secondsToWait = (int)TimeSpan.FromMinutes(10).TotalSeconds;

            InitProxy();
        }

private void InitProxy()
        {
            _proxyServer.ForwardToUpstreamGateway = true;
            var endpoint = new ExplicitProxyEndPoint(IPAddress.Any, 8000);
            _proxyServer.AddEndPoint(endpoint);

            _proxyServer.BeforeResponse += ProxyServerOnBeforeResponse;
            _proxyServer.ExceptionFunc += exception => Console.WriteLine(exception.Message);
            endpoint.BeforeTunnelConnectRequest += EndpointOnBeforeTunnelConnectRequest;
        }

protected override Task<IEnumerable<ObservedValue>> GetObservationsInternal()
        {
            StartProxy();

            _observationComplete = false;
            var timedOut = false;
            var startTime = DateTime.Now;

            while (!_observationComplete && !timedOut)
            {
                _observationComplete = ValidateObservationComplete();
                Task.Delay(TimeSpan.FromMilliseconds(100));
                var timeDiff = DateTime.Now.Subtract(startTime);
                if (timeDiff.TotalSeconds >= _secondsToWait)
                {
                    timedOut = true;
                }
            }

            StopProxy();

            if (_observationComplete)
            {
                var toReturn = _observedValues.ToArray();
                return Task.FromResult<IEnumerable<ObservedValue>>(toReturn);
            }

            if (timedOut)
            {
                var exception = new HeaderTimeoutException();
                return Task.FromException<IEnumerable<ObservedValue>>(exception);
            }

            return Task.FromResult(Enumerable.Empty<ObservedValue>());
        }

private void StartProxy()
        {
            var endpoint = _proxyServer.ProxyEndPoints.OfType<ExplicitProxyEndPoint>().FirstOrDefault();

            _proxyServer.Start();

            if (endpoint != null)
            {
                _proxyServer.SetAsSystemHttpsProxy(endpoint);
            }
        }

larrybeall avatar Apr 01 '19 04:04 larrybeall

Are you perhaps calling StartProxy multiple times, trying to bind to same port and IP twice or more? Please provide a full project to reproduce this issue perhaps by uploading it as a zip file or as a repository.

https://stackoverflow.com/questions/19874725/system-net-sockets-socketeexception-an-invalid-argument-was-supplied-error-code

justcoding121 avatar Apr 02 '19 13:04 justcoding121

Sorry, I am just getting back to this. It appears to have been a misleading error. The error was definitely happening, but I believe something on his pc was causing it to block the initial socket creation and leaving things in a weird state where the code believed the socket was open even though the system was blocking all traffic. Then the second time that error would occur. He switched to a different computer and it worked fine. Who knows.

larrybeall avatar Apr 24 '19 01:04 larrybeall

Strange. Does it still happen? May I close this issue?

honfika avatar Dec 24 '19 14:12 honfika