Lock resources before checking if we need to clean them.
See the comments deleted by this commit for more details as to how the race condition could effect the system.
See the comments deleted by this commit for more details as to how the race condition could effect the system.