# Buggy code
def _setupHotDeployment(self, userScript=None):
    """
    Determine the user script, save it to the job store and inject a reference to the saved
    copy into the batch system such that it can hot-deploy the resource on the worker
    nodes.

    :param toil.resource.ModuleDescriptor userScript: the module descriptor referencing the
           user script. If None, it will be looked up in the job store.
    """
    if userScript is not None:
        # This branch is hit when a workflow is being started
        if userScript.belongsToToil:
            logger.info(
                "User script %s belongs to Toil. No need to hot-deploy it.", userScript
            )
            userScript = None
        else:
            if self._batchSystem.supportsHotDeployment():
                # Note that by saving the ModuleDescriptor, and not the Resource we allow for
                # redeploying a potentially modified user script on workflow restarts.
                with self._jobStore.writeSharedFileStream("userScript") as f:
                    cPickle.dump(userScript, f, protocol=cPickle.HIGHEST_PROTOCOL)
            else:
                from toil.batchSystems.singleMachine import SingleMachineBatchSystem

                if not isinstance(self._batchSystem, SingleMachineBatchSystem):
                    logger.warn(
                        "Batch system does not support hot-deployment. The user "
                        "script %s will have to be present at the same location on "
                        "every worker.",
                        userScript,
                    )
                userScript = None
    else:
        # This branch is hit on restarts
        from toil.jobStores.abstractJobStore import NoSuchFileException

        try:
            with self._jobStore.readSharedFileStream("userScript") as f:
                userScript = cPickle.load(f)
        except NoSuchFileException:
            logger.info(
                "User script neither set explicitly nor present in the job store."
            )
            userScript = None
    if userScript is None:
        logger.info("No user script to hot-deploy.")
    else:
        logger.info("Saving user script %s as a resource", userScript)
        userScriptResource = userScript.saveAsResourceTo(self._jobStore)
        logger.info("Hot-deploying user script resource %s.", userScriptResource)
        self._batchSystem.setUserScript(userScriptResource)


# Traceback (HTTPError)
WARNING:toil.leader:e8f55793-15ca-471a-947f-38936f917bba:    ---TOIL WORKER OUTPUT LOG---
WARNING:toil.leader:e8f55793-15ca-471a-947f-38936f917bba:    Traceback (most recent call last):
WARNING:toil.leader:e8f55793-15ca-471a-947f-38936f917bba:      File "/usr/local/lib/python2.7/dist-packages/toil/worker.py", line 266, in main
WARNING:toil.leader:e8f55793-15ca-471a-947f-38936f917bba:        job = Job._loadJob(jobWrapper.command, jobStore)
WARNING:toil.leader:e8f55793-15ca-471a-947f-38936f917bba:      File "/usr/local/lib/python2.7/dist-packages/toil/job.py", line 937, in _loadJob
WARNING:toil.leader:e8f55793-15ca-471a-947f-38936f917bba:        userModule = cls._loadUserModule(userModule)
WARNING:toil.leader:e8f55793-15ca-471a-947f-38936f917bba:      File "/usr/local/lib/python2.7/dist-packages/toil/job.py", line 908, in _loadUserModule
WARNING:toil.leader:e8f55793-15ca-471a-947f-38936f917bba:        userModule = userModule.localize()
WARNING:toil.leader:e8f55793-15ca-471a-947f-38936f917bba:      File "/usr/local/lib/python2.7/dist-packages/toil/resource.py", line 416, in localize
WARNING:toil.leader:e8f55793-15ca-471a-947f-38936f917bba:        resource.download(callback=stash)
WARNING:toil.leader:e8f55793-15ca-471a-947f-38936f917bba:      File "/usr/local/lib/python2.7/dist-packages/toil/resource.py", line 152, in download
WARNING:toil.leader:e8f55793-15ca-471a-947f-38936f917bba:        self._save(tempDirPath)
WARNING:toil.leader:e8f55793-15ca-471a-947f-38936f917bba:      File "/usr/local/lib/python2.7/dist-packages/toil/resource.py", line 269, in _save
WARNING:toil.leader:e8f55793-15ca-471a-947f-38936f917bba:        self._download(bytesIO)
WARNING:toil.leader:e8f55793-15ca-471a-947f-38936f917bba:      File "/usr/local/lib/python2.7/dist-packages/toil/resource.py", line 223, in _download
WARNING:toil.leader:e8f55793-15ca-471a-947f-38936f917bba:        with closing(urlopen(self.url)) as content:
WARNING:toil.leader:e8f55793-15ca-471a-947f-38936f917bba:      File "/usr/lib/python2.7/urllib2.py", line 154, in urlopen
WARNING:toil.leader:e8f55793-15ca-471a-947f-38936f917bba:        return opener.open(url, data, timeout)
WARNING:toil.leader:e8f55793-15ca-471a-947f-38936f917bba:      File "/usr/lib/python2.7/urllib2.py", line 437, in open
WARNING:toil.leader:e8f55793-15ca-471a-947f-38936f917bba:        response = meth(req, response)
WARNING:toil.leader:e8f55793-15ca-471a-947f-38936f917bba:      File "/usr/lib/python2.7/urllib2.py", line 550, in http_response
WARNING:toil.leader:e8f55793-15ca-471a-947f-38936f917bba:        'http', request, response, code, msg, hdrs)
WARNING:toil.leader:e8f55793-15ca-471a-947f-38936f917bba:      File "/usr/lib/python2.7/urllib2.py", line 475, in error
WARNING:toil.leader:e8f55793-15ca-471a-947f-38936f917bba:        return self._call_chain(*args)
WARNING:toil.leader:e8f55793-15ca-471a-947f-38936f917bba:      File "/usr/lib/python2.7/urllib2.py", line 409, in _call_chain
WARNING:toil.leader:e8f55793-15ca-471a-947f-38936f917bba:        result = func(*args)
WARNING:toil.leader:e8f55793-15ca-471a-947f-38936f917bba:      File "/usr/lib/python2.7/urllib2.py", line 558, in http_error_default
WARNING:toil.leader:e8f55793-15ca-471a-947f-38936f917bba:        raise HTTPError(req.get_full_url(), code, msg, hdrs, fp)
WARNING:toil.leader:e8f55793-15ca-471a-947f-38936f917bba:    HTTPError: HTTP Error 400: Bad Request
WARNING:toil.leader:e8f55793-15ca-471a-947f-38936f917bba:    Exiting the worker because of a failed jobWrapper on host ip-172-31-45-12
WARNING:toil.leader:e8f55793-15ca-471a-947f-38936f917bba:    ERROR:toil.worker:Exiting the worker because of a failed jobWrapper on host ip-172-31-45-12
WARNING:toil.leader:e8f55793-15ca-471a-947f-38936f917bba:    WARNING:toil.jobWrapper:Due to failure we are reducing the remaining retry count of job e8f55793-15ca-471a-947f-38936f917bba to 2

