37292: [CVS Core] Unable to synchronize - Cannot close connection
diff --git a/bundles/org.eclipse.team.core/src/org/eclipse/team/internal/core/streams/TimeoutOutputStream.java b/bundles/org.eclipse.team.core/src/org/eclipse/team/internal/core/streams/TimeoutOutputStream.java
index d220581..0041e6b 100644
--- a/bundles/org.eclipse.team.core/src/org/eclipse/team/internal/core/streams/TimeoutOutputStream.java
+++ b/bundles/org.eclipse.team.core/src/org/eclipse/team/internal/core/streams/TimeoutOutputStream.java
@@ -250,6 +250,8 @@
 					bytesUntilFlush = length;
 				}
 			}
+			
+			// If there are bytes to be written, write them
 			if (len != 0) {
 				// write out all remaining bytes from the buffer before flushing
 				try {
@@ -259,12 +261,9 @@
 				} catch (InterruptedIOException e) {
 					len = e.bytesTransferred;
 				}
-				synchronized (this) {
-					head = (head + len) % iobuffer.length;
-					length -= len;
-					notify();
-				}
 			}
+			
+			// If there was a pending flush, do it
 			if (bytesUntilFlush >= 0) {
 				bytesUntilFlush -= len;
 				if (bytesUntilFlush <= 0) {
@@ -276,6 +275,15 @@
 					bytesUntilFlush = -1; // might have been 0
 				}
 			}
+			
+			// If bytes were written, update the circular buffer
+			if (len != 0) {
+				synchronized (this) {
+					head = (head + len) % iobuffer.length;
+					length -= len;
+					notify();
+				}
+			}
 		}
 	}
 }