Skip to content

Commit 70ade8c

Browse files
committed
Merge pull request #406 from Microsoft/issue245
Debugger connection retry logic
2 parents 4acda62 + 543de5a commit 70ade8c

File tree

2 files changed

+47
-1
lines changed

2 files changed

+47
-1
lines changed

Nodejs/Product/Nodejs/Debugger/Communication/DebuggerConnection.cs

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
using System.Text.RegularExpressions;
2323
using System.Threading.Tasks;
2424
using Microsoft.NodejsTools.Logging;
25+
using Microsoft.VisualStudioTools;
2526
using Microsoft.VisualStudioTools.Project;
2627
using Newtonsoft.Json;
2728

@@ -112,10 +113,49 @@ public Version NodeVersion {
112113
/// <param name="uri">URI identifying the endpoint to connect to.</param>
113114
public void Connect(Uri uri) {
114115
Utilities.ArgumentNotNull("uri", uri);
116+
LiveLogger.WriteLine("Debugger connecting to URI: {0}", uri);
115117

116118
Close();
117119
lock (_networkClientLock) {
118-
_networkClient = _networkClientFactory.CreateNetworkClient(uri);
120+
int connection_attempts = 0;
121+
const int MAX_ATTEMPTS = 5;
122+
while (true) {
123+
connection_attempts++;
124+
try {
125+
// TODO: This currently results in a call to the synchronous TcpClient
126+
// constructor, which is a blocking call, and can take a couple of seconds
127+
// to connect (with timeouts and retries). This code is running on the UI
128+
// thread. Ideally this should be connecting async, or moved off the UI thread.
129+
_networkClient = _networkClientFactory.CreateNetworkClient(uri);
130+
131+
// Unclear if the above can succeed and not be connected, but check for safety.
132+
// The code needs to either break out the while loop, or hit the retry logic
133+
// in the exception handler.
134+
if (_networkClient.Connected) {
135+
LiveLogger.WriteLine("Debugger connected successfully");
136+
break;
137+
}
138+
else {
139+
throw new SocketException();
140+
}
141+
}
142+
catch (Exception ex) {
143+
if (ex.IsCriticalException()) {
144+
throw;
145+
}
146+
LiveLogger.WriteLine("Connection attempt {0} failed with: {1}", connection_attempts, ex);
147+
if (connection_attempts >= MAX_ATTEMPTS) {
148+
throw;
149+
}
150+
else {
151+
// See above TODO. This should be moved off the UI thread or posted to retry
152+
// without blocking in the meantime. For now, this seems the lesser of two
153+
// evils. (The other being the debugger failing to attach on launch if the
154+
// debuggee socket wasn't open quickly enough).
155+
System.Threading.Thread.Sleep(200);
156+
}
157+
}
158+
}
119159
}
120160

121161
Task.Factory.StartNew(ReceiveAndDispatchMessagesWorker);

Nodejs/Product/Nodejs/Debugger/DebugEngine/Remote/NodeRemoteEnumDebugProcesses.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,16 +105,22 @@ private static NodeRemoteDebugProcess Connect(NodeRemoteDebugPort port, INetwork
105105
} catch (OperationCanceledException) {
106106
LiveLogger.WriteLine("NodeRemoteEnumDebugProcesses ping timed out.");
107107
} catch (DebuggerAlreadyAttachedException ex) {
108+
LiveLogger.WriteLine("DebuggerAlreadyAttachedException connecting to remote debugger");
108109
exception = ex;
109110
} catch (AggregateException ex) {
111+
LiveLogger.WriteLine("AggregateException connecting to remote debugger");
110112
exception = ex;
111113
} catch (IOException ex) {
114+
LiveLogger.WriteLine("IOException connecting to remote debugger");
112115
exception = ex;
113116
} catch (SocketException ex) {
117+
LiveLogger.WriteLine("SocketException connecting to remote debugger");
114118
exception = ex;
115119
} catch (WebSocketException ex) {
120+
LiveLogger.WriteLine("WebSocketException connecting to remote debugger");
116121
exception = ex;
117122
} catch (PlatformNotSupportedException) {
123+
LiveLogger.WriteLine("PlatformNotSupportedException connecting to remote debugger");
118124
MessageBox.Show(
119125
"Remote debugging of node.js Microsoft Azure applications is only supported on Windows 8 and above.",
120126
null, MessageBoxButtons.OK, MessageBoxIcon.Error);

0 commit comments

Comments
 (0)