mmm, okay this is getting more complicated, there is another case, if you open or create a child key inside the application hive key and you close the app hive key, the child key will be still accessible and the app hive key will get close successfully also, so I think it will not work if I just check recursively from the root key. Reading back, you pointed out about modifying `save_branch_info` to allow adding more hives (https://gitlab.winehq.org/wine/wine/-/merge_requests/820#note_8956), so I think it will be better to do this at the end, and save the branch in every operation or regularly. About when to delete the transient key, I think in every call to key_close_handle I could check if the parent key of the transient key is a transient key also, if it is we just keep looking backward until finding the root of the transient key, and from there do the checks if the entire hive can be deleted.