call closeAfterFlush() when needed