numPending moved to close event to prevent race condition