lune-org / lune

A standalone Luau runtime
https://lune-org.github.io/docs
Mozilla Public License 2.0
360 stars 84 forks source link

using task.spawn with ASYNC functions cause hanging process #195

Open RuizuKun-Dev opened 4 months ago

RuizuKun-Dev commented 4 months ago

Utilizing task.spawn and asynchronous operations seems to hang indefinitely, preventing it from returning control back to the shell prompt

Happens with both with lune run main.luau and when using the .exe

Steps to Reproduce:

  1. Run the provided Luau program.
  2. Observe that the program gets stuck in an infinite loop and never returns control back to the shell prompt

Code Snippet:

local process = require("@lune/process")
local stdio = require("@lune/stdio")
local task = require("@lune/task")

local function main(_): number
    local count: number = 1

    for i = 1, 1000 do
        task.spawn(function()
            -- process will never exits
            -- and will never complete the for loop
            -- unless task.spawn is commented out

            -- fs.copy also has this effect
            stdio.write(`{i}\n`)
            -- but print doesn't
            -- print(i)
            -- it seems like any ASYNC function will have this effect
        end)

        count = count + 1
    end

    stdio.prompt("text", "COMPLETE")

    return 0
end
return main(table.unpack(process.args))

Expected Behavior:

The program should execute the asynchronous tasks spawned by task.spawn, complete the loop, display the completion prompt, and return control back to the shell prompt

Actual Behavior:

The program enters an infinite loop, continuously spawning asynchronous tasks without completing the loop or displaying the completion prompt, effectively hanging and preventing it from exiting back to the shell

Environment:

Operating System: Windows 10 Home Lua Version: 0.8.3

bjcscat commented 4 months ago

Seems to be caused by the scheduler's try_tick() loop getting caught in a spinloop.

(should we have merged the scheduler into this repo too?)

filiptibell commented 4 months ago

(should we have merged the scheduler into this repo too?)

Probably a good idea. It didn't make much sense to have it in the same repo at the time but definitely does now with the new crates. I'll look into bringing it over.