Fix infinite loop in onclose -> cleanUp -> close -> onclose -> cleanUp -> ...