safe stream closing