# Buggy code
def render_POST(self, request):
    parameters = recursive_unicode(http.parse_qs(request.content.read(), 1))
    if "subscribe" not in parameters:
        request.setResponseCode(http.BAD_REQUEST)
        return json.twisted_dumps(
            {"success": False, "error": "subscribe parameter missing"}
        )

    to_subscribe = int(parameters["subscribe"][0]) == 1
    with db_session:
        channel = self.session.lm.mds.ChannelMetadata.get_for_update(
            public_key=database_blob(self.channel_pk), id_=self.channel_id
        )
        if not channel:
            request.setResponseCode(http.NOT_FOUND)
            return json.twisted_dumps({"error": "this channel cannot be found"})

        channel.subscribed = to_subscribe
        channel.share = to_subscribe
        if not to_subscribe:
            channel.local_version = 0
        channel_state = channel.to_simple_dict()["state"]

    def delete_channel():
        # TODO: this should be eventually moved to a garbage-collector-like subprocess in MetadataStore
        with db_session:
            channel = self.session.lm.mds.ChannelMetadata.get_for_update(
                public_key=database_blob(self.channel_pk), id_=self.channel_id
            )
            contents = channel.contents
            contents.delete(bulk=True)
        self.session.lm.mds._db.disconnect()

    if not to_subscribe:
        reactor.callInThread(delete_channel)

    return json.twisted_dumps(
        {"success": True, "subscribed": to_subscribe, "state": channel_state}
    )


# Traceback (RuntimeError)
Traceback (most recent call last):
File "TriblerGUI\core_manager.py", line 121, in on_received_state
RuntimeError: [Failure instance: Traceback: <class 'AssertionError'>: INFOHASH has invalid length: 48
Tribler\Core\Modules\gigachannel_manager.py:112:service_channels
Tribler\Core\Modules\gigachannel_manager.py:150:check_channels_updates
lib\site-packages\twisted\internet\defer.py:1613:unwindGenerator
lib\site-packages\twisted\internet\defer.py:1529:_cancellableInlineCallbacks
--- <exception caught here> ---
lib\site-packages\twisted\internet\defer.py:1418:_inlineCallbacks
Tribler\Core\Modules\gigachannel_manager.py:226:download_channel
Tribler\Core\TorrentDef.py:494:__init__
]

# Fixed code
def render_POST(self, request):
    parameters = recursive_unicode(http.parse_qs(request.content.read(), 1))
    if "subscribe" not in parameters:
        request.setResponseCode(http.BAD_REQUEST)
        return json.twisted_dumps(
            {"success": False, "error": "subscribe parameter missing"}
        )

    to_subscribe = int(parameters["subscribe"][0]) == 1
    with db_session:
        channel = self.session.lm.mds.ChannelMetadata.get_for_update(
            public_key=database_blob(self.channel_pk), id_=self.channel_id
        )
        if not channel:
            request.setResponseCode(http.NOT_FOUND)
            return json.twisted_dumps({"error": "this channel cannot be found"})

        if channel.status == LEGACY_ENTRY:
            return json.twisted_dumps({"error": "Cannot subscribe to a legacy channel"})

        channel.subscribed = to_subscribe
        channel.share = to_subscribe
        if not to_subscribe:
            channel.local_version = 0
        channel_state = channel.to_simple_dict()["state"]

    def delete_channel():
        # TODO: this should be eventually moved to a garbage-collector-like subprocess in MetadataStore
        with db_session:
            channel = self.session.lm.mds.ChannelMetadata.get_for_update(
                public_key=database_blob(self.channel_pk), id_=self.channel_id
            )
            contents = channel.contents
            contents.delete(bulk=True)
        self.session.lm.mds._db.disconnect()

    if not to_subscribe:
        reactor.callInThread(delete_channel)

    return json.twisted_dumps(
        {"success": True, "subscribed": to_subscribe, "state": channel_state}
    )