Nice. This is a class of error with the delightful acronym TOCTOU (Time of Check to Time of Use) which is present in an astonishing number of places.
https://en.m.wikipedia.org/wiki/Time-of-check_to_time-of-use
https://en.m.wikipedia.org/wiki/Time-of-check_to_time-of-use
if(check(resource)) {
use(resource);
}
is already vulnerable, unless you somehow make the entire piece of code run atomically.