Keg#lock: guard against nil in ensure

Fixes #17403.