Fixed runtime panic error by close that was called early.