[minor] Reduce duplicate code by using the `spark.end` method.