I am also against global variables and global functions. I just did not bother looking up on the internet how to manage the scope of functions in Lua. I will modify my code to be more inline with good uses of functions, thanks.
Also, for global variables, i couldn't find any other way to solve my problem. The issue is that the cancelled crafting event is triggered before the items are added back to the inventory, and I have no way (as far as I know) to temper with that. Thus, as I wanted to simplify my code and use the parameters given in the cancelled crafting event, I went with the global variables. However, you are right and my management here is lacking, i will as you suggested turn them to nil at the end.
I am not sure about what you mean by race condition however. I would instinctively think about the fact that several mods may be triggering on a same event and therefore, if the processing were to be too long, my code might be interrupted. In this case, I would have yet to think of a solution, but I have never encountered that yet.
Finally, you are once again right with adding and removing and this reminds me that i did not handle sub-crafting. I will work on that also. When it comes to cross mod interaction, there aren't many mods out there interacting with crafting that I know off (and none that I use), so I will have to do case by case bug fixing. One of them is handyhands, and it will not work with my mod. I plan on making a compatible copy in the future.
Thanks a lot for your feedback, it is really helpful to receive especially from an experienced modder :)
Edit : For item spilling, I do not think there is an easy fix for it unfortunately. I will look into it but i would rather not have to spend too long on such a simple mod. The one you mentioned is partial as some sub recipes might produce more than the parent recipe uses.