Skip to content
This repository was archived by the owner on Sep 27, 2021. It is now read-only.

Commit f9e5630

Browse files
committed
Make IsolateRunner.close not keep the isolate alive if called more than once.
1 parent 1477d6c commit f9e5630

File tree

2 files changed

+20
-5
lines changed

2 files changed

+20
-5
lines changed

lib/isolate_runner.dart

+8-1
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,11 @@ class IsolateRunner implements Runner {
3232
/// Future returned by [onExit]. Set when [onExit] is first read.
3333
Future<void>? _onExitFuture;
3434

35+
/// Future returned by [close].
36+
///
37+
/// Avoids hanging if calling [close] twice.
38+
Future<void>? _closeFuture;
39+
3540
/// Create an [IsolateRunner] wrapper for [isolate]
3641
///
3742
/// The preferred way to create an `IsolateRunner` is to use [spawn]
@@ -78,9 +83,11 @@ class IsolateRunner implements Runner {
7883
/// life cycle.
7984
@override
8085
Future<void> close() {
86+
var closeFuture = _closeFuture;
87+
if (closeFuture != null) return closeFuture;
8188
var channel = SingleResponseChannel();
8289
_commandPort.send(list2(_shutdown, channel.port));
83-
return channel.result.then(ignore);
90+
return _closeFuture = channel.result.then(ignore);
8491
}
8592

8693
/// Kills the isolate.

test/isolaterunner_test.dart

+12-4
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,23 @@ import 'package:test/test.dart';
1010
const _ms = Duration(milliseconds: 1);
1111

1212
void main() {
13-
test('create-close', testCreateClose);
13+
group('create-close', testCreateClose);
1414
test('create-run-close', testCreateRunClose);
1515
test('separate-isolates', testSeparateIsolates);
1616
group('isolate functions', testIsolateFunctions);
1717
}
1818

19-
Future testCreateClose() {
20-
return IsolateRunner.spawn().then((IsolateRunner runner) {
21-
return runner.close();
19+
void testCreateClose() {
20+
test('simple', () {
21+
return IsolateRunner.spawn().then((IsolateRunner runner) {
22+
return runner.close();
23+
});
24+
});
25+
test('close twice', () async {
26+
var runner = await IsolateRunner.spawn();
27+
await runner.close();
28+
// Shouldn't hang!
29+
await runner.close();
2230
});
2331
}
2432

0 commit comments

Comments
 (0)