Index: generic/tclIO.c
==================================================================
--- generic/tclIO.c
+++ generic/tclIO.c
@@ -2412,12 +2412,10 @@
 				 * available to be written. */
     int written;		/* Amount of output data actually written in
 				 * current round. */
     int errorCode = 0;		/* Stores POSIX error codes from channel
 				 * driver operations. */
-    int wroteSome = 0;		/* Set to one if any data was written to the
-				 * driver. */
 
     /*
      * Prevent writing on a dead channel -- a channel that has been closed but
      * not yet deallocated. This can occur if the exit handler for the channel
      * deallocation runs before all channels are deregistered in all
@@ -2586,12 +2584,10 @@
 	     * queued.
 	     */
 
 	    DiscardOutputQueued(statePtr);
 	    continue;
-	} else {
-	    wroteSome = 1;
 	}
 
 	bufPtr->nextRemoved += written;
 
 	/*
@@ -2605,27 +2601,10 @@
 	    }
 	    RecycleBuffer(statePtr, bufPtr, 0);
 	}
     }	/* Closes "while (1)". */
 
-    /*
-     * If we wrote some data while flushing in the background, we are done.
-     * We can't finish the background flush until we run out of data and the
-     * channel becomes writable again. This ensures that all of the pending
-     * data has been flushed at the system level.
-     */
-
-    if (statePtr->flags & BG_FLUSH_SCHEDULED) {
-	if (wroteSome) {
-	    return errorCode;
-	} else if (statePtr->outQueueHead == NULL) {
-	    ResetFlag(statePtr, BG_FLUSH_SCHEDULED);
-	    (chanPtr->typePtr->watchProc)(chanPtr->instanceData,
-		    statePtr->interestMask);
-	}
-    }
-
     /*
      * If the channel is flagged as closed, delete it when the refCount drops
      * to zero, the output queue is empty and there is no output in the
      * current output buffer.
      */
@@ -2634,10 +2613,25 @@
 	    (statePtr->outQueueHead == NULL) &&
 	    ((statePtr->curOutPtr == NULL) ||
 	    IsBufferEmpty(statePtr->curOutPtr))) {
 	return CloseChannel(interp, chanPtr, errorCode);
     }
+
+    /*
+     * If we wrote some data while flushing in the background, we are done.
+     * We can't finish the background flush until we run out of data and the
+     * channel becomes writable again. This ensures that all of the pending
+     * data has been flushed at the system level.
+     */
+
+    if (statePtr->flags & BG_FLUSH_SCHEDULED) {
+	if (statePtr->outQueueHead == NULL) {
+	    ResetFlag(statePtr, BG_FLUSH_SCHEDULED);
+	    (chanPtr->typePtr->watchProc)(chanPtr->instanceData,
+		    statePtr->interestMask);
+	}
+    }
     return errorCode;
 }
 
 /*
  *----------------------------------------------------------------------

