diff --git a/IPython/html/services/kernels/handlers.py b/IPython/html/services/kernels/handlers.py
index 605e9e6..041ffce 100644
--- a/IPython/html/services/kernels/handlers.py
+++ b/IPython/html/services/kernels/handlers.py
@@ -127,7 +127,10 @@ class ZMQChannelHandler(AuthenticatedZMQStreamHandler):
# closed before the ZMQ streams are setup, they could be None.
if self.zmq_stream is not None and not self.zmq_stream.closed():
self.zmq_stream.on_recv(None)
+ # close the socket directly, don't wait for the stream
+ socket = self.zmq_stream.socket
self.zmq_stream.close()
+ socket.close()
class IOPubHandler(ZMQChannelHandler):
diff --git a/IPython/kernel/connect.py b/IPython/kernel/connect.py
index 386f80d..a6b8b6d 100644
--- a/IPython/kernel/connect.py
+++ b/IPython/kernel/connect.py
@@ -524,6 +524,8 @@ class ConnectionFileMixin(Configurable):
socket_type = channel_socket_types[channel]
self.log.debug("Connecting to: %s" % url)
sock = self.context.socket(socket_type)
+ # set linger to 1s to prevent hangs at exit
+ sock.linger = 1000
if identity:
sock.identity = identity
sock.connect(url)