Backport of #8693 to 0.3

@tkelman  I haven't seen any related REPL problems regarding #8693 in the last week, merge away when you feel it is ready.