Skip to content

Commit 04e9145

Browse files
author
Mikhail Arkhipov
authored
Catch stack empty exception (microsoft#1998)
* Remove stale reference * Don't suppress LHS diagnostics on augmented assign * Revert "Don't suppress LHS diagnostics on augmented assign" This reverts commit 6109ac7. * Escape [ and ] * PR feedback * Catch stack exception * Undor some formatting * Catch specific exception * Log exception
1 parent 52a2397 commit 04e9145

File tree

1 file changed

+12
-6
lines changed

1 file changed

+12
-6
lines changed

src/Analysis/Ast/Impl/Analyzer/Evaluation/ExpressionEval.Scopes.cs

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
using Microsoft.Python.Analysis.Values;
2222
using Microsoft.Python.Core;
2323
using Microsoft.Python.Core.Disposables;
24-
using Microsoft.Python.Core.Text;
2524
using Microsoft.Python.Parsing.Ast;
2625

2726
namespace Microsoft.Python.Analysis.Analyzer.Evaluation {
@@ -55,12 +54,12 @@ public void DeclareVariable(string name, IMember value, VariableSource source, L
5554
if (member != null && !overwrite) {
5655
return;
5756
}
58-
57+
5958
if (source == VariableSource.Import && value is IVariable v) {
6059
CurrentScope.LinkVariable(name, v, location);
6160
return;
6261
}
63-
62+
6463
if (member != null) {
6564
if (!value.IsUnknown()) {
6665
CurrentScope.DeclareVariable(name, value, source, location);
@@ -191,10 +190,17 @@ public void Dispose() {
191190
// them better.
192191
// TODO: figure out threading/locking for the Open/Close pairs.
193192
// Debug.Assert(_eval._openScopes.Count > 0, "Attempt to close global scope");
194-
if (_eval._openScopes.Count > 0) {
195-
_eval._openScopes.Pop();
193+
try {
194+
if (_eval._openScopes.Count > 0) {
195+
_eval._openScopes.Pop();
196+
}
197+
_eval.CurrentScope = _eval._openScopes.Count == 0 ? _eval.GlobalScope : _eval._openScopes.Peek();
198+
} catch (InvalidOperationException) {
199+
// Per comment above this can happen occasionally.
200+
// The catch is tactical fix to prevent crashes since complete handling of open/close
201+
// in threaded cases would be much larger change.
202+
_eval.Log?.Log(TraceEventType.Verbose, "Error: Mismatched open/close in scope tracker - scope stack is empty on Dispose()");
196203
}
197-
_eval.CurrentScope = _eval._openScopes.Count == 0 ? _eval.GlobalScope : _eval._openScopes.Peek();
198204
}
199205
}
200206
}

0 commit comments

Comments
 (0)