how Drupal refresh/rebuild its route automatically after route programmaticaly created?
I can't see anything in the module doing it, but this is done through the router.builder service.

Either rebuild immediately, with \Drupal::service('router.builder')-&gt;rebuild(); or tell it to rebuild at the end of the request, which is \Drupal::service('router.builder')-&gt;setRebuildNeeded()

Based on what you posted, it looks to me that you went through all the boring, generic stuff and stopped right before if would have gotten interesting, in that services rebuildIfNeeded() method. which checks the rebuildNeeded flag and rebuilds if TRUE.

So, you don't have to do anything to have that method called, that happens automatically if the service got invoked, you just have to make sure that flag is set.