# Buggy code
def early_shutdown(self):
    """Called as soon as Session shutdown is initiated. Used to start
    shutdown tasks that takes some time and that can run in parallel
    to checkpointing, etc.
    :returns a Deferred that will fire once all dependencies acknowledge they have shutdown.
    """
    self._logger.info("tlm: early_shutdown")

    self.cancel_all_pending_tasks()

    # Note: session_lock not held
    self.shutdownstarttime = timemod.time()
    if self.boosting_manager:
        yield self.boosting_manager.shutdown()
    self.boosting_manager = None

    if self.torrent_checker:
        yield self.torrent_checker.shutdown()
    self.torrent_checker = None

    if self.channel_manager:
        yield self.channel_manager.shutdown()
    self.channel_manager = None

    if self.search_manager:
        yield self.search_manager.shutdown()
    self.search_manager = None

    if self.rtorrent_handler:
        yield self.rtorrent_handler.shutdown()
    self.rtorrent_handler = None

    if self.video_server:
        yield self.video_server.shutdown_server()
    self.video_server = None

    if self.version_check_manager:
        self.version_check_manager.stop()
    self.version_check_manager = None

    if self.resource_monitor:
        self.resource_monitor.stop()
    self.resource_monitor = None

    if self.tracker_manager:
        yield self.tracker_manager.shutdown()
    self.tracker_manager = None

    if self.dispersy:
        self._logger.info("lmc: Shutting down Dispersy...")
        now = timemod.time()
        try:
            success = yield self.dispersy.stop()
        except:
            print_exc()
            success = False

        diff = timemod.time() - now
        if success:
            self._logger.info(
                "lmc: Dispersy successfully shutdown in %.2f seconds", diff
            )
        else:
            self._logger.info("lmc: Dispersy failed to shutdown in %.2f seconds", diff)

    if self.metadata_store is not None:
        yield self.metadata_store.close()
    self.metadata_store = None

    if self.tftp_handler is not None:
        yield self.tftp_handler.shutdown()
    self.tftp_handler = None

    if self.channelcast_db is not None:
        yield self.channelcast_db.close()
    self.channelcast_db = None

    if self.votecast_db is not None:
        yield self.votecast_db.close()
    self.votecast_db = None

    if self.mypref_db is not None:
        yield self.mypref_db.close()
    self.mypref_db = None

    if self.torrent_db is not None:
        yield self.torrent_db.close()
    self.torrent_db = None

    if self.peer_db is not None:
        yield self.peer_db.close()
    self.peer_db = None

    if self.mainline_dht is not None:
        from Tribler.Core.DecentralizedTracking import mainlineDHT

        yield mainlineDHT.deinit(self.mainline_dht)
    self.mainline_dht = None

    if self.torrent_store is not None:
        yield self.torrent_store.close()
    self.torrent_store = None

    if self.api_manager is not None:
        yield self.api_manager.stop()
    self.api_manager = None

    if self.watch_folder is not None:
        yield self.watch_folder.stop()
    self.watch_folder = None


