Jacek Caban (@jacek) commented about dlls/ntdll/loader.c:
/* Prepare for the callee stealing the reference */wm_owned_ref = FALSE;add_module_dependency( importer->ldr.DdagNode, wm->ldr.DdagNode );}else if (process_attach( wm->ldr.DdagNode, NULL ) != STATUS_SUCCESS){proc = NULL;}}if (proc && wm_owned_ref){/* Owned, but no way to bind to a dependency; leak the reference instead */wm_owned_ref = FALSE;}- }
 - if (wm_owned_ref) LdrUnloadDll( wm->ldr.DllBase );
 
While placing it here avoids duplicating the `LdrUnloadDll` call in this commit, that won't be the case once the entire series is committed. In the end, it’s only useful in the branch handling the `!proc` case. I’d suggest moving it to the `!proc` branch (next to the `ERR`), duplicating it in the branch that handles process_attach failure, and removing the latter in the commit that eliminates the process_attach call. That way, you could also remove the need to reset `wm_owned_ref` above, simplifying the logic.