[fix] Fix a race condition for the `spark.end` method