Subject: fix chan leak with http keepalive vs close (bug [6ca52aec14])

From: aspect <[email-masked]>
Date: Sun, 12 Feb 2017 12:57:29 +0000

---
 library/http/http.tcl | 7 ++++---
 tests/http.test       | 8 ++++++++
 2 files changed, 12 insertions(+), 3 deletions(-)

diff --git a/library/http/http.tcl b/library/http/http.tcl
index ccd4cd1..19799b9 100644
--- a/library/http/http.tcl
+++ b/library/http/http.tcl
@@ -197,9 +197,10 @@ proc http::Finish {token {errormsg ""} {skipCB 0}} {
 	set state(error) [list $errormsg $errorInfo $errorCode]
 	set state(status) "error"
     }
-    if {
-	($state(status) eq "timeout") || ($state(status) eq "error") ||
-	([info exists state(connection)] && ($state(connection) eq "close"))
+    if { ($state(status) eq "timeout") 
+       || ($state(status) eq "error")
+       || ([info exists state(-keepalive)] && !$state(-keepalive))
+       || ([info exists state(connection)] && ($state(connection) eq "close"))
     } {
         CloseSocket $state(sock) $token
     }
diff --git a/tests/http.test b/tests/http.test
index 12ad475..d7e42c2 100644
--- a/tests/http.test
+++ b/tests/http.test
@@ -592,6 +592,14 @@ test http-4.15 {http::Event} -body {
 } -cleanup {
     catch {http::cleanup $token}
 } -returnCodes 1 -match glob -result "couldn't open socket*"
+test http-1.15 {Leak with Close vs Keepalive (bug [6ca52aec14]} -body {
+    set before [chan names]
+    set token [http::geturl $url -headers {X-Connection keep-alive}]
+    http::cleanup $token
+    update
+    set after [chan names]
+    expr {$before eq $after}
+} -result 1
 
 test http-5.1 {http::formatQuery} {
     http::formatQuery name1 value1 name2 "value two"
-- 
cgit v0.12