# Buggy code
def save_task_result(self, idents, msg):
    """save the result of a completed task."""
    client_id = idents[0]
    try:
        msg = self.session.unserialize(msg)
    except Exception:
        self.log.error(
            "task::invalid task result message send to %r: %r",
            client_id,
            msg,
            exc_info=True,
        )
        return

    parent = msg["parent_header"]
    if not parent:
        # print msg
        self.log.warn("Task %r had no parent!", msg)
        return
    msg_id = parent["msg_id"]
    if msg_id in self.unassigned:
        self.unassigned.remove(msg_id)

    header = msg["header"]
    engine_uuid = header.get("engine", None)
    eid = self.by_ident.get(engine_uuid, None)

    if msg_id in self.pending:
        self.log.info("task::task %r finished on %s", msg_id, eid)
        self.pending.remove(msg_id)
        self.all_completed.add(msg_id)
        if eid is not None:
            self.completed[eid].append(msg_id)
            if msg_id in self.tasks[eid]:
                self.tasks[eid].remove(msg_id)
        completed = header["date"]
        started = header.get("started", None)
        result = {
            "result_header": header,
            "result_content": msg["content"],
            "started": started,
            "completed": completed,
            "received": datetime.now(),
            "engine_uuid": engine_uuid,
        }

        result["result_buffers"] = msg["buffers"]
        try:
            self.db.update_record(msg_id, result)
        except Exception:
            self.log.error("DB Error saving task request %r", msg_id, exc_info=True)

    else:
        self.log.debug("task::unknown task %r finished", msg_id)


# Traceback (RemoteError)
In [14]: client.resubmit("2e8e2c96-1767-4c7e-b26c-498682fa4c85")
---------------------------------------------------------------------------
RemoteError                               Traceback (most recent call last)
/Users/kaazoo/<ipython-input-15-bd12698bd336> in <module>()
----> 1 client.resubmit("2e8e2c96-1767-4c7e-b26c-498682fa4c85")

/Users/kaazoo/<string> in resubmit(self, indices_or_msg_ids, subheader, block)

/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/IPython/parallel/client/client.pyc in spin_first(f, self, *args, **kwargs)
65     """Call spin() to sync state prior to calling the method."""
66     self.spin()
---> 67     return f(self, *args, **kwargs)
68
69

/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/IPython/parallel/client/client.pyc in resubmit(self, indices_or_msg_ids, subheader, block)
1238         content = msg['content']
1239         if content['status'] != 'ok':
-> 1240             raise self._unwrap_exception(content)
1241
1242         ar = AsyncHubResult(self, msg_ids=theids)

RemoteError: ValueError(Task u'2e8e2c96-1767-4c7e-b26c-498682fa4c85' appears to be inflight)
Traceback (most recent call last):
File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/IPython/parallel/controller/hub.py", line 1149, in resubmit_task
raise ValueError("Task %r appears to be inflight" % msg_id)
ValueError: Task u'2e8e2c96-1767-4c7e-b26c-498682fa4c85' appears to be inflight

# Fixed code
def save_task_result(self, idents, msg):
    """save the result of a completed task."""
    client_id = idents[0]
    try:
        msg = self.session.unserialize(msg)
    except Exception:
        self.log.error(
            "task::invalid task result message send to %r: %r",
            client_id,
            msg,
            exc_info=True,
        )
        return

    parent = msg["parent_header"]
    if not parent:
        # print msg
        self.log.warn("Task %r had no parent!", msg)
        return
    msg_id = parent["msg_id"]
    if msg_id in self.unassigned:
        self.unassigned.remove(msg_id)

    header = msg["header"]
    engine_uuid = header.get("engine", None)
    eid = self.by_ident.get(engine_uuid, None)

    status = header.get("status", None)

    if msg_id in self.pending:
        self.log.info("task::task %r finished on %s", msg_id, eid)
        self.pending.remove(msg_id)
        self.all_completed.add(msg_id)
        if eid is not None:
            if status != "aborted":
                self.completed[eid].append(msg_id)
            if msg_id in self.tasks[eid]:
                self.tasks[eid].remove(msg_id)
        completed = header["date"]
        started = header.get("started", None)
        result = {
            "result_header": header,
            "result_content": msg["content"],
            "started": started,
            "completed": completed,
            "received": datetime.now(),
            "engine_uuid": engine_uuid,
        }

        result["result_buffers"] = msg["buffers"]
        try:
            self.db.update_record(msg_id, result)
        except Exception:
            self.log.error("DB Error saving task request %r", msg_id, exc_info=True)

    else:
        self.log.debug("task::unknown task %r finished", msg_id)