Skip to content

Commit f33df2f

Browse files
committed
Fall back when DDS does not collect history for a stream
DDS only collects event history for a stream if the connected VM service supports it. The web debug service has no Timer stream, so with the new DDS implementation in the Dart SDK, `getStreamHistory('Timer')` fails with an invalid params error. `DdsExtension.onEventWithHistory` calls `getStreamHistory` when first listened to, and that error cannot be handled by listeners, which broke the Logging view (and the live connection integration test) for web apps. Check whether history is collected for a stream before using the DDS history stream, and fall back to the regular stream if not.
1 parent 82f52c3 commit f33df2f

1 file changed

Lines changed: 60 additions & 7 deletions

File tree

‎packages/devtools_app/lib/src/service/vm_service_wrapper.dart‎

Lines changed: 60 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -230,50 +230,103 @@ class VmServiceWrapper extends VmService {
230230

231231
Stream<Event> get onExtensionEventWithHistorySafe {
232232
return _maybeReturnStreamWithHistory(
233-
onExtensionEventWithHistory,
233+
EventStreams.kExtension,
234+
ddsStream: onExtensionEventWithHistory,
234235
fallbackStream: onExtensionEvent,
235236
);
236237
}
237238

238239
Stream<Event> get onLoggingEventWithHistorySafe {
239240
return _maybeReturnStreamWithHistory(
240-
onLoggingEventWithHistory,
241+
EventStreams.kLogging,
242+
ddsStream: onLoggingEventWithHistory,
241243
fallbackStream: onLoggingEvent,
242244
);
243245
}
244246

245247
Stream<Event> get onStderrEventWithHistorySafe {
246248
return _maybeReturnStreamWithHistory(
247-
onStderrEventWithHistory,
249+
EventStreams.kStderr,
250+
ddsStream: onStderrEventWithHistory,
248251
fallbackStream: onStderrEvent,
249252
);
250253
}
251254

252255
Stream<Event> get onStdoutEventWithHistorySafe {
253256
return _maybeReturnStreamWithHistory(
254-
onStdoutEventWithHistory,
257+
EventStreams.kStdout,
258+
ddsStream: onStdoutEventWithHistory,
255259
fallbackStream: onStdoutEvent,
256260
);
257261
}
258262

259263
Stream<Event> get onTimerEventWithHistorySafe {
260264
return _maybeReturnStreamWithHistory(
261-
onTimerEventWithHistory,
265+
EventStreams.kTimer,
266+
ddsStream: onTimerEventWithHistory,
262267
fallbackStream: onTimerEvent,
263268
);
264269
}
265270

266271
Stream<Event> _maybeReturnStreamWithHistory(
267-
Stream<Event> ddsStream, {
272+
String streamId, {
273+
required Stream<Event> ddsStream,
268274
required Stream<Event> fallbackStream,
269275
}) {
270276
assert(_supportedProtocolsInitialized.isCompleted);
271277
if (_ddsSupported) {
272-
return ddsStream;
278+
return _streamWithHistoryIfCollected(
279+
streamId,
280+
ddsStream: ddsStream,
281+
fallbackStream: fallbackStream,
282+
);
273283
}
274284
return fallbackStream;
275285
}
276286

287+
/// Returns [ddsStream] if DDS collects event history for [streamId], and
288+
/// [fallbackStream] otherwise.
289+
///
290+
/// DDS only collects event history for a stream when the VM service it is
291+
/// connected to supports that stream. For example, the web debug service
292+
/// does not support the `Timer` stream, so DDS rejects `getStreamHistory`
293+
/// requests for it with an invalid params error. The DDS history streams
294+
/// (e.g. [DdsExtension.onTimerEventWithHistory]) call `getStreamHistory`
295+
/// when first listened to, and that error cannot be handled by listeners, so
296+
/// we check whether history is collected before using [ddsStream].
297+
Stream<Event> _streamWithHistoryIfCollected(
298+
String streamId, {
299+
required Stream<Event> ddsStream,
300+
required Stream<Event> fallbackStream,
301+
}) async* {
302+
final historyCollected = await _isStreamHistoryCollected.putIfAbsent(
303+
streamId,
304+
() => _checkStreamHistoryCollected(streamId),
305+
);
306+
yield* historyCollected ? ddsStream : fallbackStream;
307+
}
308+
309+
/// Cache of whether DDS collects event history for a given stream id.
310+
final _isStreamHistoryCollected = <String, Future<bool>>{};
311+
312+
Future<bool> _checkStreamHistoryCollected(String streamId) async {
313+
try {
314+
await getStreamHistory(streamId);
315+
return true;
316+
} on RPCError catch (e) {
317+
if (e.code != RPCErrorKind.kInvalidParams.code) {
318+
// Do not cache unexpected failures so that we check again next time.
319+
unawaited(_isStreamHistoryCollected.remove(streamId));
320+
rethrow;
321+
}
322+
_log.fine(
323+
'Event history is not collected for stream "$streamId". Falling back '
324+
'to the stream without history.',
325+
);
326+
return false;
327+
}
328+
}
329+
277330
// Begin Dart IO extension method wrappers. We wrap these methods so that we
278331
// can override them in tests.
279332

0 commit comments

Comments
 (0)