WilliamVenner / gm_async_write

☁ file.AsyncWrite for Garry's Mod
MIT License
26 stars 0 forks source link

`file.AsyncWrite/file.AsyncAppend` can cause a crash when a dedicated server is shutdown #1

Closed FlorianLeChat closed 2 years ago

FlorianLeChat commented 2 years ago

Since a few days, or even weeks, I noticed that my dedicated development server was no longer shutting down properly. When I checked the logs, I saw that the server was crashing regularly with this error:

thread '<unnamed>' panicked at 'called `Result::unwrap()` on an `Err` value: SendError(Job { raw_path: "github/test.json", path: "garrysmod/data/github/test.json", data: [123, 10, 9, 34, 115, 116, 114, 105, 110, 103, 49, 34, 58, 32, 116, 114, 117, 101, 44, 10, 9, 34, 115, 116, 114, 105, 110, 103, 50, 34, 58, 32, 102, 97, 108, 115, 101, 10, 125], callback: None, append: false, result: None })', src/worker.rs:38:25
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
fatal runtime error: Rust panics must be rethrown
Aborted
Add "-debug" to the ./srcds_run command line to generate a debug.log to help with solving this problem
jeu. 10 févr. 2022 18:31:33 CET: Server restart in 10 seconds

Other times, the server shuts down correctly and shows the following lines:

L 02/11/2022 - 07:00:03: [Florian&Co] @ FAdmin: Server succesfully shut down.
SoundEmitter:  removing map sound overrides [86 to remove, 0 to restore]
Shutting down gm_async_write worker thread...
L 02/11/2022 - 07:00:04: server_message: "quit"
Could not find steamerrorreporter binary. Any minidumps will be uploaded in-processL02/11/2022 - 07:00:05: Log file closed.
L 02/11/2022 - 07:00:05: server_message: "restart"
ven. 11 févr. 2022 07:00:05 CET: Server Quit

After some investigation, I retrieved the only script using your module and tried to reproduce the crash reliably. Here is the relevant code:

require("async_write")

local tbl = {
    ["string1"] = true,
    ["string2"] = false
}

file.CreateDir("github")

hook.Add("ShutDown", "GitHub", function()
    file.AsyncWrite("github/test.json", util.TableToJSON(tbl, true))
    -- OR
    file.AsyncAppend("github/test.json", util.TableToJSON(tbl, true))
end)

Unfortunately these steps do not allow this issue to be reproduced all the time but it seems to appear very regularly (especially when the file is already present and it wants to rewrite the content). I did these tests in Sandbox gamemode without any addon to not falsify the results, moreover I had no other C++ modules than the latest version of your module.

Here are the information of the dedicated server (running under Debian 11):

Protocol version 24
Exe version 2021.12.15 (garrysmod)
Exe build: 16:38:55 Jan 27 2022 (8482) (4000)
GMod version 2022.02.02, branch: unknown
Linux 32bit Dedicated Server

I don't have the chance at the moment to test this issue on the Windows version but I assume it occurs in other versions too. If you need more information, I am actively monitoring the repository 👍

FlorianLeChat commented 2 years ago

This is now fixed with the latest version, thanks a lot Billy! ✅