FileAccess - directory creation and thread safety

Godot Version

v4.6.2.stable.mono.official [71f334935]

Question

I have two questions:

  1. Is FileAccess thread safe? I have a habit of running stuff like logging or saving progress on separate thread to avoid stutters. From my experience it runs on separate thread without issues, but it’s not on the list of thread-safe APIs, so it might just be an accident.
  2. ModeFlags.Write is capable of creating a file, but only in existing directory. Is there a warranty that user:// directory is bound to exist once game is launched, or it’s safer to explicitly create the directory?

Edit

I’m not sure if I correctly interpret what thread safe means - by thread safe I mean can be run on separate thread without any errors, assuming only one thread edits the file.

From what i know from basics of computer science, doing any save operations on another thread are risky, so from logical standpoint, i’d suggest you doing it on main thread instead, maybe try making a request system where you send request and return the result if it succeds?

I’m quite positive System.IO is thread safe (assuming you don’t have multiple threads accessing the file of course), but I’m using FileAccess since it’s Godot native, and it gives me access to ConfigFile, and I don’t know how well it would work with System.IO.

Thread safe means that two threads cannot manipulate the data in the same memory location at the same time. In this context it would extend to ask if two threads try to edit the same file, that one of them would fail. I have no idea if Godot does that.

But if you are only doing it from one thread, then it is “safe”.

Not only is the folder created for you, but trying to create it yourself would be a bad idea. It exists in different places in every OS. The whole point of the location is you can write to it in any OS and don’t have to create the container. You can always create a deeper folder structure inside it if you want.