Repository: ipython/ipython

Issue:
Pressing "Interrupt Kernel" kills kernel
When I  press the "Interrupt Kernel" button in the notebook, the kernel dies.
System: Windows 7, Anaconda Python 3.4.1 and current IPython master.

This is the log:

```
[I 20:06:14.782 NotebookApp] Using existing profile dir: 'C:\\Users\\hasch\\.ipython\\profile_default'
[I 20:06:14.792 NotebookApp] Serving local MathJax from C:\Users\hasch\.ipython\nbextensions\mathjax\MathJax.js at /nbex
tensions/mathjax/MathJax.js
[I 20:06:15.037 NotebookApp] Serving notebooks from local directory: i:\notebook
[I 20:06:15.037 NotebookApp] 0 active kernels
[I 20:06:15.037 NotebookApp] The IPython Notebook is running at: http://localhost:8888/
[I 20:06:15.037 NotebookApp] Use Control-C to stop this server and shut down all kernels (twice to skip confirmation).
[I 20:06:19.899 NotebookApp] Kernel started: 393b742e-b876-4e59-9e74-21e79f9e5169
[I 20:06:23.549 NotebookApp] Kernel interrupted: 393b742e-b876-4e59-9e74-21e79f9e5169
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
c:\Anaconda\lib\runpy.py in _run_module_as_main(mod_name='IPython.kernel.__main__', alter_argv=1)
    168         sys.argv[0] = mod_spec.origin
    169     return _run_code(code, main_globals, None,
--> 170                      "__main__", mod_spec)
        mod_spec = ModuleSpec(name='IPython.kernel.__main__', loader=<_frozen_importlib.SourceFileLoader object at 0x000
0000001F1D9B0>, origin='c:\\Anaconda\\lib\\site-packages\\IPython\\kernel\\__main__.py')
    171
    172 def run_module(mod_name, init_globals=None,

c:\Anaconda\lib\runpy.py in _run_code(code=<code object <module> at 0x00000000025AC5D0, fil...ite-packages\IPython\kerne
l\__main__.py", line 1>, run_globals={'__builtins__': <module 'builtins' (built-in)>, '__cached__': r'c:\Anaconda\lib\si
te-packages\IPython\kernel\__pycache__\__main__.cpython-34.pyc', '__doc__': None, '__file__': r'c:\Anaconda\lib\site-pac
kages\IPython\kernel\__main__.py', '__loader__': <_frozen_importlib.SourceFileLoader object>, '__name__': '__main__', '_
_package__': 'IPython.kernel', '__spec__': ModuleSpec(name='IPython.kernel.__main__', loade...ib\\site-packages\\IPython
\\kernel\\__main__.py'), 'app': <module 'IPython.kernel.zmq.kernelapp' from 'c:\...te-packages\\IPython\\kernel\\zmq\\ke
rnelapp.py'>}, init_globals=None, mod_name='__main__', mod_spec=ModuleSpec(name='IPython.kernel.__main__', loade...ib\\s
ite-packages\\IPython\\kernel\\__main__.py'), pkg_name='IPython.kernel', script_name=None)
     83                        __package__ = pkg_name,
     84                        __spec__ = mod_spec)
---> 85     exec(code, run_globals)
        global exec = undefined
        code = <code object <module> at 0x00000000025AC5D0, file "c:\Anaconda\lib\site-packages\IPython\kernel\__main__.
py", line 1>
        run_globals = {'__name__': '__main__', '__loader__': <_frozen_importlib.SourceFileLoader object at 0x0000000001F
1D9B0>, '__package__': 'IPython.kernel', 'app': <module 'IPython.kernel.zmq.kernelapp' from 'c:\\Anaconda\\lib\\site-pac
kages\\IPython\\kernel\\zmq\\kernelapp.py'>, '__spec__': ModuleSpec(name='IPython.kernel.__main__', loader=<_frozen_impo
rtlib.SourceFileLoader object at 0x0000000001F1D9B0>, origin='c:\\Anaconda\\lib\\site-packages\\IPython\\kernel\\__main_
_.py'), '__cached__': 'c:\\Anaconda\\lib\\site-packages\\IPython\\kernel\\__pycache__\\__main__.cpython-34.pyc', '__doc_
_': None, '__file__': 'c:\\Anaconda\\lib\\site-packages\\IPython\\kernel\\__main__.py', '__builtins__': <module 'builtin
s' (built-in)>}
     86     return run_globals
     87

c:\Anaconda\lib\site-packages\IPython\kernel\__main__.py in <module>()
      1 if __name__ == '__main__':
      2     from IPython.kernel.zmq import kernelapp as app
----> 3     app.launch_new_instance()
        global app.launch_new_instance = <bound method MetaHasTraits.launch_instance of <class 'IPython.kernel.zmq.kerne
lapp.IPKernelApp'>>

c:\Anaconda\lib\site-packages\IPython\config\application.py in launch_instance(cls=<class 'IPython.kernel.zmq.kernelapp.
IPKernelApp'>, argv=None, **kwargs={})
    572         app = cls.instance(**kwargs)
    573         app.initialize(argv)
--> 574         app.start()
        app.start = <bound method IPKernelApp.start of <IPython.kernel.zmq.kernelapp.IPKernelApp object at 0x00000000006
27358>>
    575
    576 #-----------------------------------------------------------------------------

c:\Anaconda\lib\site-packages\IPython\kernel\zmq\kernelapp.py in start(self=<IPython.kernel.zmq.kernelapp.IPKernelApp ob
ject>)
    372         self.kernel.start()
    373         try:
--> 374             ioloop.IOLoop.instance().start()
        global ioloop.IOLoop.instance.start = undefined
    375         except KeyboardInterrupt:
    376             pass

c:\Anaconda\lib\site-packages\zmq\eventloop\ioloop.py in start(self=<zmq.eventloop.ioloop.ZMQIOLoop object>)
    149     def start(self):
    150         try:
--> 151             super(ZMQIOLoop, self).start()
        global super = undefined
        global ZMQIOLoop = <class 'zmq.eventloop.ioloop.ZMQIOLoop'>
        self.start = <bound method ZMQIOLoop.start of <zmq.eventloop.ioloop.ZMQIOLoop object at 0x000000000471AB70>>
    152         except ZMQError as e:
    153             if e.errno == ETERM:

c:\Anaconda\lib\site-packages\tornado\ioloop.py in start(self=<zmq.eventloop.ioloop.ZMQIOLoop object>)
    810
    811                 try:
--> 812                     event_pairs = self._impl.poll(poll_timeout)
        event_pairs = [(<zmq.sugar.socket.Socket object at 0x00000000046691C8>, 4)]
        self._impl.poll = <bound method ZMQPoller.poll of <zmq.eventloop.ioloop.ZMQPoller object at 0x000000000471ABA8>>

        poll_timeout = 3600.0
    813                 except Exception as e:
    814                     # Depending on python version and IOLoop implementation,

c:\Anaconda\lib\site-packages\zmq\eventloop\ioloop.py in poll(self=<zmq.eventloop.ioloop.ZMQPoller object>, timeout=3600
.0)
    120         Event masks will be IOLoop.READ/WRITE/ERROR
    121         """
--> 122         z_events = self._poller.poll(1000*timeout)
        z_events = undefined
        self._poller.poll = <bound method Poller.poll of <zmq.sugar.poll.Poller object at 0x000000000471ABE0>>
        timeout = 3600.0
    123         return [ (fd,self._remap_events(evt)) for (fd,evt) in z_events ]
    124

c:\Anaconda\lib\site-packages\zmq\sugar\poll.py in poll(self=<zmq.sugar.poll.Poller object>, timeout=3600000)
     99         elif isinstance(timeout, float):
    100             timeout = int(timeout)
--> 101         return zmq_poll(self.sockets, timeout=timeout)
        global zmq_poll = <built-in function zmq_poll>
        self.sockets = [(872, 5), (<zmq.sugar.socket.Socket object at 0x00000000046691C8>, 5), (<zmq.sugar.socket.Socket
 object at 0x00000000046FAAC8>, 5)]
        timeout = 3600000
    102
    103

TypeError: 'int' object is not callable
[I 20:06:25.898 NotebookApp] KernelRestarter: restarting kernel (1/5)
WARNING:root:kernel 393b742e-b876-4e59-9e74-21e79f9e5169 restarted
``
```



