diff --git a/IPython/frontend/html/notebook/static/js/notebook.js b/IPython/frontend/html/notebook/static/js/notebook.js
index 8ef4e40..e61b909 100644
--- a/IPython/frontend/html/notebook/static/js/notebook.js
+++ b/IPython/frontend/html/notebook/static/js/notebook.js
@@ -762,6 +762,11 @@ var IPython = (function (IPython) {
// console.log(reply);
var msg_type = reply.header.msg_type;
var cell = this.cell_for_msg(reply.parent_header.msg_id);
+ if (!cell){
+ // message not from this notebook
+ console.log("Received IOPub message not caused by one of my cells");
+ return;
+ }
var output_types = ['stream','display_data','pyout','pyerr'];
if (output_types.indexOf(msg_type) >= 0) {
this.handle_output(cell, msg_type, content);
diff --git a/IPython/frontend/qt/base_frontend_mixin.py b/IPython/frontend/qt/base_frontend_mixin.py
index f59fd50..697d45f 100644
--- a/IPython/frontend/qt/base_frontend_mixin.py
+++ b/IPython/frontend/qt/base_frontend_mixin.py
@@ -106,4 +106,9 @@ class BaseFrontendMixin(object):
from this frontend.
"""
session = self._kernel_manager.session.session
- return msg['parent_header']['session'] == session
+ parent = msg['parent_header']
+ if not parent:
+ # if the message has no parent, assume it is meant for all frontends
+ return True
+ else:
+ return parent.get('session') == session
diff --git a/IPython/lib/pylabtools.py b/IPython/lib/pylabtools.py
index 4cda8a5..0b6feb1 100644
--- a/IPython/lib/pylabtools.py
+++ b/IPython/lib/pylabtools.py
@@ -19,6 +19,7 @@ Authors
# Imports
#-----------------------------------------------------------------------------
+import sys
from io import BytesIO
from IPython.utils.decorators import flag_calls
@@ -317,6 +318,8 @@ def pylab_activate(user_ns, gui=None, import_all=True, shell=None):
print """
Welcome to pylab, a matplotlib-based Python environment [backend: %s].
For more information, type 'help(pylab)'.""" % backend
+ # flush stdout, just to be safe
+ sys.stdout.flush()
return gui
diff --git a/IPython/zmq/kernelapp.py b/IPython/zmq/kernelapp.py
index 9edf871..a8d105e 100644
--- a/IPython/zmq/kernelapp.py
+++ b/IPython/zmq/kernelapp.py
@@ -287,6 +287,10 @@ class KernelApp(BaseIPythonApplication):
self.write_connection_file()
self.init_io()
self.init_kernel()
+ # flush stdout/stderr, so that anything written to these streams during
+ # initialization do not get associated with the first execution request
+ sys.stdout.flush()
+ sys.stderr.flush()
def start(self):
self.heartbeat.start()