Make sure the scratch buffer is always correctly closed
handle errors before trying to close an unavailable scratch buffer