# 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:
                        # 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 (exceptions.AttributeError)
test_normal_torrent (Tribler.Test.API.test_seeding.TestSeeding) ... Unhandled Error
Traceback (most recent call last):
File "/usr/lib/python2.7/threading.py", line 783, in __bootstrap
self.__bootstrap_inner()
File "/usr/lib/python2.7/threading.py", line 810, in __bootstrap_inner
self.run()
File "/usr/lib/python2.7/threading.py", line 763, in run
self.__target(*self.__args, **self.__kwargs)
--- <exception caught here> ---
File "/usr/lib/python2.7/dist-packages/twisted/python/threadpool.py", line 191, in _worker
result = context.call(ctx, function, *args, **kwargs)
File "/usr/lib/python2.7/dist-packages/twisted/python/context.py", line 118, in callWithContext
return self.currentContext().callWithContext(ctx, func, *args, **kw)
File "/usr/lib/python2.7/dist-packages/twisted/python/context.py", line 81, in callWithContext
return func(*args,**kw)
File "/home/jenkins/workspace/GH_Tribler_pull-request-tester-turbo-isolated_devel/tribler/Tribler/Core/Libtorrent/LibtorrentDownloadImpl.py", line 839, in session_getstate_usercallback_target
self.session.lm.threadpool.add_task(lambda: self.network_get_state(usercallback, getpeerlist), when)
exceptions.AttributeError: 'NoneType' object has no attribute 'add_task'
ok

# 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 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