Did you try writing tests for (at least same-process) duplicated hive handles? e.g. duplicate the handle, close the original, does the duplicated handle work as expected?
If that shows that it does, you'd probably want some kind of "transient" key that deletes itself when the final handle to it is closed. Probably this would mean recursively checking `handle_count` in `key_close_handle()`.