Controller Routes for Form Seem to be Cached Intermittently

I have a controller which sits in front of a Form which checks the user’s IP and does the following:

  • If the users IP is returned from a stored array map:
    • Redirects them to a “not found” page (internal/relative path)
    • Redirects them to an internal page
    • Redirects them to an external site/page
  • If the users IP is not returned from the stored map they are shown the form

I’ve had intermittent issues where most of the time users with their IPs in the map are directed appropriately, but sometimes they are not. The most interesting thing is that (and you will see in the my code) there is a lot of logging in my controller, and when a user that should be redirected to a different page/external site but are shown the form instead, there is no logging from the custom module at all. It’s as if Drupal is ignoring the controller entirely and just displaying the form to the user.

All of these users are anonymous users and are not authenticated.

I have noticed that if I have this issue (where I am seeing the form instead of being redirected) if I login to the admin and try again I am directed properly, as if I’ve reset some sort of cache or it’s being ignored because I’m authenticated.

Here is a Gist of my code:

I’m on Drupal 8.6.1

Drupal version: