Comparison

util/async.lua @ 13331:5206314d6c70

util.async: Improve debug logging in a few places Knowing the state of the coroutine as well as the runner state can be helpful.
author Matthew Wild <mwild1@gmail.com>
date Tue, 21 Nov 2023 18:12:49 +0000
parent 13330:49ecfb070240
child 13332:17cb965e55b7
comparison
equal deleted inserted replaced
13330:49ecfb070240 13331:5206314d6c70
44 end 44 end
45 45
46 local function runner_continue(thread) 46 local function runner_continue(thread)
47 -- ASSUMPTION: runner is in 'waiting' state (but we don't have the runner to know for sure) 47 -- ASSUMPTION: runner is in 'waiting' state (but we don't have the runner to know for sure)
48 if coroutine.status(thread) ~= "suspended" then -- This should suffice 48 if coroutine.status(thread) ~= "suspended" then -- This should suffice
49 log("error", "unexpected async state: thread not suspended"); 49 log("error", "unexpected async state: thread not suspended (%s, %s)", thread, coroutine.status(thread));
50 -- Fetching the traceback is likely to *crash* if a C library is calling us while suspended
51 --log("error", "coroutine stack: %s", debug.traceback());
50 return false; 52 return false;
51 end 53 end
52 local ok, state, runner = coroutine.resume(thread); 54 local ok, state, runner = coroutine.resume(thread);
53 if not ok then 55 if not ok then
54 local err = state; 56 local err = state;
196 while n > 0 and state == "ready" and not err do 198 while n > 0 and state == "ready" and not err do
197 local consumed; 199 local consumed;
198 -- Loop through queue items, and attempt to run them 200 -- Loop through queue items, and attempt to run them
199 for i = 1,n do 201 for i = 1,n do
200 local queued_input = q[i]; 202 local queued_input = q[i];
203 self:log("Resuming thread with new item [%s]", thread);
201 local ok, new_state = coroutine.resume(thread, queued_input); 204 local ok, new_state = coroutine.resume(thread, queued_input);
202 if not ok then 205 if not ok then
203 -- There was an error running the coroutine, save the error, mark runner as ready to begin again 206 -- There was an error running the coroutine, save the error, mark runner as ready to begin again
204 consumed, state, err = i, "ready", debug.traceback(thread, new_state); 207 consumed, state, err = i, "ready", debug.traceback(thread, new_state);
205 self.thread = nil; 208 self.thread = nil;
223 n = #q; 226 n = #q;
224 end 227 end
225 -- Runner processed all items it can, so save current runner state 228 -- Runner processed all items it can, so save current runner state
226 self.state = state; 229 self.state = state;
227 if err or state ~= self.notified_state then 230 if err or state ~= self.notified_state then
228 self:log("debug", "changed state from %s to %s", self.notified_state, err and ("error ("..state..")") or state); 231 self:log("debug", "changed state from %s to %s [%s %s]", self.notified_state, err and ("error ("..state..")") or state, self.thread, self.thread and coroutine.status(self.thread));
229 if err then 232 if err then
230 state = "error" 233 state = "error"
231 else 234 else
232 self.notified_state = state; 235 self.notified_state = state;
233 end 236 end