I have a managed app that uses URL.getSalesforceBaseUrl().toExternalForm() to create a Remote site for ToolingApi. This works great in all environments (Production, Dev/scratch org), however, it's pretty unreliable in Sandboxes. Sometimes it returns the proper URL sometimes it doesn't.
For example: While running the app in a sandbox, this line of code
However, using that same method inside Execute anonymous returns https://exmplesite--testing.cs61.my.salesforce.com which is the correct url needed for the toolingApi.
Anyone have any insight on what could be happening?