Don't try to lock the same formula more than once

Fixes #28765.