{"action":"created","issue":{"url":"https://api.github.com/repos/dotnet/coreclr/issues/6413","repository_url":"https://api.github.com/repos/dotnet/coreclr","labels_url":"https://api.github.com/repos/dotnet/coreclr/issues/6413/labels{/name}","comments_url":"https://api.github.com/repos/dotnet/coreclr/issues/6413/comments","events_url":"https://api.github.com/repos/dotnet/coreclr/issues/6413/events","html_url":"https://github.com/dotnet/coreclr/issues/6413","id":167084776,"number":6413,"title":"An unhandled exception of type 'System.NullReferenceException' occurred in System.Private.CoreLib.ni.dll","user":{"login":"NVentimiglia","id":125889,"avatar_url":"https://avatars.githubusercontent.com/u/125889?v=3","gravatar_id":"","url":"https://api.github.com/users/NVentimiglia","html_url":"https://github.com/NVentimiglia","followers_url":"https://api.github.com/users/NVentimiglia/followers","following_url":"https://api.github.com/users/NVentimiglia/following{/other_user}","gists_url":"https://api.github.com/users/NVentimiglia/gists{/gist_id}","starred_url":"https://api.github.com/users/NVentimiglia/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/NVentimiglia/subscriptions","organizations_url":"https://api.github.com/users/NVentimiglia/orgs","repos_url":"https://api.github.com/users/NVentimiglia/repos","events_url":"https://api.github.com/users/NVentimiglia/events{/privacy}","received_events_url":"https://api.github.com/users/NVentimiglia/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":2,"created_at":"2016-07-22T16:38:37Z","updated_at":"2016-07-25T03:24:00Z","closed_at":null,"body":"This class wraps the tcpclient yielded by the tcplistener. When I close the connection client side, the class throws a null ref exception. Early returning from the StartReceiving method evades the exception, so, my guess is this is some sort of race condition related to listeneing to a dead socket.\r\n\r\nMy problem is, I can not catch this exception. No matter how many of break points in the catch handles seems to catch anything. When I try to inspect the exception, I am pointed to System.Private.CoreLib, *which is not my code*.\r\n\r\n### Exception\r\n`````\r\n An unhandled exception of type 'System.NullReferenceException' occurred in System.Private.CoreLib.ni.dll\r\n`````\r\n\r\n### Call Stack\r\n\r\n`````\r\nSystem.Private.CoreLib.ni.dll!System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext executionContext, System.Threading.ContextCallback callback, object state)\tUnknown\r\n \tSystem.Private.CoreLib.ni.dll!System.Threading.QueueUserWorkItemCallbackDefaultContext.System.Threading.IThreadPoolWorkItem.ExecuteWorkItem()\tUnknown\r\n \tSystem.Private.CoreLib.ni.dll!System.Threading.ThreadPoolWorkQueue.Dispatch()\tUnknown\r\n`````\r\n\r\n### Code\r\n\r\n`````\r\n /// <summary>\r\n    /// http://codereview.stackexchange.com/questions/82806/asynctcpclient-asynchronous-tcpclient\r\n    /// https://github.com/sethcall/async-helper/blob/master/src/AsyncTcpClient/AsyncTcpClient.cs\r\n    /// </summary>\r\n    public class AsyncTcpClient : IDisposable\r\n    {\r\n        private bool disposed = false;\r\n        public TcpClient tcpClient;\r\n        public NetworkStream stream;\r\n        private AsyncLock myLock = new AsyncLock();\r\n\r\n        public event Action<byte[]> OnData = delegate { };\r\n        public event Action OnClose = delegate { };\r\n        public event Action<Exception> OnError = delegate { };\r\n\r\n\r\n        public bool IsOpen\r\n        {\r\n            get\r\n            {\r\n                return !disposed && this.tcpClient != null && this.tcpClient.Connected;\r\n            }\r\n        }\r\n\r\n        public AsyncTcpClient(TcpClient client)\r\n        {\r\n            tcpClient = client;\r\n            tcpClient.NoDelay = true; // No nagel\r\n            tcpClient.SendBufferSize = Signal.MAXIMUM_BUFFER_SIZE;\r\n            tcpClient.ReceiveBufferSize = Signal.MAXIMUM_BUFFER_SIZE;\r\n\r\n            stream = (tcpClient.GetStream());\r\n\r\n            Task.Factory.StartNew(StartReceiving, TaskCreationOptions.LongRunning);\r\n        }\r\n\r\n\r\n        public async Task SendAsync(byte[] data, ushort size)\r\n        {\r\n            try\r\n            {\r\n                if (!IsOpen && !disposed)\r\n                    return;\r\n\r\n                //can only send one at a time\r\n                using (var releaser = await myLock.LockAsync())\r\n                {\r\n                    //send payload\r\n                    await stream.WriteAsync(data, 0, size);\r\n                }\r\n            }\r\n            catch (Exception ex)\r\n            {\r\n                OnError(ex);\r\n                Dispose();\r\n            }\r\n        }\r\n\r\n        async void StartReceiving()\r\n        {\r\n            byte[] payloadBuffer = BufferFactory.Rent(Signal.MAXIMUM_BUFFER_SIZE);\r\n            int payloadIndex = 0;\r\n            ushort size = 0;\r\n\r\n            try\r\n            {\r\n                while (IsOpen)\r\n                {\r\n                    try\r\n                    {\r\n                        //read the size header\r\n                        payloadIndex += await stream.ReadAsync(payloadBuffer, payloadIndex, sizeof(ushort));\r\n\r\n                        //sanity\r\n                        if (!IsOpen)\r\n                            break;\r\n\r\n                        //get size\r\n                        size = BufferHelper.ReadUInt16(payloadBuffer, 0);\r\n\r\n                        //get rest of the header / payload\r\n                        payloadIndex += await stream.ReadAsync(payloadBuffer, payloadIndex, size - sizeof(ushort));\r\n\r\n                        //sanity\r\n                        if (!IsOpen)\r\n                            break;\r\n\r\n                        //copy the payload and yield it \r\n                        byte[] copy = BufferFactory.Rent(size);\r\n                        Buffer.BlockCopy(payloadBuffer, 0, copy, 0, size);\r\n                        OnData(copy);\r\n\r\n                        //reset\r\n                        payloadIndex = 0;\r\n                        size = 0;\r\n                    }\r\n\r\n                    catch (ObjectDisposedException)\r\n                    {\r\n\r\n                    }\r\n                    catch (Exception ex)\r\n                    {\r\n                        OnError(ex);\r\n                        Dispose();\r\n                    }\r\n                }\r\n            }\r\n            catch (Exception ex)\r\n            {\r\n                OnError(ex);\r\n            }\r\n\r\n            if (payloadBuffer != null)\r\n            {\r\n                BufferFactory.Return(payloadBuffer);\r\n            }\r\n        }\r\n\r\n        public void Dispose()\r\n        {\r\n            if (!disposed)\r\n            {\r\n                try\r\n                {\r\n                    if (tcpClient != null)\r\n                    {\r\n                        tcpClient.Dispose();\r\n                        tcpClient = null;\r\n                    }\r\n                    if (stream != null)\r\n                    {\r\n                        stream.Dispose();\r\n                        stream = null;\r\n                    }\r\n                }\r\n                catch (Exception ex)\r\n                {\r\n                    \r\n                }\r\n\r\n                GC.SuppressFinalize(this);\r\n\r\n                disposed = true;\r\n                OnClose();\r\n            }\r\n        }\r\n    }\r\n`````"},"comment":{"url":"https://api.github.com/repos/dotnet/coreclr/issues/comments/234828297","html_url":"https://github.com/dotnet/coreclr/issues/6413#issuecomment-234828297","issue_url":"https://api.github.com/repos/dotnet/coreclr/issues/6413","id":234828297,"user":{"login":"NVentimiglia","id":125889,"avatar_url":"https://avatars.githubusercontent.com/u/125889?v=3","gravatar_id":"","url":"https://api.github.com/users/NVentimiglia","html_url":"https://github.com/NVentimiglia","followers_url":"https://api.github.com/users/NVentimiglia/followers","following_url":"https://api.github.com/users/NVentimiglia/following{/other_user}","gists_url":"https://api.github.com/users/NVentimiglia/gists{/gist_id}","starred_url":"https://api.github.com/users/NVentimiglia/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/NVentimiglia/subscriptions","organizations_url":"https://api.github.com/users/NVentimiglia/orgs","repos_url":"https://api.github.com/users/NVentimiglia/repos","events_url":"https://api.github.com/users/NVentimiglia/events{/privacy}","received_events_url":"https://api.github.com/users/NVentimiglia/received_events","type":"User","site_admin":false},"created_at":"2016-07-25T03:24:00Z","updated_at":"2016-07-25T03:24:00Z","body":"@jkotas \r\nhttps://github.com/NVentimiglia/TCPTests/tree/master\r\n\r\n- Open Solution\r\n- Run both projects client and server, this is a solution property\r\n- Server runs on 5000, ensure nothing is using that port\r\n- On the client terminal, press return. This will connect the client.\r\n- On the client terminal, press return. This will terminate the client.\r\n- Notice that the server crashes with a NRE from inside non client code."}}
{"actor":{"display_login":"NVentimiglia"}}