How to prevent crashing when using a while loop in a corutine

I’m making a wandering navmesh script that stops for 5 seconds before finding a new destination so I am using a while loop inside a coroutine instead of an update function so that I can use the yield wait for seconds function, but putting a while loop in a coroutine just freezes the editor, is there any way around this?

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;


public class aiController : MonoBehaviour
{
    public NavMeshAgent ai;
    public float speed;
    public float walkableRadius;
    float waitTime = 0;
    bool waitToMove;


    void Start()
    {
        StartCoroutine(update());
        ai.speed = speed;
        ai.SetDestination(randomLocation());
    }

    
    IEnumerator update() 
    {
	while(true)
    { 
        if(ai.remainingDistance <= ai.stoppingDistance)
        {
            ai.SetDestination(transform.position);
            yield return new WaitForSeconds(5);
            ai.SetDestination(randomLocation());
            yield return null;
          
        }

    }
    }

 

    Vector3 randomLocation()
    {
        Vector3 finalPosition = Vector3.zero;
        Vector3 randomPosition = Random.insideUnitSphere * walkableRadius;
        randomPosition += transform.position;
        if(NavMesh.SamplePosition(randomPosition, out NavMeshHit hit, walkableRadius, 1))
        {
            finalPosition = hit.position
        }
        
        return finalPosition;
    }

   

}

`