net/http/internal/http3: do not discard response buffer on flush error

Consistent with Write, Flush should not discard a buffered response when
it fails to write to the underlying stream. Else, a server handler
writing small amounts (fits in buffer) of body that continuously flushes
it will never see an error, even if the write to the stream has been
failing.

For #70914

Change-Id: I16d53846e0cd64b0100372e1390aba8d6a6a6964
Reviewed-on: https://go-review.googlesource.com/c/go/+/848927
Reviewed-by: Damien Neil <dneil@google.com>
Auto-Submit: Nicholas Husin <nsh@golang.org>
Reviewed-by: Nicholas Husin <husin@google.com>
LUCI-TryBot-Result: golang-scoped@luci-project-accounts.iam.gserviceaccount.com <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
diff --git a/src/net/http/internal/http3/server.go b/src/net/http/internal/http3/server.go
index 56fe724..261ccd3 100644
--- a/src/net/http/internal/http3/server.go
+++ b/src/net/http/internal/http3/server.go
@@ -925,11 +925,10 @@
 	defer rw.mu.Unlock()
 	rw.writeHeaderLockedOnce()
 	if !rw.cannotHaveBody {
-		_, err := rw.bw.Write(rw.bb)
-		rw.bb.discard()
-		if err != nil {
+		if _, err := rw.bw.Write(rw.bb); err != nil {
 			return err
 		}
+		rw.bb.discard()
 	}
 	return rw.st.Flush()
 }
diff --git a/src/net/http/transport_test.go b/src/net/http/transport_test.go
index cd647e51..6386caf 100644
--- a/src/net/http/transport_test.go
+++ b/src/net/http/transport_test.go
@@ -2986,7 +2986,7 @@
 // Calling Close on a Response.Body used to just read until EOF.
 // Now it actually closes the TCP connection.
 func TestTransportCloseResponseBody(t *testing.T) {
-	runSynctest(t, testTransportCloseResponseBody, http3SkippedMode)
+	runSynctest(t, testTransportCloseResponseBody)
 }
 func testTransportCloseResponseBody(t *testing.T, mode testMode) {
 	writeErr := make(chan error, 1)