Avoid crashing on closed port in IsolateDriverConnection (#47)

Previously we would crash with an exception in the proto code (trying to
call `length` on `null`). Instead we'll now return null if there is
nothing to be read.
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 5aeb331..7f74ad1 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -1,3 +1,8 @@
+## 0.1.24
+
+* Check for closed port when trying to read a response in
+  `IsolateDriverConnection` and return `null` if there is nothing to be read.
+
 ## 0.1.23+1
 
 * Don't rely on `exitCode` to know when a worker terminates, instead wait for
diff --git a/lib/src/driver/driver_connection.dart b/lib/src/driver/driver_connection.dart
index b282aec..9e6f4db 100644
--- a/lib/src/driver/driver_connection.dart
+++ b/lib/src/driver/driver_connection.dart
@@ -103,7 +103,9 @@
 
   @override
   Future<WorkResponse> readResponse() async {
-    await _receivePortIterator.moveNext();
+    if (!await _receivePortIterator.moveNext()) {
+      return null;
+    }
     return WorkResponse.fromBuffer(_receivePortIterator.current as List<int>);
   }
 
diff --git a/pubspec.yaml b/pubspec.yaml
index dbae4cd..e8ed964 100644
--- a/pubspec.yaml
+++ b/pubspec.yaml
@@ -1,5 +1,5 @@
 name: bazel_worker
-version: 0.1.23+1
+version: 0.1.24
 
 description: Tools for creating a bazel persistent worker.
 homepage: https://github.com/dart-lang/bazel_worker
diff --git a/test/driver_connection_test.dart b/test/driver_connection_test.dart
new file mode 100644
index 0000000..afb5ae5
--- /dev/null
+++ b/test/driver_connection_test.dart
@@ -0,0 +1,23 @@
+// Copyright (c) 2020, the Dart project authors.  Please see the AUTHORS file
+// for details. All rights reserved. Use of this source code is governed by a
+// BSD-style license that can be found in the LICENSE file.
+
+import 'dart:isolate';
+import 'package:test/test.dart';
+
+import 'package:bazel_worker/src/driver/driver_connection.dart';
+
+void main() {
+  group('IsolateDriverConnection', () {
+    test('handles closed port', () async {
+      var isolatePort = ReceivePort();
+      var outsidePort = ReceivePort();
+      isolatePort.sendPort.send(outsidePort.sendPort);
+      var connection = await IsolateDriverConnection.create(isolatePort);
+
+      isolatePort.close();
+
+      expect(await connection.readResponse(), null);
+    });
+  });
+}
diff --git a/test/test_all.dart b/test/test_all.dart
index d0e63be..8219d96 100644
--- a/test/test_all.dart
+++ b/test/test_all.dart
@@ -5,9 +5,11 @@
 import 'driver_test.dart' as driver;
 import 'message_grouper_test.dart' as message_grouper;
 import 'worker_loop_test.dart' as worker_loop;
+import 'driver_connection_test.dart' as driver_connection;
 
 void main() {
   driver.main();
   message_grouper.main();
   worker_loop.main();
+  driver_connection.main();
 }