Repository: ipython/ipython

Issue:
task fails with "AssertionError: not enough buffers!" after second resubmit
When I create a task and resubmit it after it has completed, it runs a second time as expected. But when a resubmit it once again, the task fails with "AssertionError: not enough buffers!":

``` python
In [15]: client.db_query({"msg_id" : "68a7868d-e3b1-407e-a208-cc0464019e7c"})
Out[15]: 
[{u'client_uuid': None,
  u'completed': datetime.datetime(2012, 6, 4, 8, 37, 21, 960000),
  u'content': {},
  u'engine_uuid': u'1b2756fe-bc92-46bd-b3cd-4679ccb2d664',
  u'header': {u'after': [],
   u'date': datetime.datetime(2012, 6, 4, 8, 37, 21, 951000),
   u'follow': [],
   u'msg_id': u'68a7868d-e3b1-407e-a208-cc0464019e7c',
   u'msg_type': u'apply_request',
   u'retries': 0,
   u'session': u'4fcc572653c27b0d89000000',
   u'targets': [],
   u'timeout': 0.0,
   u'username': u'kaazoo'},
  u'msg_id': u'68a7868d-e3b1-407e-a208-cc0464019e7c',
  u'pyerr': {u'ename': u'AssertionError',
   u'engine_info': {u'engine_id': 0,
    u'engine_uuid': u'1b2756fe-bc92-46bd-b3cd-4679ccb2d664',
    u'method': u'apply'},
   u'evalue': u'not enough buffers!',
   u'status': u'error',
   u'traceback': [u'Traceback (most recent call last):\n',
    u'  File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/IPython/zmq/ipkernel.py", line 547, in apply_request\n    f,args,kwargs = unpack_apply_message(bufs, working, copy=False)\n',
    u'  File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/IPython/zmq/serialize.py", line 130, in unpack_apply_message\n    assert len(bufs) >= 3, "not enough buffers!"\n',
    u'AssertionError: not enough buffers!\n']},
  u'pyin': None,
  u'pyout': None,
  u'queue': None,
  u'received': datetime.datetime(2012, 6, 4, 8, 37, 21, 979000),
  u'resubmitted': None,
  u'result_content': {u'ename': u'AssertionError',
   u'engine_info': {u'engine_id': 0,
    u'engine_uuid': u'1b2756fe-bc92-46bd-b3cd-4679ccb2d664',
    u'method': u'apply'},
   u'evalue': u'not enough buffers!',
   u'status': u'error',
   u'traceback': [u'Traceback (most recent call last):\n',
    u'  File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/IPython/zmq/ipkernel.py", line 547, in apply_request\n    f,args,kwargs = unpack_apply_message(bufs, working, copy=False)\n',
    u'  File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/IPython/zmq/serialize.py", line 130, in unpack_apply_message\n    assert len(bufs) >= 3, "not enough buffers!"\n',
    u'AssertionError: not enough buffers!\n']},
  u'result_header': {u'date': datetime.datetime(2012, 6, 4, 8, 37, 21, 960000),
   u'dependencies_met': True,
   u'engine': u'1b2756fe-bc92-46bd-b3cd-4679ccb2d664',
   u'msg_id': u'cb464dc5-c473-49e7-aaf0-81e894f75390',
   u'msg_type': u'apply_reply',
   u'session': u'1b2756fe-bc92-46bd-b3cd-4679ccb2d664',
   u'started': datetime.datetime(2012, 6, 4, 8, 37, 21, 960000),
   u'status': u'error',
   u'username': u'kaazoo'},
  u'started': datetime.datetime(2012, 6, 4, 8, 37, 21, 960000),
  u'stderr': u'',
  u'stdout': u'',
  u'submitted': datetime.datetime(2012, 6, 4, 8, 37, 21, 951000)}]
```



Discussion:
Just for your information, I'm working with Python 2.7.2, pyzmq 2.1.11 and IPython from current Git master branch.


Resolving patch:
diff --git a/IPython/parallel/controller/hub.py b/IPython/parallel/controller/hub.py
--- a/IPython/parallel/controller/hub.py
+++ b/IPython/parallel/controller/hub.py
@@ -1163,7 +1163,7 @@ def finish(reply):
         # send the messages
         for rec in records:
             header = rec['header']
-            msg = self.session.msg(header['msg_type'])
+            msg = self.session.msg(header['msg_type'], parent=header)
             msg_id = msg['msg_id']
             msg['content'] = rec['content']
             
@@ -1177,7 +1177,7 @@ def finish(reply):
 
             resubmitted[rec['msg_id']] = msg_id
             self.pending.add(msg_id)
-            msg['buffers'] = []
+            msg['buffers'] = rec['buffers']
             try:
                 self.db.add_record(msg_id, init_record(msg))
             except Exception:


Test patch:
diff --git a/IPython/parallel/tests/test_client.py b/IPython/parallel/tests/test_client.py
--- a/IPython/parallel/tests/test_client.py
+++ b/IPython/parallel/tests/test_client.py
@@ -312,6 +312,20 @@ def f():
         r2 = ahr.get(1)
         self.assertFalse(r1 == r2)
 
+    def test_resubmit_chain(self):
+        """resubmit resubmitted tasks"""
+        v = self.client.load_balanced_view()
+        ar = v.apply_async(lambda x: x, 'x'*1024)
+        ar.get()
+        self._wait_for_idle()
+        ars = [ar]
+        
+        for i in range(10):
+            ar = ars[-1]
+            ar2 = self.client.resubmit(ar.msg_ids)
+        
+        [ ar.get() for ar in ars ]
+
     def test_resubmit_header(self):
         """resubmit shouldn't clobber the whole header"""
         def f():