# Fixed code
def _setupHotDeployment(self, userScript=None):
    """
    Determine the user script, save it to the job store and inject a reference to the saved
    copy into the batch system such that it can hot-deploy the resource on the worker
    nodes.

    :param toil.resource.ModuleDescriptor userScript: the module descriptor referencing the
           user script. If None, it will be looked up in the job store.
    """
    if userScript is not None:
        # This branch is hit when a workflow is being started
        if userScript.belongsToToil:
            logger.info(
                "User script %s belongs to Toil. No need to hot-deploy it.", userScript
            )
            userScript = None
        else:
            if self._batchSystem.supportsHotDeployment():
                # Note that by saving the ModuleDescriptor, and not the Resource we allow for
                # redeploying a potentially modified user script on workflow restarts.
                with self._jobStore.writeSharedFileStream("userScript") as f:
                    cPickle.dump(userScript, f, protocol=cPickle.HIGHEST_PROTOCOL)
            else:
                from toil.batchSystems.singleMachine import SingleMachineBatchSystem

                if not isinstance(self._batchSystem, SingleMachineBatchSystem):
                    logger.warn(
                        "Batch system does not support hot-deployment. The user "
                        "script %s will have to be present at the same location on "
                        "every worker.",
                        userScript,
                    )
                userScript = None
    else:
        # This branch is hit on restarts
        from toil.jobStores.abstractJobStore import NoSuchFileException

        try:
            with self._jobStore.readSharedFileStream("userScript") as f:
                userScript = cPickle.load(f)
        except NoSuchFileException:
            logger.info(
                "User script neither set explicitly nor present in the job store."
            )
            userScript = None
    if userScript is None:
        logger.info("No user script to hot-deploy.")
    else:
        logger.debug("Saving user script %s as a resource", userScript)
        userScriptResource = userScript.saveAsResourceTo(self._jobStore)
        logger.debug("Injecting user script %s into batch system.", userScriptResource)
        self._batchSystem.setUserScript(userScriptResource)
        thread = Thread(
            target=self._refreshUserScript,
            name="refreshUserScript",
            kwargs=dict(userScriptResource=userScriptResource),
        )
        thread.daemon = True
        thread.start()