lua-resty-redis icon indicating copy to clipboard operation
lua-resty-redis copied to clipboard

attempt to send data on a closed socket

Open pennkao opened this issue 6 years ago • 5 comments
trafficstars

local confRedis = { host = "127.0.0.1", port = "6379", password = "Lp8^w#H$r43@", timeout = 1000, max_idle_timeout = 1000*600, pool_size = 100 } function connectRedis() local rds, err = redis:new() if not rds then ngx.log(ngx.ERR, "redis error: ", err) return nil end

    rds:set_timeouts(confRedis.timeout, confRedis.timeout, confRedis.timeout)
    ok, err = rds:connect(confRedis.host, confRedis.port)
    if not ok then
            ngx.log(ngx.ERR, "redis connect error: ", err)
            rds:close()
            return nil
    end

    local res, err = rds:auth(confRedis.password)
    if not res then
            ngx.log(ngx.ERR, "failed to authenticate: ", err)
            rds:close()
            return nil
    end
    return rds

end function closeRedis(red)

    if not red then
            return
    end

    local times, err = red:get_reused_times()
    ngx.log(ngx.ERR, "tttttttttttt: ", times, ":", err)

    local ok, err = red:set_keepalive(confRedis.pool_max_idle_time, confRedis.pool_size)
    if not ok then
            ngx.log(ngx.ERR, "close redis: ", err)
            return
    end

end function getCertRedis(domain)

    local pemKey = domain .. ":pem"
    local keyKey = domain .. ":key"
    local rds = connectRedis()
    if rds == nil then
            ngx.log(ngx.ERR, domain, ",", "redis connect nil")
            return nil, nil
    end

    local pem, err = rds:get(pemKey)
    if err ~= nil then
            ngx.log(ngx.ERR, domain, ",", "get pem from redis nil", err)
            return nil, nil
    end
    local key, err = rds:get(keyKey)
    if err ~= nil then
            ngx.log(ngx.ERR, domain, ",", "get key from redis nil", err)
            return nil, nil
    end

    if pem == ngx.null or key == ngx.null then
            closeRedis(rds)
            return nil, nil
    end

    closeRedis(rds)
    return pem, key

end function setCertRedis(domain, pemData, keyData)

    if domain == nil or pemData == nil or keyData == nil then
            return false
    end

    local pemKey = domain .. ":pem"
    local keyKey = domain .. ":key"
    local rds = connectRedis()
    if rds == nil then
            return false
    end

    local ok, err = rds:set(pemKey, pemData)
    if not ok then
            ngx.log(ngx.ERR, domain, ",", "cache cert pem error,", err)
            return false
    end

    local ok, err = rds:set(keyKey, keyData)
    if not ok then
            ngx.log(ngx.ERR, domain, ",", "cache cert key error,", err)
            return false
    end

    closeRedis(rds)

    return true

end

errlog: 2019/08/17 11:57:25 [error] 13260#0: 63910 attempt to send data on a closed socket: u:0000000000000000, c:0000000000000000, ft:0 eof:0, context: ssl_certificate_by_lua, client: 106.14.115.254, server: 0.0.0.0:443

pennkao avatar Aug 17 '19 04:08 pennkao

Weirdly I have had similar issues, and found this https://stackoverflow.com/questions/57124285/connection-in-redis-and-lua where one answer suggested reverting to using set_timeout instead. The docs recommend set_timeouts, but this seems to have been my issue

cmac4603 avatar Oct 21 '19 15:10 cmac4603

I just encountered the same issue, using set_timeout solved it... I'm guessing the issue is in lua-nginx-module and not here, can anyone have a look at this?

erankor avatar Jan 07 '21 09:01 erankor