Discussion:
It's interesting that it seems to be claiming that `zmq_poll` is both a built-in function and a non-callable int. What pyzmq version?

pyzmq                     14.4.1                   py34_0

I've been seeing that traceback in Windows tests as well, even if the tests aren't actually failing. Sometimes the same error message is pointing to different lines.

This appears to be a regression in pyzmq 14, so I'll be looking into it. It doesn't make a lot of sense, since the code where the wrong exception is being raised hasn't actually changed at all, but then again, nothing about how Windows handles signals has ever made any sense. For now, you can install `'pyzmq<14'` to avoid the error.


Resolving patch:
diff --git a/IPython/kernel/zmq/parentpoller.py b/IPython/kernel/zmq/parentpoller.py
--- a/IPython/kernel/zmq/parentpoller.py
+++ b/IPython/kernel/zmq/parentpoller.py
@@ -1,10 +1,13 @@
-# Standard library imports.
+# Copyright (c) IPython Development Team.
+# Distributed under the terms of the Modified BSD License.
+
 try:
     import ctypes
 except:
     ctypes = None
 import os
 import platform
+import signal
 import time
 try:
     from _thread import interrupt_main  # Py 3
@@ -129,7 +132,10 @@ def run(self):
                 handle = handles[result - WAIT_OBJECT_0]
 
                 if handle == self.interrupt_handle:
-                    interrupt_main()
+                    # check if signal handler is callable
+                    # to avoid 'int not callable' error (Python issue #23395)
+                    if callable(signal.getsignal(signal.SIGINT)):
+                        interrupt_main()
 
                 elif handle == self.parent_handle:
                     os._exit(1)


Test patch: