Skip to content
This repository was archived by the owner on Nov 23, 2017. It is now read-only.

TypeError: signal handler must be signal.SIG_IGN, signal.SIG_DFL, or a callable object #396

Closed
sametmax opened this issue Aug 5, 2016 · 2 comments

Comments

@sametmax
Copy link

sametmax commented Aug 5, 2016

If you register a signal handler for SIGTERM, start the event loop then kill it with CTRL + C, you get this error:

Exception ignored in: <bound method BaseEventLoop.__del__ of <_UnixSelectorEventLoop running=False closed=True debug=False>>
Traceback (most recent call last):
  File "/usr/lib/python3.5/asyncio/base_events.py", line 431, in __del__
  File "/usr/lib/python3.5/asyncio/unix_events.py", line 58, in close
  File "/usr/lib/python3.5/asyncio/unix_events.py", line 140, in remove_signal_handler
  File "/usr/lib/python3.5/signal.py", line 47, in signal
TypeError: signal handler must be signal.SIG_IGN, signal.SIG_DFL, or a callable object

Minimal code to get the error:

import signal
import asyncio

loop = asyncio.get_event_loop()

def bar():
    loop.stop()

loop.add_signal_handler(signal.SIGTERM, bar)
loop.run_forever()

This will not occur if you register a handler for another signal, such as SIGINT.

@vxgmichel
Copy link

This will not occur if you register a handler for another signal, such as SIGINT.

It does actually occur for all signals except SIGINT (SIGINT has a special treatment in loop.remove_signal_handler). This error has been reported on bugs.python.org as issue32548. I've looked into it, and it turns out this problem is unrelated to asyncio. It can be reproduced using the _signal module only:

import _signal

class A:
    def __del__(self):
        _signal.signal(_signal.SIGTERM, _signal.SIG_DFL)

a = A()

It also affects python 2. This happens because the signalmodule resources are cleared before the garbage collector calls a.__del__. It causes this if statement to fail and a misleading TypeError is raised.

It's quite similar to issue 14173 (fixed in 2012).

@sametmax
Copy link
Author

sametmax commented Aug 5, 2016

Thanks. Let me close this then, no need for duplicate tickets.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants