# Buggy code
def add_task_in_thread(self, wrapper, delay=0, task_name=None):
    assert wrapper

    if not task_name:
        with self._lock:
            self._auto_counter += 1
        task_name = "threadpool_manager %d" % self._auto_counter

    def delayed_call(delay, task_name):
        self.register_task(
            task_name, self._reactor.callLater(delay, reactor.callInThread, wrapper)
        )

    reactor.callFromThread(delayed_call, delay, task_name)


# Traceback (exceptions.AssertionError)
ERROR   1435578237.91 tunnel_community:1091  Got ping from {95.211.198.140:21004} (not responding)
ERROR   1435578237.95 tunnel_community:1091  Got ping from {145.94.62.234:7759} (not responding)
ERROR   1435578238.59             log:455   Unhandled Error
Traceback (most recent call last):
File "/usr/lib/python2.7/threading.py", line 763, in run
self.__target(*self.__args, **self.__kwargs)
File "/home/pouwelse/GITHUB/tribler/Tribler/Core/Utilities/twisted_thread.py", line 55, in _reactor_runner
reactor.run(installSignalHandlers=False)
File "/usr/lib/python2.7/dist-packages/twisted/internet/base.py", line 1192, in run
self.mainLoop()
File "/usr/lib/python2.7/dist-packages/twisted/internet/base.py", line 1201, in mainLoop
self.runUntilCurrent()
--- <exception caught here> ---
File "/usr/lib/python2.7/dist-packages/twisted/internet/base.py", line 797, in runUntilCurrent
f(*a, **kw)
File "/home/pouwelse/GITHUB/tribler/Tribler/Core/APIImplementation/threadpoolmanager.py", line 29, in <lambda>
reactor.callFromThread(lambda: self.register_task(task_name, self._reactor.callLater(delay, wrapper)))
File "/home/pouwelse/GITHUB/tribler/Tribler/dispersy/taskmanager.py", line 36, in register_task
assert not self.is_pending_task_active(name), name
exceptions.AssertionError: threadpool_manager 486

Unhandled Error
Traceback (most recent call last):
File "/usr/lib/python2.7/threading.py", line 763, in run
self.__target(*self.__args, **self.__kwargs)
File "/home/pouwelse/GITHUB/tribler/Tribler/Core/Utilities/twisted_thread.py", line 55, in _reactor_runner
reactor.run(installSignalHandlers=False)
File "/usr/lib/python2.7/dist-packages/twisted/internet/base.py", line 1192, in run
self.mainLoop()
File "/usr/lib/python2.7/dist-packages/twisted/internet/base.py", line 1201, in mainLoop
self.runUntilCurrent()
--- <exception caught here> ---
File "/usr/lib/python2.7/dist-packages/twisted/internet/base.py", line 797, in runUntilCurrent
f(*a, **kw)
File "/home/pouwelse/GITHUB/tribler/Tribler/Core/APIImplementation/threadpoolmanager.py", line 29, in <lambda>
reactor.callFromThread(lambda: self.register_task(task_name, self._reactor.callLater(delay, wrapper)))
File "/home/pouwelse/GITHUB/tribler/Tribler/dispersy/taskmanager.py", line 36, in register_task
assert not self.is_pending_task_active(name), name
exceptions.AssertionError: threadpool_manager 486
ERROR   1435578252.92 tunnel_community:1091  Got ping from {145.94.62.234:7759} (not responding)
ERROR   1435578252.98 tunnel_community:1091  Got ping from {95.211.198.140:21004} (not responding)

# Fixed code
def add_task_in_thread(self, wrapper, delay=0, task_name=None):
    assert wrapper

    def delayed_call(delay, task_name):
        self.register_task(
            self._check_task_name(task_name),
            self._reactor.callLater(delay, reactor.callInThread, wrapper),
        )

    reactor.callFromThread(delayed_call, delay, task_name)