openlang

await for over ReceivePort never finishes after isolates are done

hi all, small dart tool here, it summarises temperature CSVs and I spawn one isolate per file so the big ones don't wait for each other. i run it in WSL like this:

dart run bin/summarize.dart data/north.csv data/south.csv -v

with -v i see the two "spawned worker" lines and then two "got ... rows" lines, and the row counts are right. so the workers clearly do their work. but then nothing. no table, no prompt back, i have to Ctrl+C.

first i thought the readAsLinesSync in the worker was blocking something, then i wrapped the worker body in try/catch, but nothing is thrown and nothing changes.

what confuses me is that i remember the loop in main ending by itself once all the spawned isolates were finished. that's why i'm using await for and not counting results. this is the main part:

final port = ReceivePort();

for (final p in paths) {
  await Isolate.spawn(summarizeFile, WorkOrder(p, port.sendPort));
  log.debug('spawned worker for $p');
}

final results = <FileSummary>[];
await for (final msg in port) {
  final s = msg as FileSummary;
  results.add(s);
  log.info('got ${s.path}: ${s.count} rows');
}

results.sort((a, b) => a.path.compareTo(b.path));
printTable(results);

worker just does order.replyTo.send(FileSummary(...)) at the end and returns. recent Dart 3.x. did something change about when that loop ends? i don't want to rewrite the collecting part before i understand this.

The conversation continues 2

Follow the reasoning
  1. Permalink

    Nothing changed — a ReceivePort never closes on its own. The await for ends only when the port is closed, and nobody closes it. Isolates finishing doesn't count. Your memory is probably from a setup with Isolate.run or an exit-port trick.

    Easiest fix: count results, and call port.close() once you have paths.length. Or use Isolate.run per file and Future.wait.

    Docs: https://api.dart.dev/stable/dart-isolate/ReceivePort-class.html

Other sides of the same question

  1. canceled context but worker goroutines still alive after first error (always workers + 2)

    Hi all, I am building a small Go CLI, shotcheck, that checks every PNG in a folder against the exact size the App Store slot…

    Replies 4Latest activity go
  2. Marking the filter async did nothing for the freeze

    ledgerscan search over 400k entries. i made run async and await it from main, thinking the 100 ms heartbeat Timer would keep…

    Replies 2Latest activity dart
  3. outliers.log has more lines than the :outliers counter, 8 futures

    I fold 20000 simulated tide readings into one atom from 8 futures. Every reading above the threshold also gets appended to ou…

    Replies 1Latest activity clojure