def kill_all_tasks_in_cgroup(cgroup, kill_process_fn):
    tasksFile = os.path.join(cgroup, 'tasks')
    freezer_file = os.path.join(cgroup, 'freezer.state')

    def try_write_to_freezer(content):
        try:
            util.write_file(content, freezer_file)
        except __HOLE__:
            pass # expected if freezer not enabled, we try killing without it

    i = 0
    while True:
        i += 1
        # TODO We can probably remove this loop over signals and just send
        # SIGKILL. We added this loop when killing sub-processes was not reliable
        # and we did not know why, but now it is reliable.
        for sig in [signal.SIGKILL, signal.SIGINT, signal.SIGTERM]:
            try_write_to_freezer('FROZEN')
            with open(tasksFile, 'rt') as tasks:
                task = None
                for task in tasks:
                    task = task.strip()
                    if i > 1:
                        logging.warning('Run has left-over process with pid %s '
                                        'in cgroup %s, sending signal %s (try %s).',
                                        task, cgroup, sig, i)
                    kill_process_fn(int(task), sig)

                if task is None:
                    return # No process was hanging, exit
            try_write_to_freezer('THAWED')
            time.sleep(i * 0.5) # wait for the process to exit, this might take some time
dataset/ETHPy150Open sosy-lab/benchexec/benchexec/cgroups.py/kill_all_tasks_in_cgroup