Skip to content

Commit 1bd59fa

Browse files
committed
Catch exception in VariableDetails.GetChildren()
This change catches the GetValueInvocationException that can be raised in VariableDetails.GetChildren() when it is called on a thread other than the debugger pipeline thread. In the future, all requests for children of a variable should be done on this thread. For now we just catch the exception so that the debug adapter does not crash.
1 parent 97ad6ff commit 1bd59fa

File tree

1 file changed

+55
-44
lines changed

1 file changed

+55
-44
lines changed

src/PowerShellEditorServices/Debugging/VariableDetails.cs

Lines changed: 55 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -181,62 +181,73 @@ private static VariableDetails[] GetChildren(object obj)
181181
IDictionary dictionary = obj as IDictionary;
182182
IEnumerable enumerable = obj as IEnumerable;
183183

184-
if (psObject != null)
184+
try
185185
{
186-
childVariables.AddRange(
187-
psObject
188-
.Properties
189-
.Select(p => new VariableDetails(p)));
190-
}
191-
else if (dictionary != null)
192-
{
193-
childVariables.AddRange(
194-
dictionary
195-
.OfType<DictionaryEntry>()
196-
.Select(e => new VariableDetails(e.Key.ToString(), e.Value)));
197-
}
198-
else if (enumerable != null && !(obj is string))
199-
{
200-
int i = 0;
201-
foreach (var item in enumerable)
186+
if (psObject != null)
202187
{
203-
childVariables.Add(
204-
new VariableDetails(
205-
string.Format("[{0}]", i),
206-
item));
207-
208-
i++;
188+
childVariables.AddRange(
189+
psObject
190+
.Properties
191+
.Select(p => new VariableDetails(p)));
209192
}
210-
}
211-
else if (obj != null)
212-
{
213-
// Object must be a normal .NET type, pull all of its
214-
// properties and their values
215-
Type objectType = obj.GetType();
216-
var properties =
217-
objectType.GetProperties(
218-
BindingFlags.Public | BindingFlags.Instance);
219-
220-
foreach (var property in properties)
193+
else if (dictionary != null)
194+
{
195+
childVariables.AddRange(
196+
dictionary
197+
.OfType<DictionaryEntry>()
198+
.Select(e => new VariableDetails(e.Key.ToString(), e.Value)));
199+
}
200+
else if (enumerable != null && !(obj is string))
221201
{
222-
try
202+
int i = 0;
203+
foreach (var item in enumerable)
223204
{
224205
childVariables.Add(
225206
new VariableDetails(
226-
property.Name,
227-
property.GetValue(obj)));
207+
string.Format("[{0}]", i),
208+
item));
209+
210+
i++;
228211
}
229-
catch (Exception)
212+
}
213+
else if (obj != null)
214+
{
215+
// Object must be a normal .NET type, pull all of its
216+
// properties and their values
217+
Type objectType = obj.GetType();
218+
var properties =
219+
objectType.GetProperties(
220+
BindingFlags.Public | BindingFlags.Instance);
221+
222+
foreach (var property in properties)
230223
{
231-
// Some properties can throw exceptions, add the property
232-
// name and empty string
233-
childVariables.Add(
234-
new VariableDetails(
235-
property.Name,
236-
string.Empty));
224+
try
225+
{
226+
childVariables.Add(
227+
new VariableDetails(
228+
property.Name,
229+
property.GetValue(obj)));
230+
}
231+
catch (Exception)
232+
{
233+
// Some properties can throw exceptions, add the property
234+
// name and empty string
235+
childVariables.Add(
236+
new VariableDetails(
237+
property.Name,
238+
string.Empty));
239+
}
237240
}
238241
}
239242
}
243+
catch (GetValueInvocationException)
244+
{
245+
// This exception occurs when accessing the value of a
246+
// variable causes a script to be executed. Right now
247+
// we aren't loading children on the pipeline thread so
248+
// this causes an exception to be raised. In this case,
249+
// just return an empty list of children.
250+
}
240251

241252
return childVariables.ToArray();
242253
}

0 commit comments

Comments
 (0)