{"action":"created","issue":{"url":"https://api.github.com/repos/vtortola/WebSocketListener/issues/14","labels_url":"https://api.github.com/repos/vtortola/WebSocketListener/issues/14/labels{/name}","comments_url":"https://api.github.com/repos/vtortola/WebSocketListener/issues/14/comments","events_url":"https://api.github.com/repos/vtortola/WebSocketListener/issues/14/events","html_url":"https://github.com/vtortola/WebSocketListener/issues/14","id":62508610,"number":14,"title":"Occasional reconnect problems","user":{"login":"Dev-Eloper","id":8024701,"avatar_url":"https://avatars.githubusercontent.com/u/8024701?v=3","gravatar_id":"","url":"https://api.github.com/users/Dev-Eloper","html_url":"https://github.com/Dev-Eloper","followers_url":"https://api.github.com/users/Dev-Eloper/followers","following_url":"https://api.github.com/users/Dev-Eloper/following{/other_user}","gists_url":"https://api.github.com/users/Dev-Eloper/gists{/gist_id}","starred_url":"https://api.github.com/users/Dev-Eloper/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/Dev-Eloper/subscriptions","organizations_url":"https://api.github.com/users/Dev-Eloper/orgs","repos_url":"https://api.github.com/users/Dev-Eloper/repos","events_url":"https://api.github.com/users/Dev-Eloper/events{/privacy}","received_events_url":"https://api.github.com/users/Dev-Eloper/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":8,"created_at":"2015-03-17T20:22:02Z","updated_at":"2015-03-18T20:55:29Z","closed_at":null,"body":"Hi,\r\n\r\nI'm experiencing occasional problems when the client has closed the connection (perhaps ungracefully) and then tries to reconnect. The issue is quite difficult to reproduce. I see it about once a week. When it occurs, the symptoms are as follows:\r\n\r\n- The client reports that the connection was established successfully\r\n- The server however does not return from WebSocketListener.AcceptWebSocketAsync()\r\n- The server does arrive at WebSocketListener.NegotiateWebSocket(), and in fact this function returns a positive result: it says the socket is connected and no error occurred.\r\n- When I try to connect from a different client, AcceptWebSocketAsync() does return and a connection with the second client is established succesfully.\r\n- As soon as the second client has connected, a new connection attempt by the first client is also accepted.\r\n\r\nSo, it's quite a mystery to me what is going on here. To me this looks like a bug in the TransformBlock class, because I would expect TransformBlock.ReceiveAsync() to return as soon as NegotiateWebSocket has produced a valid WebSocketNegotiationResult. Perhaps you have some ideas?"},"comment":{"url":"https://api.github.com/repos/vtortola/WebSocketListener/issues/comments/83172278","html_url":"https://github.com/vtortola/WebSocketListener/issues/14#issuecomment-83172278","issue_url":"https://api.github.com/repos/vtortola/WebSocketListener/issues/14","id":83172278,"user":{"login":"Dev-Eloper","id":8024701,"avatar_url":"https://avatars.githubusercontent.com/u/8024701?v=3","gravatar_id":"","url":"https://api.github.com/users/Dev-Eloper","html_url":"https://github.com/Dev-Eloper","followers_url":"https://api.github.com/users/Dev-Eloper/followers","following_url":"https://api.github.com/users/Dev-Eloper/following{/other_user}","gists_url":"https://api.github.com/users/Dev-Eloper/gists{/gist_id}","starred_url":"https://api.github.com/users/Dev-Eloper/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/Dev-Eloper/subscriptions","organizations_url":"https://api.github.com/users/Dev-Eloper/orgs","repos_url":"https://api.github.com/users/Dev-Eloper/repos","events_url":"https://api.github.com/users/Dev-Eloper/events{/privacy}","received_events_url":"https://api.github.com/users/Dev-Eloper/received_events","type":"User","site_admin":false},"created_at":"2015-03-18T20:55:29Z","updated_at":"2015-03-18T20:55:29Z","body":"I was able to solve the problem with HandshakeAsync by making ReadHttpRequest asynchronous:\r\n\r\n<code>\r\nprivate async Task ReadHttpRequestAsync(Stream clientStream, WebSocketHandshake handshake)\r\n        {\r\n            using (var sr = new StreamReader(clientStream, Encoding.ASCII, false, 1024, true))\r\n            {\r\n                String line = await sr.ReadLineAsync().ConfigureAwait(false);\r\n\r\n                ParseGET(line, handshake);\r\n\r\n                while (!String.IsNullOrWhiteSpace(line = await sr.ReadLineAsync().ConfigureAwait(false)))\r\n                    ParseHeader(line, handshake);\r\n            }\r\n        }\r\n</code>\r\n\r\nWith this change, NegotiateWebSocket() does arrive at Task.WhenAny(timeoutTask, handshakeTask), causing AcceptWebSocketAsync() to return when the NegotiationTimeout expires."}}