Skip to content

Commit fef8cbf

Browse files
keertipCommit Queue
authored andcommitted
Add a finite retry for creating contexts.
Fixes #61931. The exception we see when there are no file watchers available is a file path not found exception. This is not specific enough for us to exit on the exception, hence the finite retries. Change-Id: I8ba5081e1e916d3a1272821367806dc7d8391ec4 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/462780 Commit-Queue: Keerti Parthasarathy <[email protected]> Reviewed-by: Brian Wilkerson <[email protected]>
1 parent e9bf7d4 commit fef8cbf

File tree

1 file changed

+22
-4
lines changed

1 file changed

+22
-4
lines changed

pkg/analysis_server/lib/src/context_manager.dart

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,9 @@ abstract class ContextManagerCallbacks {
169169
/// Class that maintains a mapping from included/excluded paths to a set of
170170
/// folders that should correspond to analysis contexts.
171171
class ContextManagerImpl implements ContextManager {
172+
/// The max number of tries to add watchers while building contexts.
173+
static const int _maxWatcherRetries = 5;
174+
172175
/// The [OverlayResourceProvider] used to check for the existence of overlays
173176
/// and to convert paths into [Resource].
174177
final OverlayResourceProvider resourceProvider;
@@ -213,6 +216,9 @@ class ContextManagerImpl implements ContextManager {
213216
@override
214217
List<String> includedPaths = <String>[];
215218

219+
/// The number of trials to create watchers for the files in the context.
220+
int _watcherRetries = 0;
221+
216222
/// The instrumentation service used to report instrumentation data.
217223
final InstrumentationService _instrumentationService;
218224

@@ -702,10 +708,15 @@ class ContextManagerImpl implements ContextManager {
702708
// Errors in the watcher such as "Directory watcher closed
703709
// unexpectedly" on Windows when the buffer overflows also
704710
// require that we restarted to be consistent.
705-
needsBuild = true;
711+
if (_watcherRetries < _maxWatcherRetries) {
712+
needsBuild = true;
713+
_watcherRetries++;
714+
} else {
715+
needsBuild = false;
716+
}
706717
_instrumentationService.logError(
707718
'Temporary watcher error; restarting context build.\n'
708-
'$error\n$stackTrace',
719+
'watcherRetries:$_watcherRetries, needsBuild: $needsBuild, $error\n$stackTrace',
709720
);
710721
},
711722
),
@@ -906,8 +917,15 @@ class ContextManagerImpl implements ContextManager {
906917
_instrumentationService.logError(
907918
'Watcher error; refreshing contexts.\n$error\n$stackTrace',
908919
);
909-
// TODO(mfairhurst): Optimize this, or perhaps be less complete.
910-
refresh();
920+
// Retry a finite number of times.
921+
if (_watcherRetries < _maxWatcherRetries) {
922+
_watcherRetries++;
923+
refresh();
924+
} else {
925+
_instrumentationService.logError(
926+
'Error: Unable to create contexts, exiting ...',
927+
);
928+
}
911929
}
912930

913931
/// Checks whether the current roots were built using the same paths as

0 commit comments

Comments
 (0)