fix bugs that writer or os not closed