fix tsched crashing when a process gets removed
when a process removes another process, the amount of processes tsched has decrements, but the for loop doesn't update. a check has been implemented to check for when the task that's getting looked up doesn't exist.
This commit is contained in:
+124
-122
@@ -14,139 +14,141 @@ local gpu = component.gpu
|
|||||||
local log = require("log")
|
local log = require("log")
|
||||||
|
|
||||||
function _G._PUBLIC.tsched.runAsTask(path, ...)
|
function _G._PUBLIC.tsched.runAsTask(path, ...)
|
||||||
local args = { ... }
|
local args = { ... }
|
||||||
local function taskFunction()
|
local function taskFunction()
|
||||||
local result, errorMessage = xpcall(function(...)
|
local result, errorMessage = xpcall(function(...)
|
||||||
local args = table.pack(...)
|
local args = table.pack(...)
|
||||||
if not filesystem.exists(path) then
|
if not filesystem.exists(path) then
|
||||||
error("No such file: " .. path)
|
error("No such file: " .. path)
|
||||||
end
|
end
|
||||||
local handle, data, tmpdata = filesystem.open(path), "", nil
|
local handle, data, tmpdata = filesystem.open(path), "", nil
|
||||||
repeat
|
repeat
|
||||||
tmpdata = handle:read(math.huge or math.maxinteger)
|
tmpdata = handle:read(math.huge or math.maxinteger)
|
||||||
data = data .. (tmpdata or "")
|
data = data .. (tmpdata or "")
|
||||||
until not tmpdata
|
until not tmpdata
|
||||||
handle:close()
|
handle:close()
|
||||||
|
|
||||||
-- Userland environment definition
|
-- Userland environment definition
|
||||||
local userland = table.copy(_PUBLIC)
|
local userland = table.copy(_PUBLIC)
|
||||||
userland._G = userland
|
userland._G = userland
|
||||||
userland.load = function(chunk, chunkname, mode, env)
|
userland.load = function(chunk, chunkname, mode, env)
|
||||||
if not env or env == _G then
|
if not env or env == _G then
|
||||||
env = userland
|
env = userland
|
||||||
end -- if they SOMEHOW get the kernel environment they're not running jack shit
|
end -- if they SOMEHOW get the kernel environment they're not running jack shit
|
||||||
return load(chunk, chunkname, mode, env)
|
return load(chunk, chunkname, mode, env)
|
||||||
end
|
end
|
||||||
userland.require = reqgen(userland.load)
|
userland.require = reqgen(userland.load)
|
||||||
|
|
||||||
assert(load(data, "=" .. path, "t", userland))(table.unpack(args))
|
assert(load(data, "=" .. path, "t", userland))(table.unpack(args))
|
||||||
end, function(errorMessage)
|
end, function(errorMessage)
|
||||||
return errorMessage .. "\n \n" .. debug.traceback()
|
return errorMessage .. "\n \n" .. debug.traceback()
|
||||||
end, --[[ path,]] table.unpack(args))
|
end, --[[ path,]] table.unpack(args))
|
||||||
if not result then
|
if not result then
|
||||||
if print then
|
if print then
|
||||||
gpu.freeAllBuffers()
|
gpu.freeAllBuffers()
|
||||||
print("\n\27[91m" .. errorMessage)
|
print("\n\27[91m" .. errorMessage)
|
||||||
else
|
else
|
||||||
error(errorMessage)
|
error(errorMessage)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
--require(path, table.unpack(args))
|
--require(path, table.unpack(args))
|
||||||
end
|
end
|
||||||
local _, taskInfo = _PUBLIC.tsched.addTask(taskFunction, string.match(tostring(path), "([^/]+)%.lua$"))
|
local _, taskInfo = _PUBLIC.tsched.addTask(taskFunction, string.match(tostring(path), "([^/]+)%.lua$"))
|
||||||
taskInfo.path = path
|
taskInfo.path = path
|
||||||
taskInfo.args = table.copy(args)
|
taskInfo.args = table.copy(args)
|
||||||
end
|
end
|
||||||
|
|
||||||
function _G._PUBLIC.tsched.addTask(func, name)
|
function _G._PUBLIC.tsched.addTask(func, name)
|
||||||
local task = coroutine.create(func)
|
local task = coroutine.create(func)
|
||||||
local taskInfo = { ["task"] = task, ["name"] = name, ["id"] = idCounter }
|
local taskInfo = { ["task"] = task, ["name"] = name, ["id"] = idCounter }
|
||||||
if currentTask and type(currentTask.id) == "number" then
|
if currentTask and type(currentTask.id) == "number" then
|
||||||
taskInfo.parent = currentTask.id
|
taskInfo.parent = currentTask.id
|
||||||
end
|
end
|
||||||
table.insert(tsched.tasks, taskInfo)
|
table.insert(tsched.tasks, taskInfo)
|
||||||
idCounter = idCounter + 1
|
idCounter = idCounter + 1
|
||||||
if taskInfo.parent then
|
if taskInfo.parent then
|
||||||
log.kernel.info(
|
log.kernel.info(
|
||||||
"Created task " .. name .. " with PID " .. idCounter - 1 .. " by parent with PID " .. taskInfo.parent
|
"Created task " .. name .. " with PID " .. idCounter - 1 .. " by parent with PID " .. taskInfo.parent
|
||||||
)
|
)
|
||||||
else
|
else
|
||||||
log.kernel.info("Created task " .. name .. " with PID " .. idCounter - 1 .. " (no parent found)")
|
log.kernel.info("Created task " .. name .. " with PID " .. idCounter - 1 .. " (no parent found)")
|
||||||
end
|
end
|
||||||
return task, taskInfo
|
return task, taskInfo
|
||||||
end
|
end
|
||||||
|
|
||||||
function _G._PUBLIC.tsched.removeTask(id)
|
function _G._PUBLIC.tsched.removeTask(id)
|
||||||
-- TODO: Check for user permissions before running
|
-- TODO: Check for user permissions before running
|
||||||
for index, task in pairs(tsched.tasks) do
|
for index, task in pairs(tsched.tasks) do
|
||||||
if task.id == id then
|
if task.id == id then
|
||||||
table.remove(tsched.tasks, index)
|
table.remove(tsched.tasks, index)
|
||||||
log.kernel.info("Removed task with PID " .. id)
|
log.kernel.info("Removed task with PID " .. id)
|
||||||
return true
|
return true
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
log.kernel.warn("Tried to remove task that doesn't exist - PID " .. id)
|
log.kernel.warn("Tried to remove task that doesn't exist - PID " .. id)
|
||||||
return false
|
return false
|
||||||
end
|
end
|
||||||
|
|
||||||
function handleError(errormsg)
|
function handleError(errormsg)
|
||||||
if errormsg == nil then -- TODO: Replace with proper error handling
|
if errormsg == nil then -- TODO: Replace with proper error handling
|
||||||
print("\27[91munknown error" .. "\n \n" .. debug.traceback())
|
print("\27[91munknown error" .. "\n \n" .. debug.traceback())
|
||||||
else
|
else
|
||||||
print("\27[91m" .. tostring(errormsg) .. "\n \n" .. debug.traceback())
|
print("\27[91m" .. tostring(errormsg) .. "\n \n" .. debug.traceback())
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
local function runTasks()
|
local function runTasks()
|
||||||
for i = 1, #_G.tsched.tasks do
|
for i = 1, #_G.tsched.tasks do
|
||||||
if tsched.tasks[i] then
|
if tsched.tasks[i] then
|
||||||
currentTask = tsched.tasks[i]
|
currentTask = tsched.tasks[i]
|
||||||
local result, errorMessage = coroutine.resume(tsched.tasks[i].task)
|
local result, errorMessage = coroutine.resume(tsched.tasks[i].task)
|
||||||
if not result then
|
if not result then
|
||||||
handleError(errorMessage)
|
handleError(errorMessage)
|
||||||
end
|
end
|
||||||
if coroutine.status(tsched.tasks[i].task) == "dead" then
|
if not tsched.tasks[i] then
|
||||||
_PUBLIC.tsched.removeTask(tsched.tasks[i].id)
|
log.kernel.warn("Attempted to update a non-existent task. This is likely because it was removed.")
|
||||||
--ocelot.log("Removed coroutine")
|
elseif coroutine.status(tsched.tasks[i].task) == "dead" then
|
||||||
i = i - 1
|
_PUBLIC.tsched.removeTask(tsched.tasks[i].id)
|
||||||
end
|
--ocelot.log("Removed coroutine")
|
||||||
--computer.pullSignal(0)
|
i = i - 1
|
||||||
--coroutine.yield()
|
end
|
||||||
end
|
--computer.pullSignal(0)
|
||||||
end
|
--coroutine.yield()
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
function _G._PUBLIC.tsched.getCurrentTask()
|
function _G._PUBLIC.tsched.getCurrentTask()
|
||||||
return table.copy(currentTask)
|
return table.copy(currentTask)
|
||||||
end
|
end
|
||||||
|
|
||||||
function _G._PUBLIC.tsched.getTasks()
|
function _G._PUBLIC.tsched.getTasks()
|
||||||
return table.copy(tsched.tasks)
|
return table.copy(tsched.tasks)
|
||||||
end
|
end
|
||||||
|
|
||||||
local function taskFunction()
|
local function taskFunction()
|
||||||
local result, errorMessage = xpcall(function()
|
local result, errorMessage = xpcall(function()
|
||||||
if not filesystem.exists("/halyde/kernel/evmgr.lua") then
|
if not filesystem.exists("/halyde/kernel/evmgr.lua") then
|
||||||
error("No such file: /halyde/kernel/evmgr.lua")
|
error("No such file: /halyde/kernel/evmgr.lua")
|
||||||
end
|
end
|
||||||
local handle, data, tmpdata = filesystem.open("/halyde/kernel/evmgr.lua"), "", nil
|
local handle, data, tmpdata = filesystem.open("/halyde/kernel/evmgr.lua"), "", nil
|
||||||
repeat
|
repeat
|
||||||
tmpdata = handle:read(math.huge or math.maxinteger)
|
tmpdata = handle:read(math.huge or math.maxinteger)
|
||||||
data = data .. (tmpdata or "")
|
data = data .. (tmpdata or "")
|
||||||
until not tmpdata
|
until not tmpdata
|
||||||
handle:close()
|
handle:close()
|
||||||
assert(load(data, "=/halyde/kernel/evmgr.lua"))()
|
assert(load(data, "=/halyde/kernel/evmgr.lua"))()
|
||||||
end, function(errorMessage)
|
end, function(errorMessage)
|
||||||
return errorMessage .. "\n \n" .. debug.traceback()
|
return errorMessage .. "\n \n" .. debug.traceback()
|
||||||
end, "/halyde/kernel/evmgr.lua")
|
end, "/halyde/kernel/evmgr.lua")
|
||||||
if not result then
|
if not result then
|
||||||
if print then
|
if print then
|
||||||
gpu.freeAllBuffers()
|
gpu.freeAllBuffers()
|
||||||
print("\n\27[91m" .. errorMessage)
|
print("\n\27[91m" .. errorMessage)
|
||||||
else
|
else
|
||||||
error(errorMessage)
|
error(errorMessage)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
_PUBLIC.tsched.addTask(taskFunction, "evmgr")
|
_PUBLIC.tsched.addTask(taskFunction, "evmgr")
|
||||||
package.preload("event")
|
package.preload("event")
|
||||||
@@ -154,26 +156,26 @@ package.preload("event")
|
|||||||
log.kernel.info("Starting startup apps...")
|
log.kernel.info("Starting startup apps...")
|
||||||
local handle, data, tmpdata = filesystem.open("/halyde/config/startupapps.json", "r"), "", nil
|
local handle, data, tmpdata = filesystem.open("/halyde/config/startupapps.json", "r"), "", nil
|
||||||
repeat
|
repeat
|
||||||
tmpdata = handle:read(math.huge or math.maxinteger)
|
tmpdata = handle:read(math.huge or math.maxinteger)
|
||||||
data = data .. (tmpdata or "")
|
data = data .. (tmpdata or "")
|
||||||
until not tmpdata
|
until not tmpdata
|
||||||
handle:close()
|
handle:close()
|
||||||
for _, line in ipairs(json.decode(data)) do
|
for _, line in ipairs(json.decode(data)) do
|
||||||
if line ~= "" then
|
if line ~= "" then
|
||||||
--[[ if _G.print then
|
--[[ if _G.print then
|
||||||
print(line)
|
print(line)
|
||||||
end ]]
|
end ]]
|
||||||
_G._PUBLIC.tsched.runAsTask(line)
|
_G._PUBLIC.tsched.runAsTask(line)
|
||||||
runTasks()
|
runTasks()
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
-- _G.cormgr.loadCoroutine("/halyde/core/shell.lua")
|
-- _G.cormgr.loadCoroutine("/halyde/core/shell.lua")
|
||||||
|
|
||||||
log.setPrintLogs(false)
|
log.setPrintLogs(false)
|
||||||
while true do
|
while true do
|
||||||
runTasks()
|
runTasks()
|
||||||
if #_G.tsched.tasks == 0 then
|
if #_G.tsched.tasks == 0 then
|
||||||
log.kernel.warn("No more tasks left! Shutting down...")
|
log.kernel.warn("No more tasks left! Shutting down...")
|
||||||
computer.shutdown()
|
computer.shutdown()
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -1,4 +0,0 @@
|
|||||||
[37m[00:00:04:12] Debug!
|
|
||||||
[97m[00:00:04:19] Info!
|
|
||||||
[93m[00:00:04:25] Warning!
|
|
||||||
[91m[00:00:04:32] Error!
|
|
||||||
Reference in New Issue
Block a user