y-crdt/ypy

Can not access YDoc object created in another thread

trungleduc opened this issue · 2 comments

Hi, I'm having an issue while accessing a YDoc created from another thread. This is the reproduction code and the error message.

import y_py as Y
import threading, queue
q = queue.Queue()
def create_doc(q):
    a = Y.YDoc()
    q.put(a)
thread = threading.Thread(target=create_doc, args=(q,))
thread.start()
thread.join()
doc = q.get()
doc.get_array('foo')
thread '<unnamed>' panicked at 'assertion failed: `(left == right)`
  left: `ThreadId(1)`,
 right: `ThreadId(4)`: y_py::y_doc::YDoc is unsendable, but sent to another thread!', /root/.cargo/registry/src/github.com-1ecc6299db9ec823/pyo3-0.16.6/src/impl_/pyclass.rs:853:9
---------------------------------------------------------------------------
PanicException                            Traceback (most recent call last)
Cell In[15], line 11
      9 thread.join()
     10 doc = q.get()
---> 11 doc.get_array('foo')

PanicException: assertion failed: `(left == right)`
  left: `ThreadId(1)`,
 right: `ThreadId(4)`: y_py::y_doc::YDoc is unsendable, but sent to another thread!

Currently objects in ypy do not implement the Send trait. This is because some of the internals of the yrs Doc type (for the version we use) do not implement Send. In short, you cannot access or pass ypy types between threads. To get around this, you could pass messages between threads using a channel or queue and then integrate those messages into the doc on each thread.