diff --git a/gdb/annotate.c b/gdb/annotate.c
index 6cce693..97e2b2b 100644
--- a/gdb/annotate.c
+++ b/gdb/annotate.c
@@ -72,8 +72,17 @@ annotate_breakpoints_invalid (void)
       && (!breakpoints_invalid_emitted
 	  || async_background_execution_p ()))
     {
-      target_terminal_ours ();
+      /* If the inferior owns the terminal (e.g., we're resuming),
+	 make sure to leave with the inferior still owning it.  */
+      int was_inferior = target_terminal_is_inferior ();
+
+      target_terminal_ours_for_output ();
+
       printf_unfiltered (("\n\032\032breakpoints-invalid\n"));
+
+      if (was_inferior)
+	target_terminal_inferior ();
+
       breakpoints_invalid_emitted = 1;
     }
 }
@@ -210,8 +219,17 @@ annotate_frames_invalid (void)
       && (!frames_invalid_emitted
 	  || async_background_execution_p ()))
     {
-      target_terminal_ours ();
+      /* If the inferior owns the terminal (e.g., we're resuming),
+	 make sure to leave with the inferior still owning it.  */
+      int was_inferior = target_terminal_is_inferior ();
+
+      target_terminal_ours_for_output ();
+
       printf_unfiltered (("\n\032\032frames-invalid\n"));
+
+      if (was_inferior)
+	target_terminal_inferior ();
+
       frames_invalid_emitted = 1;
     }
 }
diff --git a/gdb/target.c b/gdb/target.c
index bcc736b..c1eb869 100644
--- a/gdb/target.c
+++ b/gdb/target.c
@@ -461,6 +461,14 @@ target_terminal_init (void)
 
 /* See target.h.  */
 
+int
+target_terminal_is_inferior (void)
+{
+  return (terminal_state == terminal_is_inferior);
+}
+
+/* See target.h.  */
+
 void
 target_terminal_inferior (void)
 {
diff --git a/gdb/target.h b/gdb/target.h
index c1ab124..dc5a1f3 100644
--- a/gdb/target.h
+++ b/gdb/target.h
@@ -1380,6 +1380,11 @@ extern int target_insert_breakpoint (struct gdbarch *gdbarch,
 extern int target_remove_breakpoint (struct gdbarch *gdbarch,
 				     struct bp_target_info *bp_tgt);
 
+/* Returns true if the terminal settings of the inferior are in
+   effect.  */
+
+extern int target_terminal_is_inferior (void);
+
 /* Initialize the terminal settings we record for the inferior,
    before we actually run the inferior.  */
 
diff --git a/gdb/testsuite/gdb.base/annota-input-while-running.c b/gdb/testsuite/gdb.base/annota-input-while-running.c
new file mode 100644
index 0000000..3db1399
--- /dev/null
+++ b/gdb/testsuite/gdb.base/annota-input-while-running.c
@@ -0,0 +1,25 @@
+/* This testcase is part of GDB, the GNU debugger.
+
+   Copyright 2014 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/>.  */
+
+#include <unistd.h>
+
+int
+main (void)
+{
+  sleep (5);
+  return 0; /* set break here */
+}
diff --git a/gdb/testsuite/gdb.base/annota-input-while-running.exp b/gdb/testsuite/gdb.base/annota-input-while-running.exp
new file mode 100644
index 0000000..1375718
--- /dev/null
+++ b/gdb/testsuite/gdb.base/annota-input-while-running.exp
@@ -0,0 +1,130 @@
+# Copyright 1999-2014 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 that annotations support doesn't leave GDB's terminal settings
+# into effect when we run a foreground command.
+
+if [is_remote target] then {
+    # We cannot use runto_main because of the different prompt we get
+    # when using annotation level 2.
+    return 0
+}
+
+standard_testfile
+
+if {[prepare_for_testing "failed to prepare" $testfile $srcfile debug] == -1} {
+    return -1
+}
+
+# Break at main
+
+gdb_test "break main" \
+    "Breakpoint.*at.* file .*$srcfile.*\\." \
+    "breakpoint main"
+
+# NOTE: this prompt is OK only when the annotation level is > 1
+# NOTE: When this prompt is in use the gdb_test procedure cannot be
+# used because it assumes that the last char after the gdb_prompt is a
+# white space.  This is not true with this annotated prompt.  So we
+# must use the gdb_annota_test replacement below, or
+# gdb_test_multiple.
+
+set old_gdb_prompt $gdb_prompt
+set gdb_prompt "\r\n\032\032pre-prompt\r\n$gdb_prompt \r\n\032\032prompt\r\n"
+
+# Like gdb_test, but cope with the annotation prompt.
+proc gdb_annota_test {command pattern message} {
+    global gdb_prompt
+
+    gdb_test_multiple $command $message {
+	-re "$pattern$gdb_prompt$" {
+	    pass "$message"
+	}
+	-re "$gdb_prompt$" {
+	    fail "$message"
+	}
+    }
+}
+
+# Set the annotation level to 2.
+
+set test "annotation set at level 2"
+gdb_annota_test "set annotate 2" ".*" "annotation set at level 2"
+
+# Run to main.
+
+gdb_annota_test "run" \
+    "\r\n\032\032post-prompt.*\r\n\r\n\032\032stopped.*" \
+    "run until main breakpoint"
+
+set test "delete breakpoints"
+gdb_test_multiple "delete" $test {
+    -re "Delete all breakpoints. .y or n." {
+	send_gdb "y\n"
+	exp_continue
+    }
+    -re "$gdb_prompt$" {
+	pass $test
+    }
+}
+
+# Set the target running, and then type something.  GDB used to have a
+# bug where it'd be accepting input even though the target was
+# supposedly resumed in the foreground.  This ultimately resulted in
+# readline aborting.
+
+set linenum [gdb_get_line_number "set break here"]
+
+gdb_annota_test "break $linenum" \
+    "Breakpoint .*$srcfile, line .*" \
+    "break after sleep"
+
+# Continue, and wait a bit to make sure the inferior really starts
+# running.  Wait less than much the program sleeps, which is 5
+# seconds, though.
+set saw_continuing 0
+set test "continue"
+gdb_test_multiple $test $test {
+    -timeout 2
+    -re "Continuing\\." {
+	set saw_continuing 1
+	exp_continue
+    }
+    timeout {
+	gdb_assert $saw_continuing $test
+    }
+}
+
+# Type something.
+send_gdb "print 1\n"
+
+# Poor buggy GDB would crash before the breakpoint was hit.
+set test "breakpoint hit"
+gdb_test_multiple "" $test {
+    -re "stopped\r\n$gdb_prompt" {
+	pass $test
+    }
+}
+
+set test "print command result"
+gdb_test_multiple "" $test {
+    -re "\r\n1\r\n\r\n\032\032value-history-end\r\n$gdb_prompt" {
+	pass $test
+    }
+}
+
+# Restore the original prompt for the rest of the testsuite.
+
+set gdb_prompt $old_gdb_prompt
