This is a recipe for how you can achieve your goal with your approach. I will not write your code for you, just tell how it can be done.
Your goal is to set / change environment variables in the current bash shell, effectively using a possible complex shell script. Some component of this script may decide that the execution of this script source should stop. The tricky part is that this solution does not have to be at the top level, but can be located in a nested function call. return does not help, but exit completes the source shell, which is undesirable.
Your task is simplified thanks to your statement:
the additional complexity that I really cannot include in the minimum example makes it very desirable to centralize the completion procedure in a function.
Here's how you do it:
Instead of looking for your real script, which determines which environment should be installed (" realscript.bash "), you use another script, " ipcscript.bash ".
ipcscript.bash establish some interprocess communication. It may be a channel on some additional file descriptor that you open with exec, it may be a temporary file, it may be something else.
ipcscript.bash will run realscript.bash as a child process. This means that the environment changes that realscript.bash does first only affect the environment of this bash child process. By running realscript.bash as a child process, you will be able to stop execution at any nested level with exit without terminating the source shell.
Your exit call will live, when you write, in a centralized function called from any level when it is decided to stop execution. Now, before completing the shutdown function, it is necessary to write the current environment to the IPC engine in a suitable format.
ipcscript.bash will read the environment settings from the IPC engine and play all the settings in the search process.