Fix issue: triggering end before file closed