Over time, I’ve found that there’s a lot of APIs in Zig that constantly return errors I can’t really do anything about if they happen. error.OutOfMemory is an example, and error.WriteFailed is another.
When writing a library I can’t know the context of the calling code (who may actually be able to handle the error), so in those cases I do just propagate the error to the caller by using try everywhere.
In an application context though, where I know if the thing returns OOM or the Writer fails there’s nothing I can do and I’d mostly just crash, I’ve been finding myself writing catch @panic(...) often, and even writing wrapper functions that does it for me.
The code without try littered everywhere is always easier to read. I also feel like receiving an error code is not actually all that useful if it could have come from 50 different places in the function you just called, so for stuff that does return an error, it’s nice knowing it will be one I can actually handle and know a lot about.
At the same time though, it bugs me that this makes the code inflexible, and I can’t help but wonder if there’s some hypothetical way of handling WriteFailed or OOM in certain cases that I’m totally skipping by doing this.
In the android game I have been developing, I try to have boundaries where errors cannot propagate upwards. An example for this is for different scenes.
Say we have a scene manager and it calls the init, update and draw functions of the scenes. If a scene returns an error out of these functions it will try its best to recover to a reasonable state, if it cannot it will just restart the scene entirely. Scene manager pre allocates the memory given to a specific scene so oom is sorta recoverable as well.
This also makes handling the scene managers errors easy, because with how I structured the manager, it can only return fatal errors. So, main function tries to log it and crashes.
Yeah, failing early and then writing swathes of code that can assume no errors will happen is usually my favorite way of handling it, but it doesn’t really work for stuff like allocators or writers that can fail at any moment.
I also make sure to reserve memory first and all of that, but I’m curious how that makes OOM recoverable? I’ve never really understood what you’re supposed to do on OOM to be frank, unless you’re requesting memory that’s completely optional. If you need the thing, you kinda just… need the thing and there’s not much you can do if you don’t get it. If that makes sense, lol.
For fatal errors, I don’t really see the point in returning them all the way to main, so I just immediately crash then and there.
Scenes in my game are written in a way to use static memory as much as possible, and when it needs an arbitrary amount of memory it can reach for the allocator. The allocator has a limited amount of memory allocated by the scene manager, and the amount changes depending on the scene.
A new scene gets 10MB of pre allocated memory, during development of the said new scene, the memory it gets either shrinks or expands. I generally try to keep it under 10MB, but it is what it is sometimes (a huge scene with shit ton of entities for example). I also pre allocate the gpu memory using the same principles, though in a much more limited and specialized way.
I should clarify that my scenes never directly interact with each other. Scene manager handles the interactions, and these interactions are kept very simple.
This way, it is pretty unusual for a scene to run out of its allocated memory. If that ever happens, scene has three choices, either try to free up some memory its using (or reuse), give up what its trying to do, or propagate the error to the scene manager.
I don’t have much experience with trying to recover writers, but memory allocations can be handled if done properly. Zig’s stdlib conventions allows us to easily use custom allocators, by taking advantage of that, we can limit the amount of memory parts of our code using.
Detailed logs are great way to find the assumptions you made are actually not valid. Well, that is if the app managed to properly do the logging.
EDIT: If the scene manager cannot reserve the memory a scene needs, well, at that point something gone very wrong. I constrain my software depending on where it is going to be executed. In this specific case, if Android cannot even give me 10MBs on average per scene, that is a sign of either I suck at optimizing memory usage or just crash the app.
An example of a library function that recovers from OutOfMemory is std.HashMap.getOrPut:
self.growIfNeeded(allocator, 1, ctx) catch |err| {
// If allocation fails, try to do the lookup anyway.
// If we find an existing item, we can return it.
// Otherwise return the error, we could not add another.
const index = self.getIndex(key, key_ctx) orelse return err;
return GetOrPutResult{
.key_ptr = &self.keys()[index],
.value_ptr = &self.values()[index],
.found_existing = true,
};
};