Use GetSelectedExecutionContext() instead of GetCommandInterpreter().GetExecutionContext() in SBDebugger::GetInternalVariableValue/SBDebugger::SetInternalVariable. The execution context in the command interpreter might be empty, if no commands has been executed yet (it is updated only when handling commands or completions -- e.g. https://github.com/llvm/llvm-project/blob/main/lldb/source/Interpreter/CommandInterpreter.cpp#L1855).
I think it's fine to fall back to the "currently selected target" if the command interpreter doesn't have a current execution context, that should not be your first choice.
For instance, if you are running this command in a Python breakpoint callback, then the current Interpreter context will be set to the target/process/thread that the interpreter is running that command for, whereas the selected context will be whatever the user last selected, and may be for a totally different target/process/thread.