Set closed flag *after* calling flush().
diff --git a/Lib/io.py b/Lib/io.py
index 72c9e17..ed94665 100644
--- a/Lib/io.py
+++ b/Lib/io.py
@@ -227,8 +227,10 @@
         'closed' property (see below) to test.
         """
         if not self.__closed:
-            self.__closed = True
-            self.flush()
+            try:
+                self.flush()
+            finally:
+                self.__closed = True
 
     def __del__(self) -> None:
         """Destructor.  Calls close()."""