util.async: Improve debug logging in a few places
authorMatthew Wild <mwild1@gmail.com>
Tue, 21 Nov 2023 18:12:49 +0000
changeset 13335 5206314d6c70
parent 13334 49ecfb070240
child 13336 17cb965e55b7
util.async: Improve debug logging in a few places Knowing the state of the coroutine as well as the runner state can be helpful.
util/async.lua
--- a/util/async.lua	Tue Nov 21 18:11:40 2023 +0000
+++ b/util/async.lua	Tue Nov 21 18:12:49 2023 +0000
@@ -46,7 +46,9 @@
 local function runner_continue(thread)
 	-- ASSUMPTION: runner is in 'waiting' state (but we don't have the runner to know for sure)
 	if coroutine.status(thread) ~= "suspended" then -- This should suffice
-		log("error", "unexpected async state: thread not suspended");
+		log("error", "unexpected async state: thread not suspended (%s, %s)", thread, coroutine.status(thread));
+		-- Fetching the traceback is likely to *crash* if a C library is calling us while suspended
+		--log("error", "coroutine stack: %s", debug.traceback());
 		return false;
 	end
 	local ok, state, runner = coroutine.resume(thread);
@@ -198,6 +200,7 @@
 		-- Loop through queue items, and attempt to run them
 		for i = 1,n do
 			local queued_input = q[i];
+			self:log("Resuming thread with new item [%s]", thread);
 			local ok, new_state = coroutine.resume(thread, queued_input);
 			if not ok then
 				-- There was an error running the coroutine, save the error, mark runner as ready to begin again
@@ -225,7 +228,7 @@
 	-- Runner processed all items it can, so save current runner state
 	self.state = state;
 	if err or state ~= self.notified_state then
-		self:log("debug", "changed state from %s to %s", self.notified_state, err and ("error ("..state..")") or state);
+		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));
 		if err then
 			state = "error"
 		else