fix freeze when register for KOSync on Kindle

This commit is contained in:
chrox
2015-03-21 13:18:34 +08:00
parent 82393f0ed3
commit 8836225cec
4 changed files with 26 additions and 9 deletions

View File

@@ -3,7 +3,6 @@ local DEBUG = require("dbg")
local HTTPClient = {
input_timeouts = 0,
INPUT_TIMEOUT = 100*1000,
}
function HTTPClient:new()
@@ -19,7 +18,7 @@ function HTTPClient:request(request, response_callback)
UIManager:initLooper()
UIManager.looper:add_callback(function()
-- avoid endless waiting for input
UIManager.INPUT_TIMEOUT = self.INPUT_TIMEOUT
UIManager:setInputTimeout()
self.input_timeouts = self.input_timeouts + 1
local turbo = require("turbo")
-- disable success and warning logs
@@ -27,9 +26,9 @@ function HTTPClient:request(request, response_callback)
turbo.log.categories.warning = false
local client = turbo.async.HTTPClient({verify_ca = "none"})
local res = coroutine.yield(client:fetch(request.url, request))
-- reset INPUT_TIMEOUT to nil when all HTTP requests are fullfilled.
self.input_timeouts = self.input_timeouts - 1
UIManager.INPUT_TIMEOUT = self.input_timeouts > 0 and self.INPUT_TIMEOUT or nil
-- reset INPUT_TIMEOUT to nil when all HTTP requests are fullfilled.
if self.input_timeouts == 0 then UIManager:resetInputTimeout() end
if response_callback then
response_callback(res)
end

View File

@@ -436,6 +436,14 @@ function UIManager:_repaint()
self.refresh_counted = false
end
function UIManager:setInputTimeout(timeout)
self.INPUT_TIMEOUT = timeout or 200*1000
end
function UIManager:resetInputTimeout()
self.INPUT_TIMEOUT = nil
end
function UIManager:handleInput()
local wait_until, now
-- run this in a loop, so that paints can trigger events
@@ -500,7 +508,17 @@ function UIManager:handleInput()
if self.looper then
DEBUG("handle input in turbo I/O looper")
self.looper:add_callback(function() self:handleInput() end)
self.looper:add_callback(function()
-- FIXME: force close looper when there is unhandled error,
-- otherwise the looper will hang. Any better solution?
xpcall(function() self:handleInput() end, function(err)
io.stderr:write(err .. "\n")
io.stderr:write(debug.traceback() .. "\n")
io.stderr:flush()
self.looper:close()
os.exit(1)
end)
end)
end
end

View File

@@ -117,7 +117,7 @@ function KOSyncClient:update_progress(username, password,
end)
self.client:enable("AsyncHTTP", {thread = co})
coroutine.resume(co)
if UIManager.looper then UIManager.INPUT_TIMEOUT = 100 end
if UIManager.looper then UIManager:setInputTimeout() end
end
function KOSyncClient:get_progress(username, password,
@@ -144,7 +144,7 @@ function KOSyncClient:get_progress(username, password,
end)
self.client:enable("AsyncHTTP", {thread = co})
coroutine.resume(co)
if UIManager.looper then UIManager.INPUT_TIMEOUT = 100 end
if UIManager.looper then UIManager:setInputTimeout() end
end
return KOSyncClient

View File

@@ -162,7 +162,7 @@ function KOSync:doRegister(username, password)
})
else
UIManager:show(InfoMessage:new{
text = _(body.message or "Unknown server error"),
text = _(body and body.message or "Unknown server error"),
})
end
end
@@ -191,7 +191,7 @@ function KOSync:doLogin(username, password)
})
else
UIManager:show(InfoMessage:new{
text = _(body.message or "Unknown server error"),
text = _(body and body.message or "Unknown server error"),
})
end
end