From 38749636b9ab82f904854c1d1b7aae17130b5d74 Mon Sep 17 00:00:00 2001
From: Daniel Stenberg <daniel@haxx.se>
Date: Sat, 15 Nov 2014 22:17:55 +0100
Subject: [PATCH] multi: when leaving for timeout, close accordingly

Fixes the problem when a transfer in a pipeline times out.

Based-on-patch-by: Carlo Wood
---
 lib/multi.c | 15 +++++++++------
 1 file changed, 9 insertions(+), 6 deletions(-)

diff --git a/lib/multi.c b/lib/multi.c
index fddb4c5..71c3a16 100644
--- a/lib/multi.c
+++ b/lib/multi.c
@@ -934,10 +934,12 @@ static CURLMcode multi_runsingle(struct Curl_multi *multi,
   int control;
 
   if(!GOOD_EASY_HANDLE(data))
     return CURLM_BAD_EASY_HANDLE;
 
+  data->result = CURLE_OK;
+
   do {
     /* this is a single-iteration do-while loop just to allow a
        break to skip to the end of it */
     bool disconnect_conn = FALSE;
 
@@ -1007,20 +1009,21 @@ static CURLMcode multi_runsingle(struct Curl_multi *multi,
                   Curl_tvdiff(now, data->progress.t_startsingle),
                   k->bytecount);
           }
         }
 
-        /* Force the connection closed because the server could continue to
-           send us stuff at any time. (The disconnect_conn logic used below
-           doesn't work at this point). */
-        connclose(data->easy_conn, "Disconnected with pending data");
+        /* Force connection closed if the connection has indeed been used */
+        if(data->mstate >= CURLM_STATE_DO) {
+          connclose(data->easy_conn, "Disconnected with pending data");
+          disconnect_conn = TRUE;
+        }
         data->result = CURLE_OPERATION_TIMEDOUT;
-        multistate(data, CURLM_STATE_COMPLETED);
-        break;
       }
     }
 
+    if(!data->result)
+
     switch(data->mstate) {
     case CURLM_STATE_INIT:
       /* init this transfer. */
       data->result=Curl_pretransfer(data);
 
-- 
2.1.1

