# Buggy code
def network_get_state(self, usercallback, getpeerlist):
    """Called by network thread"""
    with self.dllock:
        if self.handle is None:
            self._logger.debug(
                "LibtorrentDownloadImpl: network_get_state: Download not running"
            )
            if self.dlstate != DLSTATUS_CIRCUITS:
                progress = self.progressbeforestop
            else:
                tunnel_community = self.ltmgr.trsession.lm.tunnel_community
                progress = (
                    tunnel_community.tunnels_ready(self.get_hops())
                    if tunnel_community
                    else 1
                )

            ds = DownloadState(self, self.dlstate, self.error, progress)
        else:
            (status, stats, seeding_stats, logmsgs) = self.network_get_stats(
                getpeerlist
            )
            ds = DownloadState(
                self,
                status,
                self.error,
                self.get_progress(),
                stats=stats,
                seeding_stats=seeding_stats,
                filepieceranges=self.filepieceranges,
                logmsgs=logmsgs,
            )
            self.progressbeforestop = ds.get_progress()

        if usercallback:
            # Invoke the usercallback function via a new thread.
            # After the callback is invoked, the return values will be passed to the
            # returncallback for post-callback processing.
            if not self.done and self.session.lm.threadpool:
                # runs on the reactor
                def session_getstate_usercallback_target():
                    when, getpeerlist = usercallback(ds)
                    if when > 0.0 and self.session.lm.threadpool:
                        # Schedule next invocation, either on general or DL specific
                        self.session.lm.threadpool.add_task(
                            lambda: self.network_get_state(usercallback, getpeerlist),
                            when,
                        )

                self.session.lm.threadpool.add_task_in_thread(
                    session_getstate_usercallback_target
                )
        else:
            return ds


# Traceback (isError)
Regression

Tribler.Test.Core.Modules.RestApi.test_downloads_endpoint.TestDownloadsEndpoint.test_remove_download_no_remove_data_param (from nosetests)
Failing for the past 1 build (Since Failed#2109 )
Took 4.4 sec.
Error Message

Test raised 1 unhandled Twisted exceptions:
[{'log_namespace': u'log_legacy', 'log_time': 1467938979.639, 'format': '%(log_legacy)s', 'log_system': '-', 'log_legacy': <twisted.logger._stdlib.StringifiableFromEvent object at 0x06D22830>, 'log_level': <LogLevel=critical>, 'system': '-', 'isError': 1, 'failure': <twisted.python.failure.Failure exceptions.AttributeError: 'NoneType' object has no attribute 'add_task'>, 'time': 1467938979.639, 'log_text': 'Unhandled Error\nTraceback (most recent call last):\n  File "C:\\Python27\\lib\\threading.py", line 763, in run\n    self.__target(*self.__args, **self.__kwargs)\n  File "C:\\workspace\\jenkins\\workspace\\GH_Tribler_PR_tests_win32\\tribler\\Tribler\\Core\\Utilities\\twisted_thread.py", line 55, in _reactor_runner\n    reactor.run(installSignalHandlers=False)\n  File "C:\\Python27\\lib\\site-packages\\twisted\\internet\\base.py", line 1194, in run\n    self.mainLoop()\n  File "C:\\Python27\\lib\\site-packages\\twisted\\internet\\base.py", line 1203, in mainLoop\n    self.runUntilCurrent()\n--- <exception caught here> ---\n  File "C:\\Python27\\lib\\site-packages\\twisted\\internet\\base.py", line 825, in runUntilCurrent\n    call.func(*call.args, **call.kw)\n  File "C:\\workspace\\jenkins\\workspace\\GH_Tribler_PR_tests_win32\\tribler\\Tribler\\Core\\Libtorrent\\__init__.py", line 27, in <lambda>\n    lambda_f = lambda a = args, kwa = kwargs: invoke_func(*a, **kwa)\n  File "C:\\workspace\\jenkins\\workspace\\GH_Tribler_PR_tests_win32\\tribler\\Tribler\\Core\\Libtorrent\\__init__.py", line 28, in invoke_func\n    download.session.lm.threadpool.add_task(lambda_f, 1)\nexceptions.AttributeError: \'NoneType\' object has no attribute \'add_task\'\n', 'log_format': u'{log_text}', 'message': (), 'why': None}]

# Fixed code
def network_get_state(self, usercallback, getpeerlist):
    """Called by network thread"""
    with self.dllock:
        if self.handle is None:
            self._logger.debug(
                "LibtorrentDownloadImpl: network_get_state: Download not running"
            )
            if self.dlstate != DLSTATUS_CIRCUITS:
                progress = self.progressbeforestop
            else:
                tunnel_community = self.ltmgr.trsession.lm.tunnel_community
                progress = (
                    tunnel_community.tunnels_ready(self.get_hops())
                    if tunnel_community
                    else 1
                )

            ds = DownloadState(self, self.dlstate, self.error, progress)
        else:
            (status, stats, seeding_stats, logmsgs) = self.network_get_stats(
                getpeerlist
            )
            ds = DownloadState(
                self,
                status,
                self.error,
                self.get_progress(),
                stats=stats,
                seeding_stats=seeding_stats,
                filepieceranges=self.filepieceranges,
                logmsgs=logmsgs,
            )
            self.progressbeforestop = ds.get_progress()

        if usercallback:
            # Invoke the usercallback function via a new thread.
            # After the callback is invoked, the return values will be passed to the
            # returncallback for post-callback processing.
            if not self.done and not self.session.lm.shutdownstarttime:
                # runs on the reactor
                def session_getstate_usercallback_target():
                    when, getpeerlist = usercallback(ds)
                    if when > 0.0 and not self.session.lm.shutdownstarttime:
                        # Schedule next invocation, either on general or DL specific
                        def reschedule_cb():
                            dc = reactor.callLater(
                                when,
                                lambda: self.network_get_state(
                                    usercallback, getpeerlist
                                ),
                            )
                            self.register_task("downloads_cb", dc)

                        reactor.callFromThread(reschedule_cb)

                reactor.callInThread(session_getstate_usercallback_target)
        else:
            return ds