def _worker_handler(future, worker, pipe, timeout):
    """Worker lifecycle manager.

    Waits for the worker to be perform its task,
    collects result, runs the callback and cleans up the process.

    """
    result = _get_result(future, pipe, timeout)

    if isinstance(result, BaseException):
        if isinstance(result, ProcessExpired):
            result.exitcode = worker.exitcode
        if not isinstance(result, CancelledError):
            future.set_exception(result)
    else:
        future.set_result(result)

    if worker.is_alive():
        stop_process(worker)