correct loop termination for ipython usage