Skip to content
Tcl Source Code History Of Ticket 6ca52aec14
Login

History Of Ticket 6ca52aec14

Artifacts Associated With Ticket 6ca52aec14

  1. Ticket change [a3fc0173ca] (rid 142526) by anonymous on 2015-07-17 05:23:22:

    1. assignee initialized to: "nobody"
    2. closer initialized to: "nobody"
    3. cmimetype initialized to: "text/plain"
    4. comment initialized to:
      Hello,
      The 'geturl' command of the 'http' package does not free up properly the used variables if geturl if a GET request is made and the '-keepalive' option is not used ('Connection: close' is transmitted), but if the server responses (by mistake) with 'Connection: keep-alive'.
      
      After running 10 times ...
      
         set Handle [::http::geturl "http://localhost:8080/c"]
         puts "$k:\n  Handle:$Handle\n  Status: [::http::status $Handle]\n  Data: [http::data $Handle]"
         ::http::cleanup $Handle
      
      ... and having the server responding with 'Connection: keep-alive' the following variables will be defined in the http namespace (info vars http::*):
      
         ::http::http ::http::urlTypes ::http::1 ::http::encodings ::http::2 ::http::3 ::http::4 ::http::5 ::http::6 ::http::7 ::http::8 ::http::9 ::http::strict ::http::formMap ::http::defaultKeepalive ::http::socketmap ::http::defaultCharset
      
      The variables 1..9 are the temporary variables that should be deleted with the 'http::cleanup' command.
      
      Below there is a test case composed by 2 parts (files):
      
      * HTTP server: It opens and listens the local port 8080. It accepts 3 requests (http://localhost:8080/a, http://localhost:8080/b, http://localhost:8080/c) that are responded in 3 different ways: Request 'a' will not return any 'Connection' attribute, request 'b' will return 'Connection: close', and request 'c' will return 'Connection: keep-alive'.
      * HTTP client: It performs for each of the 3 requests accepted by the HTTP server 10 requests. After each request the variables in the 'http' namespace are printed.
      
      ------------------------- File 'Run MemoryLeak_TestCase_Server.tcl' -------------------------
      
      namespace eval HttpServer {
      	proc Start {port} {
      		puts "HttpServer::Start $port"
      		set Server [socket -server [namespace current]::Accept $port]
      	}
      
      	proc Accept {sock host port} {
      		puts "HttpServer::Accept $sock $host $port"
      		fconfigure $sock -blocking 0 -buffering none
      		fileevent $sock readable [list [namespace current]::Handle $sock]
      	}
      
      	proc Handle {Socket} {
      		puts "HttpServer::Handle $Socket"
      		if {[eof $Socket]} {
      			puts "  eof->close socket"
      			close $Socket
      			return
      		}
      
      		puts "  Read data"
      		set Data [read $Socket]
      		regexp -line {^(.*)$} $Data {} FirstLine
      		regsub -all -line {^} $Data {   -> } Data
      		puts $Data
      
      		if {[regexp {GET /(.*) HTTP/1.1} $FirstLine {} GetArgs]} {
      			puts "  GetArgs:$GetArgs"
      			puts "  Return data"
      			if {$GetArgs=="a"} {
      				puts $Socket "HTTP/1.1 200 OK"
      				puts $Socket ""
      				puts $Socket "Data received on [clock seconds] - a"
      			} elseif {$GetArgs=="b"} {
      				puts $Socket "HTTP/1.1 200 OK"
      				puts $Socket "Connection: close"
      				puts $Socket ""
      				puts $Socket "Data received on [clock seconds] - b"
      			} elseif {$GetArgs=="c"} {
      				puts $Socket "HTTP/1.1 200 OK"
      				puts $Socket "Connection: keep-alive"
      				puts $Socket ""
      				puts $Socket "Data received on [clock seconds] - c"
      			} else {
      				puts $Socket "HTTP/1.1 404 not found"
      				puts $Socket ""
      				puts $Socket "<html><h1>404 - Not found '$GetArgs'</h1></html>"
      			}
      		} else {
      			puts $Socket "HTTP/1.1 400 bad request"
      			puts $Socket ""
      			puts $Socket "<html><h1>400 - Bad request</h1></html>"
      		}
      
      		puts "  Close socket $Socket"
      		close $Socket
      	}
      }; # end namespace HttpServer
      
      HttpServer::Start 8080
      
      ------------------------- File 'Run MemoryLeak_TestCase_Client.tcl' -------------------------
      
      package require http
      # source ./http-2.8.5.tm
      
      proc ListVarHttp {} {
      	puts "HttpVars([llength [info vars http::*]]): [info vars http::*]"
      }
      
      ListVarHttp
      
      puts "GetUrl http://localhost:8080/a"
      for {set k 0} {$k<10} {incr k} {
      	set Handle [::http::geturl "http://localhost:8080/a"]
      	puts "$k:\n  Handle:$Handle\n  Status: [::http::status $Handle]\n  Data: [http::data $Handle]"
      	::http::cleanup $Handle
      
      	ListVarHttp
      }
      
      puts "GetUrl http://localhost:8080/b"
      for {set k 0} {$k<10} {incr k} {
      	set Handle [::http::geturl "http://localhost:8080/b"]
      	puts "$k:\n  Handle:$Handle\n  Status: [::http::status $Handle]\n  Data: [http::data $Handle]"
      	::http::cleanup $Handle
      
      	ListVarHttp
      }
      
      puts "GetUrl http://localhost:8080/c"
      for {set k 0} {$k<10} {incr k} {
      	set Handle [::http::geturl "http://localhost:8080/c"]
      	puts "$k:\n  Handle:$Handle\n  Status: [::http::status $Handle]\n  Data: [http::data $Handle]"
      	::http::cleanup $Handle
      
      	ListVarHttp
      }
      
    5. foundin initialized to: "HTTP package 2.7.10 and 2.8.5"
    6. is_private initialized to: "0"
    7. login: "anonymous"
    8. priority initialized to: "5 Medium"
    9. private_contact initialized to: "c7f3b5a0831cb53da82e8d0adc0c229e226e82cc"
    10. resolution initialized to: "None"
    11. severity initialized to: "Minor"
    12. status initialized to: "Open"
    13. submitter initialized to: "anonymous"
    14. subsystem initialized to: "29. http Package"
    15. title initialized to:
      HTTP package: Memory leak if client requests "Connection: close" but server responses with "Connection: keep-alive"
      
    16. type initialized to: "Bug"
  2. Ticket change [effa6f3340] (rid 142551) by aspect on 2015-07-20 04:15:57:

    1. icomment:
      The following attempts to test more concisely:
      
      ----
      package require http
      #source patch.tcl
      package require coroutine
      
      proc go {args} {
          tailcall coroutine ::goro#[llength [info commands ::goro#*]] {*}$args
      }
      
      proc accept {chan host port} {
          fconfigure $chan -blocking 0 -buffering none
          fileevent $chan readable [info coroutine]
          catch {
              while {![eof $chan]} {
                  while {[coroutine::util gets $chan header] > 0} {
                      # discard headers
                  }
                  foreach line {
                      "HTTP/1.1 200 OK"
                      "Connection: keep-alive"
                      "Content-length: 15"
                      ""
                      "<html></html>"
                  } {
                      puts $chan $line
                  }
              }
          }
          puts "$chan returning"
          catch {close $chan}
      }
      
      set port 8080
      while {[catch {socket -server {go accept} 8080} server]} {
          incr port
      }
      puts "listening on $port"
      
      set before [list [info vars ::http::*] [chan names]]
      set tok [::http::geturl http://localhost:$port/c]
      ::http::cleanup $tok
      update  ;# allow events to complete
      set after [list [info vars ::http::*] [chan names]]
      if {$before eq $after} {} else {
          puts "ERROR: before $before"
          puts "ERROR: after  $after"
      }
      ----
      
      A possible fix appears to be in ::http::Finish, checking that state(-keepalive) exists and is true before testing state(connection):
      
      ----
      --- library/http/http.tcl       2015-07-02 13:15:03.723694211 +1000
      +++ library/http/http.tcl.patched       2015-07-20 14:13:43.063724688 +1000
      @@ -197,9 +197,10 @@
              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
           }
      ----
      
    2. login: "aspect"
    3. mimetype: "text/plain"
  3. Ticket change [e9a2281c8b] (rid 142558) by anonymous on 2015-07-20 15:35:13:

    1. icomment:
      The patch suggested by 'aspect' on 2015-07-20 04:15:57 corrects the memory leakage issue of the http package versions 2.7.10 and 2.8.5.
      
      Since the http package 2.8(.5) requires Tcl version 8.6 it would be good to patch also the http package 2.7(.10). This allows using a corrected http package also with Tcl 8.5.
      
    2. login: "anonymous"
    3. mimetype: "text/plain"
  4. Ticket change [7c4aa22bf6] (rid 142559) by anonymous on 2015-07-20 15:37:36:

    1. comment changed to:
      Hello,
      The 'geturl' command of the 'http' package does not free up properly the used variables if geturl if a GET request is made and the '-keepalive' option is not used ('Connection: close' is transmitted), but if the server responses (by mistake) with 'Connection: keep-alive'.
      
      After running 10 times ...
      
         set Handle [::http::geturl "http://localhost:8080/c"]
         puts "$k:\n  Handle:$Handle\n  Status: [::http::status $Handle]\n  Data: [http::data $Handle]"
         ::http::cleanup $Handle
      
      ... and having the server responding with 'Connection: keep-alive' the following variables will be defined in the http namespace (info vars http::*):
      
         ::http::http ::http::urlTypes ::http::1 ::http::encodings ::http::2 ::http::3 ::http::4 ::http::5 ::http::6 ::http::7 ::http::8 ::http::9 ::http::strict ::http::formMap ::http::defaultKeepalive ::http::socketmap ::http::defaultCharset
      
      The variables 1..9 are the temporary variables that should be deleted with the 'http::cleanup' command.
      
      Below there is a test case composed by 2 parts (files):
      
      * HTTP server: It opens and listens the local port 8080. It accepts 3 requests (http://localhost:8080/a, http://localhost:8080/b, http://localhost:8080/c) that are responded in 3 different ways: Request 'a' will not return any 'Connection' attribute, request 'b' will return 'Connection: close', and request 'c' will return 'Connection: keep-alive'.
      * HTTP client: It performs for each of the 3 requests accepted by the HTTP server 10 requests. After each request the variables in the 'http' namespace are printed.
      
      ------------------------- File 'Run MemoryLeak_TestCase_Server.tcl' -------------------------
      
      namespace eval HttpServer {
      	proc Start {port} {
      		puts "HttpServer::Start $port"
      		set Server [socket -server [namespace current]::Accept $port]
      	}
      
      	proc Accept {sock host port} {
      		puts "HttpServer::Accept $sock $host $port"
      		fconfigure $sock -blocking 0 -buffering none
      		fileevent $sock readable [list [namespace current]::Handle $sock]
      	}
      
      	proc Handle {Socket} {
      		puts "HttpServer::Handle $Socket"
      		if {[eof $Socket]} {
      			puts "  eof->close socket"
      			close $Socket
      			return
      		}
      
      		puts "  Read data"
      		set Data [read $Socket]
      		regexp -line {^(.*)$} $Data {} FirstLine
      		regsub -all -line {^} $Data {   -> } Data
      		puts $Data
      
      		if {[regexp {GET /(.*) HTTP/1.1} $FirstLine {} GetArgs]} {
      			puts "  GetArgs:$GetArgs"
      			puts "  Return data"
      			if {$GetArgs=="a"} {
      				puts $Socket "HTTP/1.1 200 OK"
      				puts $Socket "Content-length: 31"
      				puts $Socket ""
      				puts $Socket "Data received on [clock seconds] - a"
      			} elseif {$GetArgs=="b"} {
      				puts $Socket "HTTP/1.1 200 OK"
      				puts $Socket "Content-length: 31"
      				puts $Socket "Connection: close"
      				puts $Socket ""
      				puts $Socket "Data received on [clock seconds] - b"
      			} elseif {$GetArgs=="c"} {
      				puts $Socket "HTTP/1.1 200 OK"
      				puts $Socket "Content-length: 31"
      				puts $Socket "Connection: keep-alive"
      				puts $Socket ""
      				puts $Socket "Data received on [clock seconds] - c"
      			} else {
      				puts $Socket "HTTP/1.1 404 not found"
      				puts $Socket ""
      				puts $Socket "<html><h1>404 - Not found '$GetArgs'</h1></html>"
      			}
      		} else {
      			puts $Socket "HTTP/1.1 400 bad request"
      			puts $Socket ""
      			puts $Socket "<html><h1>400 - Bad request</h1></html>"
      		}
      
      		puts "  Close socket $Socket"
      		close $Socket
      	}
      }; # end namespace HttpServer
      
      HttpServer::Start 8080
      
      ------------------------- File 'Run MemoryLeak_TestCase_Client.tcl' -------------------------
      
      package require http
      # source ./http-2.8.5.tm
      
      proc ListVarHttp {} {
      	puts "HttpVars([llength [info vars http::*]]): [info vars http::*]"
      }
      
      ListVarHttp
      
      puts "GetUrl http://localhost:8080/a"
      for {set k 0} {$k<10} {incr k} {
      	set Handle [::http::geturl "http://localhost:8080/a"]
      	puts "$k:\n  Handle:$Handle\n  Status: [::http::status $Handle]\n  Data: [http::data $Handle]"
      	::http::cleanup $Handle
      
      	ListVarHttp
      }
      
      puts "GetUrl http://localhost:8080/b"
      for {set k 0} {$k<10} {incr k} {
      	set Handle [::http::geturl "http://localhost:8080/b"]
      	puts "$k:\n  Handle:$Handle\n  Status: [::http::status $Handle]\n  Data: [http::data $Handle]"
      	::http::cleanup $Handle
      
      	ListVarHttp
      }
      
      puts "GetUrl http://localhost:8080/c"
      for {set k 0} {$k<10} {incr k} {
      	set Handle [::http::geturl "http://localhost:8080/c"]
      	puts "$k:\n  Handle:$Handle\n  Status: [::http::status $Handle]\n  Data: [http::data $Handle]"
      	::http::cleanup $Handle
      
      	ListVarHttp
      }
      
    2. login: "anonymous"
    3. mimetype: "text/plain"
  5. Ticket change [e54706a469] (rid 142871) by anonymous on 2015-08-19 19:38:13:

    1. icomment:
      Small correction of the test script provided by 'aspect' on 2015-07-20 04:15:57: The socket opening loop should be:
      
      set port 8080
      while {[catch {socket -server {go accept} $port} server]} {
          incr port }
      
      But the proposed fix works well.
      
    2. login: "anonymous"
    3. mimetype: "text/plain"
  6. Ticket change [c78545c150] (rid 151892) by aspect on 2017-02-12 13:02:48:

    1. icomment:
      patch pushed to branch [bug-0520d17284].
      
      8.6 only - the channel leak did not disappear when I tried it on 8.5; perhaps a deeper problem there since I don't think 8.5 has proper HTTP/1.1 support (?).
      
    2. login: "aspect"
    3. mimetype: "text/plain"
  7. Ticket change [76c4d60e3f] (rid 153651) by jan.nijtmans on 2017-05-09 11:31:57:

    1. assignee changed to: "jan.nijtmans"
    2. icomment: "Fixed in core-8-6-branch and trunk."
    3. login: "jan.nijtmans"
    4. mimetype: "text/plain"
    5. resolution changed to: "Fixed"
  8. Ticket change [7275c71c69] (rid 153652) by jan.nijtmans on 2017-05-09 11:33:37:

    1. closedate changed to: "2457882.98168014"
    2. closer changed to: "jan.nijtmans"
    3. login: "jan.nijtmans"
    4. mimetype: "text/plain"
    5. status changed to: "Closed"
  9. Ticket change [fdba6cdf28] (rid 153837) by aspect on 2017-05-19 15:10:11:

    1. closedate changed to: "2457893.13208098"
    2. closer changed to: "aspect"
    3. icomment:
      Emiliano reported in the chat a test failure on netbsd of http-4.16 (introduced by [8d81ec63d5d] but incorrectly called http-1.15).
      
      I can't see how it's failing, but in any case the test doesn't do what it says on the tin:  -headers {X-Connection keep-alive}  just gets echoed back by tests/httpd (with "X-" intact), and in any case it closes the socket immediately.
      
      Test httpd-4.16 is useless, and I suggest simply removing it - the alternative is to rework tests/httpd to support keepalive, which is both more effort and liable to cause more rework.
      
    4. login: "aspect"
    5. mimetype: "text/plain"