return;
     }
 
-  mi_proceeded = 1;
+  mi_interp *mi = as_mi_interp (top_level_interpreter ());
+
+  if (mi == nullptr)
+    return;
+
+  mi->mi_proceeded = 1;
 }
 
 /* When the element is non-zero, no MI notifications will be emitted in
      will make it impossible for frontend to know what's going on.
 
      In future (MI3), we'll be outputting "^done" here.  */
-  if (!running_result_record_printed && mi_proceeded)
+  if (!mi->running_result_record_printed && mi->mi_proceeded)
     {
       gdb_printf (mi->raw_stdout, "%s^running\n",
                  current_token ? current_token : "");
     for (thread_info *tp : all_non_exited_threads (targ, ptid))
       mi_output_running (tp);
 
-  if (!running_result_record_printed && mi_proceeded)
+  if (!mi->running_result_record_printed && mi->mi_proceeded)
     {
-      running_result_record_printed = 1;
+      mi->running_result_record_printed = 1;
       /* This is what gdb used to do historically -- printing prompt
         even if it cannot actually accept any input.  This will be
         surely removed for MI3, and may be removed even earlier.  */
 
 
   /* MI's CLI builder (wraps OUT).  */
   struct ui_out *cli_uiout;
+
+  int running_result_record_printed = 1;
+
+  /* Flag indicating that the target has proceeded since the last
+     command was issued.  */
+  int mi_proceeded;
 };
 
 /* Output the shared object attributes to UIOUT.  */
 
    command including all option, and make it possible.  */
 static struct mi_parse *current_context;
 
-int running_result_record_printed = 1;
-
-/* Flag indicating that the target has proceeded since the last
-   command was issued.  */
-int mi_proceeded;
-
 static void mi_cmd_execute (struct mi_parse *parse);
 
 static void mi_execute_async_cli_command (const char *cli_command,
   scoped_restore save_token = make_scoped_restore (¤t_token,
                                                   context->token);
 
-  running_result_record_printed = 0;
-  mi_proceeded = 0;
+  mi->running_result_record_printed = 0;
+  mi->mi_proceeded = 0;
   switch (context->op)
     {
     case MI_COMMAND:
         to directly use the mi_interp's uiout, since the command
         could have reset the interpreter, in which case the current
         uiout will most likely crash in the mi_out_* routines.  */
-      if (!running_result_record_printed)
+      if (!mi->running_result_record_printed)
        {
          gdb_puts (context->token, mi->raw_stdout);
          /* There's no particularly good reason why target-connect results
            || current_interp_named_p (INTERP_MI3)
            || current_interp_named_p (INTERP_MI4))
          {
-           if (!running_result_record_printed)
+           if (!mi->running_result_record_printed)
              {
                gdb_puts (context->token, mi->raw_stdout);
                gdb_puts ("^done", mi->raw_stdout);
 
 
 extern char *current_token;
 
-extern int running_result_record_printed;
-extern int mi_proceeded;
-
 struct mi_suppress_notification
 {
   /* Breakpoint notification suppressed?  */
 
--- /dev/null
+#include <unistd.h>
+
+int
+main (void)
+{
+  sleep (1234);
+}
 
--- /dev/null
+# Copyright 2023 Free Software Foundation, Inc.
+
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 3 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.
+
+# Test doing an -exec-run while there are two MI UIs.
+
+load_lib mi-support.exp
+
+standard_testfile
+
+if {[build_executable $testfile.exp $testfile ${srcfile} "debug"] == -1} {
+    untested "failed to compile"
+    return
+}
+
+# Run one configuration of the test.
+#
+# UI_TO_RUN is the UI that should issue the run command.
+
+proc do_test { ui_to_run } {
+    if {[mi_clean_restart $::binfile "separate-mi-tty"] != 0} {
+       fail "could not start gdb"
+       return
+    }
+
+    with_spawn_id $::gdb_main_spawn_id {
+       lassign [create_mi_ui] second_mi_spawn_id second_mi_tty_name
+    }
+
+    with_spawn_id $second_mi_spawn_id {
+       gdb_expect {
+           -re "=thread-group-added,id=\"i1\"\r\n$::mi_gdb_prompt$" {
+               pass "consume"
+           }
+       }
+    }
+
+    if { $ui_to_run == "first" } {
+       set spawn_id_to_run $::mi_spawn_id
+    } elseif { $ui_to_run == "second" } {
+       set spawn_id_to_run $second_mi_spawn_id
+    } else {
+       error "invalid ui_to_run value"
+    }
+
+    with_spawn_id $spawn_id_to_run {
+       # mi_runto_main implicitly verifies that the UI doing the -exec-run gets
+       # the expected ^running record.
+       mi_runto_main
+    }
+}
+
+foreach_with_prefix ui_to_run {first second} {
+    do_test $ui_to_run
+}
+
 
     }
 }
 
+# Create a new pty, and reate a new MI UI (using the new-ui command) on it.
+#
+# Return a list with the spawn id for that pty and the pty file name.
+
+proc create_mi_ui {} {
+    spawn -pty
+    set tty_name $spawn_out(slave,name)
+    gdb_test_multiple "new-ui mi $tty_name" "new-ui" {
+       -re "New UI allocated\r\n$::gdb_prompt $" {
+       }
+    }
+
+    return [list $spawn_id $tty_name]
+}
+
 #
 # Like default_mi_gdb_start below, but the MI is created as a separate
 # ui in a new tty.  The global MI_SPAWN_ID is updated to point at the
     gdb_start
 
     # Create the new PTY for the MI UI.
-    spawn -pty
-    set mi_spawn_id $spawn_id
-    set mi_tty_name $spawn_out(slave,name)
-    gdb_test_multiple "new-ui mi $mi_tty_name" "new-ui" {
-       -re "New UI allocated\r\n$gdb_prompt $" {
-       }
-    }
+    lassign [create_mi_ui] mi_spawn_id mi_tty_name
 
     # Switch to the MI channel.
     set gdb_main_spawn_id $gdb_spawn_id
            fail "$errmsg"
            return -1
        }
-        -re ".*$mi_gdb_prompt\[ \]*$" {
+        -re "(.*$mi_gdb_prompt\[ \]*)$" {
            if {![string match "" $message]} {
                fail "$message (unexpected output)"
            }
       # file.", etc. to the CLI stream.
       set extra_output "&\"\[^\r\n\]+\"\r\n"
   }
+
   mi_gdb_test "200-break-insert [join $extra_opts " "] -t $func" "${extra_output}200\\^done,$bp" \
       "breakpoint at $func"