nanos-world / issues

Issue Tracker for nanos world
9 stars 1 forks source link

Using File class inside RequestAsync callback #930

Closed vugi99 closed 1 year ago

vugi99 commented 1 year ago

Prerequisites

Your Environment

Description

Using File class inside RequestAsync callback. Leading to undefined behaviors

Steps to reproduce the behavior

test-file-class.zip

Expected behavior

Should work properly without freezing / spamming console / doing undefined stuff.

Actual behavior

With the provided package : NanosWorldCore.log

When having others requests pending using the File class in the callback (Also it’s a RequestAsync inside a RequestAsync callback), the server froze when closing the file...

I believe there could be different behaviors.

gtnardy commented 1 year ago

Fixed, thx for the example

vugi99 commented 1 year ago

1.58.0 still breaks with my current code. --> When having others requests pending using the File class in the callback (Also it’s a RequestAsync inside a RequestAsync callback), the server froze when closing the file...

image

vugi99 commented 1 year ago

Here's how to reproduce

local URL_Github_API = "https://api.github.com"

local repo_owner = "nanos-world"
local repo_name = "api"
local repo_branch = "main"

local endpoint_API_Get_Files_On_Repo = "/repos/" .. repo_owner .. "/" .. repo_name .. "/git/trees/" .. repo_branch .. "?recursive=1"

function UpdateAPICacheAsync()
    Console.Log("Updating Nanos api cache")
    HTTP.RequestAsync(URL_Github_API, endpoint_API_Get_Files_On_Repo, "GET", "", "application/json", false, {}, function(status, data) -- random request async
        if status == 200 then
            if not File.Exists("Packages/" .. Package.GetName() .. "/.tcache") then
                local ret = File.CreateDirectory("Packages/" .. Package.GetName() .. "/.tcache")
                if not ret then
                    Console.Error("Cannot create cache path")
                end
            end

            local f_count = 0
            for i = 1, 5 do -- Increase loop end if it doesn't trigger
                HTTP.RequestAsync(URL_Github_API, endpoint_API_Get_Files_On_Repo, "GET", "", "application/json", false, {}, function(status_file, data_file) -- random request async inside
                    if status_file == 200 then
                        f_count = f_count + 1
                        local f_save_path = "Packages/" .. Package.GetName() .. "/.tcache/" .. tostring(f_count) .. ".txt" -- save file path in package cache
                        Console.Log("Opening '" .. f_save_path .. "'")
                        local save_file = File(f_save_path, true)
                        if save_file then
                            Console.Log("Writing '" .. f_save_path .. "'")
                            save_file:Write(data_file)
                            Console.Log("Closing '" .. f_save_path .. "'")
                            save_file:Close()
                            Console.Log("Closed '" .. f_save_path .. "'")
                        else
                            Console.Error("Cannot Open file '" .. f_save_path .. "'")
                        end
                    else
                        Console.Error("Got status " .. tostring(status_file))
                    end
                end)
            end
        end
    end)
end
UpdateAPICacheAsync()

I need the api on the server to be able to get the required data to Duplicate entities (data about the entity is important too)

gtnardy commented 1 year ago

Fixed the deadlock