# Traceback (AssertionError)
Tuples differ: (2, ('127.0.0.1', [5, 4, 3, 2,... != (2, ('127.0.0.1', [5, 4, 3, 2,...

First differing element 2:
u'None'
''

- (2, ('127.0.0.1', [5, 4, 3, 2, 1]), u'None')
?                                     - ----

+ (2, ('127.0.0.1', [5, 4, 3, 2, 1]), '')
Stacktrace

Traceback (most recent call last):
File "c:\python27\lib\unittest\case.py", line 329, in run
testMethod()
File "C:\workspace\jenkins\workspace\GH_Tribler_PR_tests_win64\tribler\Tribler\Test\test_as_server.py", line 58, in check
result = fun(*argv, **kwargs)
File "C:\workspace\jenkins\workspace\GH_Tribler_PR_tests_win64\tribler\Tribler\Test\Core\Upgrade\test_config_upgrade_70_71.py", line 33, in test_read_test_libtribler_conf
self.assertEqual(result_config.get_anon_proxy_settings(), (2, ("127.0.0.1", [5, 4, 3, 2, 1]), ''))
File "c:\python27\lib\unittest\case.py", line 513, in assertEqual
assertion_func(first, second, msg=msg)
File "c:\python27\lib\unittest\case.py", line 753, in assertTupleEqual
self.assertSequenceEqual(tuple1, tuple2, msg, seq_type=tuple)
File "c:\python27\lib\unittest\case.py", line 724, in assertSequenceEqual
self.fail(msg)
File "c:\python27\lib\unittest\case.py", line 410, in fail
raise self.failureException(msg)
AssertionError: Tuples differ: (2, ('127.0.0.1', [5, 4, 3, 2,... != (2, ('127.0.0.1', [5, 4, 3, 2,...

First differing element 2:
u'None'
''

- (2, ('127.0.0.1', [5, 4, 3, 2, 1]), u'None')
?                                     - ----

+ (2, ('127.0.0.1', [5, 4, 3, 2, 1]), '')

# Fixed code
def early_shutdown(self):
    """Called as soon as Session shutdown is initiated. Used to start
    shutdown tasks that takes some time and that can run in parallel
    to checkpointing, etc.
    :returns a Deferred that will fire once all dependencies acknowledge they have shutdown.
    """
    self._logger.info("tlm: early_shutdown")

    self.cancel_all_pending_tasks()

    # Note: session_lock not held
    self.shutdownstarttime = timemod.time()
    if self.boosting_manager:
        yield self.boosting_manager.shutdown()
    self.boosting_manager = None

    if self.torrent_checker:
        yield self.torrent_checker.shutdown()
    self.torrent_checker = None

    if self.channel_manager:
        yield self.channel_manager.shutdown()
    self.channel_manager = None

    if self.search_manager:
        yield self.search_manager.shutdown()
    self.search_manager = None

    if self.rtorrent_handler:
        yield self.rtorrent_handler.shutdown()
    self.rtorrent_handler = None

    if self.video_server:
        yield self.video_server.shutdown_server()
    self.video_server = None

    if self.version_check_manager:
        self.version_check_manager.stop()
    self.version_check_manager = None

    if self.resource_monitor:
        self.resource_monitor.stop()
    self.resource_monitor = None

    self.tracker_manager = None

    if self.dispersy:
        self._logger.info("lmc: Shutting down Dispersy...")
        now = timemod.time()
        try:
            success = yield self.dispersy.stop()
        except:
            print_exc()
            success = False

        diff = timemod.time() - now
        if success:
            self._logger.info(
                "lmc: Dispersy successfully shutdown in %.2f seconds", diff
            )
        else:
            self._logger.info("lmc: Dispersy failed to shutdown in %.2f seconds", diff)

    if self.metadata_store is not None:
        yield self.metadata_store.close()
    self.metadata_store = None

    if self.tftp_handler is not None:
        yield self.tftp_handler.shutdown()
    self.tftp_handler = None

    if self.channelcast_db is not None:
        yield self.channelcast_db.close()
    self.channelcast_db = None

    if self.votecast_db is not None:
        yield self.votecast_db.close()
    self.votecast_db = None

    if self.mypref_db is not None:
        yield self.mypref_db.close()
    self.mypref_db = None

    if self.torrent_db is not None:
        yield self.torrent_db.close()
    self.torrent_db = None

    if self.peer_db is not None:
        yield self.peer_db.close()
    self.peer_db = None

    if self.mainline_dht is not None:
        from Tribler.Core.DecentralizedTracking import mainlineDHT

        yield mainlineDHT.deinit(self.mainline_dht)
    self.mainline_dht = None

    if self.torrent_store is not None:
        yield self.torrent_store.close()
    self.torrent_store = None

    if self.api_manager is not None:
        yield self.api_manager.stop()
    self.api_manager = None

    if self.watch_folder is not None:
        yield self.watch_folder.stop()
    self.watch_folder = None