daurnimator / lua-http

HTTP Library for Lua. Supports HTTP(S) 1.0, 1.1 and 2.0; client and server.
https://daurnimator.github.io/lua-http/
MIT License
778 stars 80 forks source link

request:go timeout doesn't work? #172

Open zhiburt opened 3 years ago

zhiburt commented 3 years ago

Issue

I was experimenting with Lua and follow your examples from documentation. And I've noticed that timeout on go function seems don't work from time to time... It works for certain cases but sometimes its not.

I had a simple script which does 5-10 request in a loop. And I've noticed that most of time it stuck on forever waiting of go method. The behavior is reproducible (I guess).

One strange thing happening when it stucks in most cases I got opened a page in firefox. (That's why I was thinking about redirecting here)

I do provide here an output of trace which I get after Ctrl+C. I do not provide an exact script and data since it seems to be unnecessary. (In the best case scenario every one can bump into this by creating a loop for 5-10 request. In the worst one it's a tricky issue. And most likely I am doing something wrong).

I am not sure if the issue is worth being overseeing. But I decided create an issue since I did spend a couple of hours haven't found any explanation for that behavior so far.

A basis of the script I use

for _, url in ipairs(urls) do
    req(url)
end

function req(url)
    local req = http_request.new_from_uri(url)
    req.follow_redirects = false

    local headers, stream = req:go(5)
    if headers == nil then
        return nil
    end
    if headers:get ":status" ~= "200" then
        return nil
    end

    local body = assert(stream:get_body_as_string())
    return body
end

most often traces

lua: /usr/share/lua/5.4/http/h2_connection.lua:253: interrupted!
stack traceback:
    /usr/share/lua/5.4/http/h2_connection.lua:372: in method 'read_http2_frame'
    /usr/share/lua/5.4/http/h2_connection.lua:253: in method 'step'
    /usr/share/lua/5.4/http/h2_stream.lua:1221: in method 'get_headers'
    /usr/share/lua/5.4/http/request.lua:592: in method 'go'
    main.lua:18: in local 'request'
    main.lua:42: in main chunk
    [C]: in ?
lua: /usr/share/lua/5.4/cqueues/socket.lua:103: interrupted!
stack traceback:
    [C]: in upvalue '_onerror'
    /usr/share/lua/5.4/cqueues/socket.lua:103: in method 'onerror'
    /usr/share/lua/5.4/cqueues/socket.lua:138: in upvalue 'oops'
    /usr/share/lua/5.4/cqueues/socket.lua:555: in method 'xread'
    /usr/share/lua/5.4/http/h2_connection.lua:361: in method 'read_http2_frame'
    /usr/share/lua/5.4/http/h2_connection.lua:253: in method 'step'
    /usr/share/lua/5.4/http/h2_stream.lua:1221: in method 'get_headers'
    /usr/share/lua/5.4/http/request.lua:592: in method 'go'
    main.lua:18: in local 'request'
    main.lua:42: in main chunk

too much CPU consumption?

Waiting consumes a lot of CPU

image

Thank you for the library.