{"action":"created","comment":{"url":"https://api.github.com/repos/Microsoft/nodejstools/pulls/comments/38590673","id":38590673,"diff_hunk":"@@ -112,10 +112,53 @@ sealed class DebuggerConnection : IDebuggerConnection {\n         /// <param name=\"uri\">URI identifying the endpoint to connect to.</param>\n         public void Connect(Uri uri) {\n             Utilities.ArgumentNotNull(\"uri\", uri);\n+            LiveLogger.WriteLine(\"Debugger connecting to URI: {0}\", uri);\n \n             Close();\n             lock (_networkClientLock) {\n-                _networkClient = _networkClientFactory.CreateNetworkClient(uri);\n+                int connection_attempts = 0;\n+                const int MAX_ATTEMPTS = 5;\n+                while (true)\r\n+                {\r\n+                    connection_attempts++;\r\n+                    try\r\n+                    {\r\n+                        // TODO: This currently results in a call to the synchronous TcpClient\r\n+                        // constructor, which is a blocking call, and can take a couple of seconds\r\n+                        // to connect (with timeouts and retries). This code is running on the UI\r\n+                        // thread. Ideally this should be connecting async, or moved off the UI thread.\r\n+                        _networkClient = _networkClientFactory.CreateNetworkClient(uri);\r\n+\r\n+                        // Unclear if the above can succeed and not be connected, but check for safety.\r\n+                        // The code needs to either break out the while loop, or hit the retry logic\r\n+                        // in the exception handler.\r\n+                        if (_networkClient.Connected)\r\n+                        {\r\n+                            LiveLogger.WriteLine(\"Debugger connected successfully\");\r\n+                            break;\r\n+                        }\r\n+                        else\r\n+                        {\r\n+                            throw new SocketException();\r\n+                        }\r\n+                    }\r\n+                    catch (Exception ex)\r\n+                    {\r\n+                        LiveLogger.WriteLine(\"Connection attempt {0} failed with: {1}\", connection_attempts, ex);\r\n+                        if (connection_attempts >= MAX_ATTEMPTS)\r\n+                        {\r\n+                            throw;\r\n+                        }\r\n+                        else\r\n+                        {\r\n+                            // See above TODO. This should be moved off the UI thread or posted to retry\r\n+                            // without blocking in the meantime. For now, this seems the lesser of two\r\n+                            // evils. (The other being the debugger failing to attach on launch if the\r\n+                            // debuggee socket wasn't open quickly enough).\r\n+                            System.Threading.Thread.Sleep(200);\r","path":"Nodejs/Product/Nodejs/Debugger/Communication/DebuggerConnection.cs","position":52,"original_position":48,"commit_id":"543de5a60c9acd6729015a6deb2171aec2b9cf67","original_commit_id":"7285903785c6208689d68a98f8ec108c1b32ba21","user":{"login":"mousetraps","id":762848,"avatar_url":"https://avatars.githubusercontent.com/u/762848?v=3","gravatar_id":"","url":"https://api.github.com/users/mousetraps","html_url":"https://github.com/mousetraps","followers_url":"https://api.github.com/users/mousetraps/followers","following_url":"https://api.github.com/users/mousetraps/following{/other_user}","gists_url":"https://api.github.com/users/mousetraps/gists{/gist_id}","starred_url":"https://api.github.com/users/mousetraps/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/mousetraps/subscriptions","organizations_url":"https://api.github.com/users/mousetraps/orgs","repos_url":"https://api.github.com/users/mousetraps/repos","events_url":"https://api.github.com/users/mousetraps/events{/privacy}","received_events_url":"https://api.github.com/users/mousetraps/received_events","type":"User","site_admin":false},"body":"gotcha, makes sense","created_at":"2015-09-02T22:03:26Z","updated_at":"2015-09-02T22:03:26Z","html_url":"https://github.com/Microsoft/nodejstools/pull/406#discussion_r38590673","pull_request_url":"https://api.github.com/repos/Microsoft/nodejstools/pulls/406","_links":{"self":{"href":"https://api.github.com/repos/Microsoft/nodejstools/pulls/comments/38590673"},"html":{"href":"https://github.com/Microsoft/nodejstools/pull/406#discussion_r38590673"},"pull_request":{"href":"https://api.github.com/repos/Microsoft/nodejstools/pulls/406"}}},"pull_request":{"url":"https://api.github.com/repos/Microsoft/nodejstools/pulls/406","id":44019849,"html_url":"https://github.com/Microsoft/nodejstools/pull/406","diff_url":"https://github.com/Microsoft/nodejstools/pull/406.diff","patch_url":"https://github.com/Microsoft/nodejstools/pull/406.patch","issue_url":"https://api.github.com/repos/Microsoft/nodejstools/issues/406","number":406,"state":"open","locked":false,"title":"Debugger connection retry logic","user":{"login":"billti","id":993909,"avatar_url":"https://avatars.githubusercontent.com/u/993909?v=3","gravatar_id":"","url":"https://api.github.com/users/billti","html_url":"https://github.com/billti","followers_url":"https://api.github.com/users/billti/followers","following_url":"https://api.github.com/users/billti/following{/other_user}","gists_url":"https://api.github.com/users/billti/gists{/gist_id}","starred_url":"https://api.github.com/users/billti/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/billti/subscriptions","organizations_url":"https://api.github.com/users/billti/orgs","repos_url":"https://api.github.com/users/billti/repos","events_url":"https://api.github.com/users/billti/events{/privacy}","received_events_url":"https://api.github.com/users/billti/received_events","type":"User","site_admin":false},"body":"This adds retry logic when attempting to connect to the debug port on Node, in an attempt to resolve #245 and #246.\r\n\r\nIn some scenarios, especially when using Debug builds of Node/io.js, the Node process doesn't open the debug port (5858 by default) in time. At the network level, this results in the connection being refused. This change adds retry logic if the connection fails (and additional logging).\r\n\r\nOn my machine, using the 'Debug' build of the latest io.js branch (as using 'Release' shows no issue at all), I can now see the connection retry and succeed, e.g.\r\n\r\n```\r\n[17:20:09.0841895] AD7Engine Created (63760528)\r\n[17:20:09.0907085] AD7Engine Called SetRegistryRoot\r\n[17:20:09.0912082] AD7Engine Called SetLocale\r\n[17:20:09.0947085] AD7Engine LaunchSuspended Called with flags 'LAUNCH_ENABLE_ENC' (63760528)\r\n[17:20:09.1791871] Debugger connecting to URI: tcp://localhost:5858/\r\n[17:20:11.2412522] Connection attempt 1 failed with: System.Net.Sockets.SocketException (0x80004005): No connection could be made because the target machine actively refused it 127.0.0.1:5858\r\n   at System.Net.Sockets.TcpClient..ctor(String hostname, Int32 port)\r\n   at Microsoft.NodejsTools.Debugger.Communication.NetworkClientFactory.CreateNetworkClient(Uri uri) in C:\\src\\nodejstools\\Nodejs\\Product\\Nodejs\\Debugger\\Communication\\NetworkClientFactory.cs:line 32\r\n   at Microsoft.NodejsTools.Debugger.Communication.DebuggerConnection.Connect(Uri uri) in C:\\src\\nodejstools\\Nodejs\\Product\\Nodejs\\Debugger\\Communication\\DebuggerConnection.cs:line 130\r\n[17:20:12.9912323] Debugger connected successfully\r\n[17:20:12.9912323] DebuggerConnection: Request: {\"command\":\"scripts\",\"seq\":1,\"type\":\"request\",\"arguments\":{\"includeSource\":false}}\r\n```\r\n\r\nNote that on a retry connect, sometime the debugger enters a state where it doesn't appear to be connected, but using \"Debug / Break all\" will then break on the first line and debugging can then continue. This needs to be looked at separately.","created_at":"2015-09-02T17:31:58Z","updated_at":"2015-09-02T22:03:26Z","closed_at":null,"merged_at":null,"merge_commit_sha":"57145f1c88e2792bfd28a6cbf77ec79ec55e4f7c","assignee":null,"milestone":null,"commits_url":"https://api.github.com/repos/Microsoft/nodejstools/pulls/406/commits","review_comments_url":"https://api.github.com/repos/Microsoft/nodejstools/pulls/406/comments","review_comment_url":"https://api.github.com/repos/Microsoft/nodejstools/pulls/comments{/number}","comments_url":"https://api.github.com/repos/Microsoft/nodejstools/issues/406/comments","statuses_url":"https://api.github.com/repos/Microsoft/nodejstools/statuses/543de5a60c9acd6729015a6deb2171aec2b9cf67","head":{"label":"Microsoft:issue245","ref":"issue245","sha":"543de5a60c9acd6729015a6deb2171aec2b9cf67","user":{"login":"Microsoft","id":6154722,"avatar_url":"https://avatars.githubusercontent.com/u/6154722?v=3","gravatar_id":"","url":"https://api.github.com/users/Microsoft","html_url":"https://github.com/Microsoft","followers_url":"https://api.github.com/users/Microsoft/followers","following_url":"https://api.github.com/users/Microsoft/following{/other_user}","gists_url":"https://api.github.com/users/Microsoft/gists{/gist_id}","starred_url":"https://api.github.com/users/Microsoft/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/Microsoft/subscriptions","organizations_url":"https://api.github.com/users/Microsoft/orgs","repos_url":"https://api.github.com/users/Microsoft/repos","events_url":"https://api.github.com/users/Microsoft/events{/privacy}","received_events_url":"https://api.github.com/users/Microsoft/received_events","type":"Organization","site_admin":false},"repo":{"id":33017149,"name":"nodejstools","full_name":"Microsoft/nodejstools","owner":{"login":"Microsoft","id":6154722,"avatar_url":"https://avatars.githubusercontent.com/u/6154722?v=3","gravatar_id":"","url":"https://api.github.com/users/Microsoft","html_url":"https://github.com/Microsoft","followers_url":"https://api.github.com/users/Microsoft/followers","following_url":"https://api.github.com/users/Microsoft/following{/other_user}","gists_url":"https://api.github.com/users/Microsoft/gists{/gist_id}","starred_url":"https://api.github.com/users/Microsoft/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/Microsoft/subscriptions","organizations_url":"https://api.github.com/users/Microsoft/orgs","repos_url":"https://api.github.com/users/Microsoft/repos","events_url":"https://api.github.com/users/Microsoft/events{/privacy}","received_events_url":"https://api.github.com/users/Microsoft/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/Microsoft/nodejstools","description":"Node.js Tools for Visual Studio","fork":false,"url":"https://api.github.com/repos/Microsoft/nodejstools","forks_url":"https://api.github.com/repos/Microsoft/nodejstools/forks","keys_url":"https://api.github.com/repos/Microsoft/nodejstools/keys{/key_id}","collaborators_url":"https://api.github.com/repos/Microsoft/nodejstools/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/Microsoft/nodejstools/teams","hooks_url":"https://api.github.com/repos/Microsoft/nodejstools/hooks","issue_events_url":"https://api.github.com/repos/Microsoft/nodejstools/issues/events{/number}","events_url":"https://api.github.com/repos/Microsoft/nodejstools/events","assignees_url":"https://api.github.com/repos/Microsoft/nodejstools/assignees{/user}","branches_url":"https://api.github.com/repos/Microsoft/nodejstools/branches{/branch}","tags_url":"https://api.github.com/repos/Microsoft/nodejstools/tags","blobs_url":"https://api.github.com/repos/Microsoft/nodejstools/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/Microsoft/nodejstools/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/Microsoft/nodejstools/git/refs{/sha}","trees_url":"https://api.github.com/repos/Microsoft/nodejstools/git/trees{/sha}","statuses_url":"https://api.github.com/repos/Microsoft/nodejstools/statuses/{sha}","languages_url":"https://api.github.com/repos/Microsoft/nodejstools/languages","stargazers_url":"https://api.github.com/repos/Microsoft/nodejstools/stargazers","contributors_url":"https://api.github.com/repos/Microsoft/nodejstools/contributors","subscribers_url":"https://api.github.com/repos/Microsoft/nodejstools/subscribers","subscription_url":"https://api.github.com/repos/Microsoft/nodejstools/subscription","commits_url":"https://api.github.com/repos/Microsoft/nodejstools/commits{/sha}","git_commits_url":"https://api.github.com/repos/Microsoft/nodejstools/git/commits{/sha}","comments_url":"https://api.github.com/repos/Microsoft/nodejstools/comments{/number}","issue_comment_url":"https://api.github.com/repos/Microsoft/nodejstools/issues/comments{/number}","contents_url":"https://api.github.com/repos/Microsoft/nodejstools/contents/{+path}","compare_url":"https://api.github.com/repos/Microsoft/nodejstools/compare/{base}...{head}","merges_url":"https://api.github.com/repos/Microsoft/nodejstools/merges","archive_url":"https://api.github.com/repos/Microsoft/nodejstools/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/Microsoft/nodejstools/downloads","issues_url":"https://api.github.com/repos/Microsoft/nodejstools/issues{/number}","pulls_url":"https://api.github.com/repos/Microsoft/nodejstools/pulls{/number}","milestones_url":"https://api.github.com/repos/Microsoft/nodejstools/milestones{/number}","notifications_url":"https://api.github.com/repos/Microsoft/nodejstools/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/Microsoft/nodejstools/labels{/name}","releases_url":"https://api.github.com/repos/Microsoft/nodejstools/releases{/id}","created_at":"2015-03-28T01:37:20Z","updated_at":"2015-09-02T11:19:35Z","pushed_at":"2015-09-02T21:52:41Z","git_url":"git://github.com/Microsoft/nodejstools.git","ssh_url":"git@github.com:Microsoft/nodejstools.git","clone_url":"https://github.com/Microsoft/nodejstools.git","svn_url":"https://github.com/Microsoft/nodejstools","homepage":"http://aka.ms/explorentvs","size":98307,"stargazers_count":643,"watchers_count":643,"language":"C#","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":84,"mirror_url":null,"open_issues_count":187,"forks":84,"open_issues":187,"watchers":643,"default_branch":"master"}},"base":{"label":"Microsoft:master","ref":"master","sha":"289de92203b478122d38c63341b24cfa46288770","user":{"login":"Microsoft","id":6154722,"avatar_url":"https://avatars.githubusercontent.com/u/6154722?v=3","gravatar_id":"","url":"https://api.github.com/users/Microsoft","html_url":"https://github.com/Microsoft","followers_url":"https://api.github.com/users/Microsoft/followers","following_url":"https://api.github.com/users/Microsoft/following{/other_user}","gists_url":"https://api.github.com/users/Microsoft/gists{/gist_id}","starred_url":"https://api.github.com/users/Microsoft/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/Microsoft/subscriptions","organizations_url":"https://api.github.com/users/Microsoft/orgs","repos_url":"https://api.github.com/users/Microsoft/repos","events_url":"https://api.github.com/users/Microsoft/events{/privacy}","received_events_url":"https://api.github.com/users/Microsoft/received_events","type":"Organization","site_admin":false},"repo":{"id":33017149,"name":"nodejstools","full_name":"Microsoft/nodejstools","owner":{"login":"Microsoft","id":6154722,"avatar_url":"https://avatars.githubusercontent.com/u/6154722?v=3","gravatar_id":"","url":"https://api.github.com/users/Microsoft","html_url":"https://github.com/Microsoft","followers_url":"https://api.github.com/users/Microsoft/followers","following_url":"https://api.github.com/users/Microsoft/following{/other_user}","gists_url":"https://api.github.com/users/Microsoft/gists{/gist_id}","starred_url":"https://api.github.com/users/Microsoft/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/Microsoft/subscriptions","organizations_url":"https://api.github.com/users/Microsoft/orgs","repos_url":"https://api.github.com/users/Microsoft/repos","events_url":"https://api.github.com/users/Microsoft/events{/privacy}","received_events_url":"https://api.github.com/users/Microsoft/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/Microsoft/nodejstools","description":"Node.js Tools for Visual Studio","fork":false,"url":"https://api.github.com/repos/Microsoft/nodejstools","forks_url":"https://api.github.com/repos/Microsoft/nodejstools/forks","keys_url":"https://api.github.com/repos/Microsoft/nodejstools/keys{/key_id}","collaborators_url":"https://api.github.com/repos/Microsoft/nodejstools/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/Microsoft/nodejstools/teams","hooks_url":"https://api.github.com/repos/Microsoft/nodejstools/hooks","issue_events_url":"https://api.github.com/repos/Microsoft/nodejstools/issues/events{/number}","events_url":"https://api.github.com/repos/Microsoft/nodejstools/events","assignees_url":"https://api.github.com/repos/Microsoft/nodejstools/assignees{/user}","branches_url":"https://api.github.com/repos/Microsoft/nodejstools/branches{/branch}","tags_url":"https://api.github.com/repos/Microsoft/nodejstools/tags","blobs_url":"https://api.github.com/repos/Microsoft/nodejstools/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/Microsoft/nodejstools/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/Microsoft/nodejstools/git/refs{/sha}","trees_url":"https://api.github.com/repos/Microsoft/nodejstools/git/trees{/sha}","statuses_url":"https://api.github.com/repos/Microsoft/nodejstools/statuses/{sha}","languages_url":"https://api.github.com/repos/Microsoft/nodejstools/languages","stargazers_url":"https://api.github.com/repos/Microsoft/nodejstools/stargazers","contributors_url":"https://api.github.com/repos/Microsoft/nodejstools/contributors","subscribers_url":"https://api.github.com/repos/Microsoft/nodejstools/subscribers","subscription_url":"https://api.github.com/repos/Microsoft/nodejstools/subscription","commits_url":"https://api.github.com/repos/Microsoft/nodejstools/commits{/sha}","git_commits_url":"https://api.github.com/repos/Microsoft/nodejstools/git/commits{/sha}","comments_url":"https://api.github.com/repos/Microsoft/nodejstools/comments{/number}","issue_comment_url":"https://api.github.com/repos/Microsoft/nodejstools/issues/comments{/number}","contents_url":"https://api.github.com/repos/Microsoft/nodejstools/contents/{+path}","compare_url":"https://api.github.com/repos/Microsoft/nodejstools/compare/{base}...{head}","merges_url":"https://api.github.com/repos/Microsoft/nodejstools/merges","archive_url":"https://api.github.com/repos/Microsoft/nodejstools/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/Microsoft/nodejstools/downloads","issues_url":"https://api.github.com/repos/Microsoft/nodejstools/issues{/number}","pulls_url":"https://api.github.com/repos/Microsoft/nodejstools/pulls{/number}","milestones_url":"https://api.github.com/repos/Microsoft/nodejstools/milestones{/number}","notifications_url":"https://api.github.com/repos/Microsoft/nodejstools/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/Microsoft/nodejstools/labels{/name}","releases_url":"https://api.github.com/repos/Microsoft/nodejstools/releases{/id}","created_at":"2015-03-28T01:37:20Z","updated_at":"2015-09-02T11:19:35Z","pushed_at":"2015-09-02T21:52:41Z","git_url":"git://github.com/Microsoft/nodejstools.git","ssh_url":"git@github.com:Microsoft/nodejstools.git","clone_url":"https://github.com/Microsoft/nodejstools.git","svn_url":"https://github.com/Microsoft/nodejstools","homepage":"http://aka.ms/explorentvs","size":98307,"stargazers_count":643,"watchers_count":643,"language":"C#","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":84,"mirror_url":null,"open_issues_count":187,"forks":84,"open_issues":187,"watchers":643,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/Microsoft/nodejstools/pulls/406"},"html":{"href":"https://github.com/Microsoft/nodejstools/pull/406"},"issue":{"href":"https://api.github.com/repos/Microsoft/nodejstools/issues/406"},"comments":{"href":"https://api.github.com/repos/Microsoft/nodejstools/issues/406/comments"},"review_comments":{"href":"https://api.github.com/repos/Microsoft/nodejstools/pulls/406/comments"},"review_comment":{"href":"https://api.github.com/repos/Microsoft/nodejstools/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/Microsoft/nodejstools/pulls/406/commits"},"statuses":{"href":"https://api.github.com/repos/Microsoft/nodejstools/statuses/543de5a60c9acd6729015a6deb2171aec2b9cf67"}}}}