Fix bad writes on close.