# Buggy code
def writeGlobalFile(self, localFileName, cleanup=False):
    """
    Takes a file (as a path) and uploads it to to the global file store,
    returns an ID that can be used to retrieve the file.

    If cleanup is True then the global file will be deleted once the job
    and all its successors have completed running. If not the global file
    must be deleted manually.

    The write is asynchronous, so further modifications during execution
    to the file pointed by localFileName will result in undetermined behavior.
    """
    jobStoreFileID = self.jobStore.getEmptyFileStoreID(
        None if not cleanup else self.jobWrapper.jobStoreID
    )
    self.queue.put((open(localFileName, "r"), jobStoreFileID))
    # Now put the file into the cache if it is a path within localTempDir
    absLocalFileName = os.path.abspath(localFileName)
    if absLocalFileName.startswith(self.localTempDir):
        # Chmod to make file read only
        os.chmod(absLocalFileName, stat.S_IRUSR | stat.S_IRGRP | stat.S_IROTH)
        self.jobStoreFileIDToCacheLocation[jobStoreFileID] = absLocalFileName
    return jobStoreFileID


# Traceback (RuntimeError)
[centos@arjun-fake-podk toil_tests]$ python module_tests.py
No handlers could be found for logger "toil.resource"
INFO:toil.lib.bioio:Logging set at level: DEBUG
INFO:toil.lib.bioio:Logging to file: /data1/star_log
INFO:toil.common:Using the single machine batch system
INFO:toil.jobStores.fileJobStore:Jobstore directory is: /data1/toil_tempdir
WARNING:toil.batchSystems.singleMachine:Limiting maxCores to CPU count of system (8).
INFO:toil.batchSystems.singleMachine:Setting up the thread pool with 80 workers, given a minimum CPU fraction of 0.100000 and a maximum CPU value of 8.
INFO:toil.common:Written the environment for the jobs to the environment file
WARNING:toil.resource:Can't globalize module ModuleDescriptor(dirPath='/usr/local/lib/python2.7/site-packages', name='toil.job', extension='.pyc').
WARNING:toil.resource:Can't globalize module ModuleDescriptor(dirPath='/usr/local/lib/python2.7/site-packages', name='toil.job', extension='.pyc').
WARNING:toil.resource:Can't globalize module ModuleDescriptor(dirPath='/usr/local/lib/python2.7/site-packages', name='toil.job', extension='.pyc').
INFO:toil.leader:Checked batch system has no running jobs and no updated jobs
INFO:toil.leader:Found 1 jobs to start and 0 jobs with successors to run
INFO:toil.leader:Starting the main loop
DEBUG:toil.leader:Built the jobs list, currently have 1 jobs to update and 0 jobs issued
DEBUG:toil.batchSystems.singleMachine:Issuing the command: /usr/local/bin/python -E /usr/local/lib/python2.7/site-packages/toil/worker.py /data1/toil_tempdir e/j/jobaSjV4A with memory: 2147483648, cores: 1, disk: 2147483648
DEBUG:toil.leader:Issued job with job store ID: e/j/jobaSjV4A and job batch system ID: 0 and cores: 1, disk: 2147483648, and memory: 2147483648
DEBUG:toil.batchSystems.singleMachine:Acquiring 2147483648 bytes of memory from a pool of 9223372036854775807.
DEBUG:toil.batchSystems.singleMachine:Acquiring 10 fractional cores from a pool of 80 to satisfy a request of 1.000000 cores
INFO:toil.batchSystems.singleMachine:Executing command: '/usr/local/bin/python -E /usr/local/lib/python2.7/site-packages/toil/worker.py /data1/toil_tempdir e/j/jobaSjV4A'.
Exception in thread Thread-13:
Traceback (most recent call last):
File "/usr/local/lib/python2.7/threading.py", line 810, in __bootstrap_inner
self.run()
File "/usr/local/lib/python2.7/threading.py", line 763, in run
self.__target(*self.__args, **self.__kwargs)
File "/usr/local/lib/python2.7/site-packages/toil/job.py", line 398, in asyncWrite
raise RuntimeError("The termination flag is set, exiting")
RuntimeError: The termination flag is set, exiting

Exception in thread Thread-14:
Traceback (most recent call last):
File "/usr/local/lib/python2.7/threading.py", line 810, in __bootstrap_inner
self.run()
File "/usr/local/lib/python2.7/threading.py", line 763, in run
self.__target(*self.__args, **self.__kwargs)
File "/usr/local/lib/python2.7/site-packages/toil/job.py", line 398, in asyncWrite
raise RuntimeError("The termination flag is set, exiting")
RuntimeError: The termination flag is set, exiting

Exception RuntimeError: RuntimeError('cannot join current thread',) in <bound method FileStore.__del__ of <toil.job.FileStore object at 0x7f5142899f90>> ignored

# Fixed code
def writeGlobalFile(self, localFileName, cleanup=False):
    """
    Takes a file (as a path) and uploads it to to the global file store,
    returns an ID that can be used to retrieve the file.

    If cleanup is True then the global file will be deleted once the job
    and all its successors have completed running. If not the global file
    must be deleted manually.

    If the local file is a file returned by Job.FileStore.getLocalTempFile
    or is in a directory, or, recursively, a subdirectory, returned by
    Job.FileStore.getLocalTempDir then the write is asynchronous,
    so further modifications during execution to the file pointed by
    localFileName will result in undetermined behavior. Otherwise, the
    method will block until the file is written to the file store.
    """
    # Put the file into the cache if it is a path within localTempDir
    absLocalFileName = os.path.abspath(localFileName)
    cleanupID = None if not cleanup else self.jobWrapper.jobStoreID
    if absLocalFileName.startswith(self.localTempDir):
        jobStoreFileID = self.jobStore.getEmptyFileStoreID(cleanupID)
        self.queue.put((open(absLocalFileName, "r"), jobStoreFileID))
        # Chmod to make file read only to try to prevent accidental user modification
        os.chmod(absLocalFileName, stat.S_IRUSR | stat.S_IRGRP | stat.S_IROTH)
        self.jobStoreFileIDToCacheLocation[jobStoreFileID] = absLocalFileName
    else:
        # Write the file directly to the file store
        jobStoreFileID = self.jobStore.writeFile(localFileName, cleanupID)
    return jobStoreFileID