Skip to content

Stream.fromIterable doesn't handle errors during iteration well #33431

@kevmoo

Description

@kevmoo

If an error is throw, it's not caught. The app just seems to exit with 0 without done being hit. Very weird.

The sample code at the bottom has the following output:

custom safe iteratorToStream
*** STARTING ***
*** STREAM CREATED ***
 DATA: 1
 DATA: 2
ERROR: no 3!
DONE!
*** DONE ***
true

new Stream.fromIterator
*** STARTING ***
*** STREAM CREATED ***
 DATA: 1
 DATA: 2
ERROR: no 3!

Note: no *** DONE ***, no true printed at the bottom. The app just exits (with status 0).

This is on -dev.61

import 'dart:async';

main() async {
  print('custom safe iteratorToStream');
  var completed = await _printStream(safeIteratorToStream);
  print(completed);
  print('');

  print('new Stream.fromIterator');
  completed =
      await _printStream((iterable) => new Stream.fromIterable(iterable));
  // THIS IS NEVER CALLED!
  print(completed);
}

Future<bool> _printStream(Stream makeStream(Iterable iterable)) async {
  print('*** STARTING ***');
  var stream = makeStream(_badIterable());
  print('*** STREAM CREATED ***');
  await stream.transform(_printingTransformer).drain();
  print('*** DONE ***');
  return true;
}

final _printingTransformer = new StreamTransformer<Object, Object>.fromHandlers(
    handleData: (data, sink) {
  print(' DATA: $data');
}, handleError: (error, stack, sink) {
  print('ERROR: $error');
}, handleDone: (sink) {
  print('DONE!');
  sink.close();
});

Stream safeIteratorToStream(Iterable source) {
  var controller = new StreamController();

  new Future(() {
    try {
      var iterator = source.iterator;
      while (iterator.moveNext()) {
        controller.add(iterator.current);
      }
    } catch (e, stack) {
      controller.addError(e, stack);
    } finally {
      controller.close();
    }
  });

  return controller.stream;
}

Iterable<int> _badIterable() sync* {
  yield 1;
  yield 2;
  throw 'no 3!';
}

CC @lrhn

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-core-librarySDK core library issues (core, async, ...); use area-vm or area-web for platform specific libraries.library-asynctype-bugIncorrect behavior (everything from a crash to more subtle misbehavior)

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions