From 39ecf5f9d298e01ebb992cc7fb7350e25ffaeef6 Mon Sep 17 00:00:00 2001 From: William Hesse Date: Mon, 20 Mar 2023 15:33:04 +0000 Subject: [PATCH] [io] Calling flush after close on File.open's IOSync should not throw an exception The documentation of IOSync states that calling flush on a closed IOSink may have no effect: https://github.com/dart-lang/sdk/blob/main/sdk/lib/io/io_sink.dart#L109 The future returned by close in the new implementation does not complete until after a call to flush has completed. The previous implementation of File.open did not throw when calling flush after close. CoreLibraryReviewExempt: IO only, restoring previous semantics Bug: b/274405250 Change-Id: I56bbe02e886085cc8156e60264f5b77593c8a075 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/289823 Reviewed-by: Emmanuel Pellereau --- sdk/lib/io/file_impl.dart | 2 +- tests/standalone/io/file_iosink_test.dart | 9 ++------- tests/standalone_2/io/file_iosink_test.dart | 9 ++------- 3 files changed, 5 insertions(+), 15 deletions(-) diff --git a/sdk/lib/io/file_impl.dart b/sdk/lib/io/file_impl.dart index 66089672b29..0b66b09c760 100644 --- a/sdk/lib/io/file_impl.dart +++ b/sdk/lib/io/file_impl.dart @@ -439,7 +439,7 @@ class _RandomAccessFileIOSync implements IOSink { Future flush() { if (_isClosed) { - throw StateError("${_file?.path ?? "IOSink"} is closed"); + return done; } if (_isBound) { throw StateError("${_file?.path ?? "IOSink"} is bound to a stream"); diff --git a/tests/standalone/io/file_iosink_test.dart b/tests/standalone/io/file_iosink_test.dart index c4cf4969d32..511697f126f 100644 --- a/tests/standalone/io/file_iosink_test.dart +++ b/tests/standalone/io/file_iosink_test.dart @@ -392,13 +392,8 @@ Future testFlushDuringClose(Directory tmpDir) async { final sink = file.openWrite(); final close = sink.close(); - try { - await sink.flush(); - } on StateError catch (e) { - Expect.contains('flush_during_close is closed', e.message); - } - - await close; + Expect.equals(file, await sink.flush()); + Expect.equals(file, await close); } Future testFailedWrite(Directory tmpDir) async { diff --git a/tests/standalone_2/io/file_iosink_test.dart b/tests/standalone_2/io/file_iosink_test.dart index cb57eb9fa03..2518b8939aa 100644 --- a/tests/standalone_2/io/file_iosink_test.dart +++ b/tests/standalone_2/io/file_iosink_test.dart @@ -394,13 +394,8 @@ Future testFlushDuringClose(Directory tmpDir) async { final sink = file.openWrite(); final close = sink.close(); - try { - await sink.flush(); - } on StateError catch (e) { - Expect.contains('flush_during_close is closed', e.message); - } - - await close; + Expect.equals(file, await sink.flush()); + Expect.equals(file, await close); } Future testFailedWrite(Directory tmpDir) async {