Skip to content

Possible race condition in dask.utils:Dispatch.dispatch #9181

Closed

Description

@zklaus

What happened:
When using a large number of tasks with np.ma.core.MaskedArray blocks, sometimes the scheduler loses the ability to deserialize them. I think I have tracked this down to a possible race condition in dask.utils:Dispatch.dispatch, where the lazy registration function is popped off before the registration. If another dispatch happens between those two events, the implementation is not yet in self._lookup, but also the lazy registration function is no longer present and thus dask concludes that it can not deserialize the item.

What you expected to happen:
That dask/distributed retains the ability to deserialize masked arrays.

Minimal Complete Verifiable Example:

I don't have a complete verifiable example due to the tricky nature of reproducing race conditions.

Environment:

  • Dask version: main
  • Python version: Python 3.8.13 | packaged by conda-forge
  • Operating System: Linux
  • Install method (conda, pip, source): Conda environment + dask and distributed source
Cluster Dump State:

Activity

  1. added
    needs triage
    on Jun 10, 2022
  2. pavithraes commented on Jun 14, 2022

    @pavithraes
    Member

    @zklaus Thanks for sharing!

    I see the dispatch function was updated in #8185. I believe @jcrist is on vacation, @jsignell do you have thoughts on this since you reviewed that PR?

  3. jrbourbeau commented on Jun 14, 2022

    @jrbourbeau
    Member

    @zklaus do you have any traceback / error messages / logs you can post? I'm curious under what circumstances the distributed scheduler is running Dispatch-related code in multiple threads

  4. zklaus commented on Jun 15, 2022

    @zklaus
    Author

    I attach a slurm log file. slurm-18620772.out.txt
    The interesting bits are in lines 93795ff, which I reproduce below.
    Note that I added debug output that shows the self._lookup and self._lazy fields and that Dispatch has lost the ability to deserialize masked arrays, but this is after many, many masked arrays have been deserialized in this same run before.

    It is perfectly possible that this has nothing to do with any race condition, but I have a local fix using RWLockWrite that works well.

      93795 2022-06-09 12:30:36,526 - distributed.protocol.core - CRITICAL - Failed to deserialize
      93796 Traceback (most recent call last):
      93797   File "/home/sm_klazi/src/git/distributed/distributed/protocol/core.py", line 159, in loads
      93798     return msgpack.loads(
      93799   File "msgpack/_unpacker.pyx", line 194, in msgpack._cmsgpack.unpackb
      93800   File "/home/sm_klazi/src/git/distributed/distributed/protocol/core.py", line 139, in _decode_default
      93801     return merge_and_deserialize(
      93802   File "/home/sm_klazi/src/git/distributed/distributed/protocol/serialize.py", line 487, in merge_and_deserialize
      93803     return deserialize(header, merged_frames, deserializers=deserializers)
      93804   File "/home/sm_klazi/src/git/distributed/distributed/protocol/serialize.py", line 416, in deserialize
      93805     return loads(header, frames)
      93806   File "/home/sm_klazi/src/git/distributed/distributed/protocol/serialize.py", line 57, in dask_loads
      93807     loads = dask_deserialize.dispatch(typ)
      93808   File "/home/sm_klazi/src/git/dask/dask/utils.py", line 600, in dispatch
      93809     raise TypeError(f"No dispatch for {cls}. Only have {self._lookup} and {self._lazy}")
      93810 TypeError: No dispatch for <class 'numpy.ma.core.MaskedArray'>. Only have {<class 'bytes'>: <function _deserialize_bytes at 0x2b5a4db8daf0>, <class 'bytearray'>: <function _deserialize_bytearray at 0x2b5a4db8db80>, <class 'array.array'>: <function _deserialize_a  93810 rray at 0x2b5a4db8dca0>, <class 'memoryview'>: <function _deserialize_memoryview at 0x2b5a4db8ddc0>, <class 'dict'>: <bound method ObjectDictSerializer.deserialize of <distributed.protocol.serialize.ObjectDictSerializer object at 0x2b5a4db87a60>>} and {'scipy':   93810 <function _register_scipy at 0x2b5a4db904c0>, 'h5py': <function _register_h5py at 0x2b5a4db90550>, 'netCDF4': <function _register_netcdf4 at 0x2b5a4db905e0>, 'keras': <function _register_keras at 0x2b5a4db90670>, 'sparse': <function _register_sparse at 0x2b5a4db  93810 90700>, 'pyarrow': <function _register_arrow at 0x2b5a4db90790>, 'torchvision': <function _register_torch at 0x2b5a4db90940>, 'torch': <function _register_torch at 0x2b5a4db90940>, 'cupyx': <function _register_cupy at 0x2b5a4db90c10>, 'cupy': <function _register  93810 _cupy at 0x2b5a4db90c10>, 'numba': <function _register_numba at 0x2b5a4db909d0>, 'rmm': <function _register_rmm at 0x2b5a4db90a60>, 'dask_cudf': <function _register_cudf at 0x2b5a4db90dc0>, 'cudf': <function _register_cudf at 0x2b5a4db90dc0>, 'cuml': <function _  93810 register_cuml at 0x2b5a4db90af0>}
      93811 2022-06-09 12:30:36,552 - distributed.worker - ERROR - No dispatch for <class 'numpy.ma.core.MaskedArray'>. Only have {<class 'bytes'>: <function _deserialize_bytes at 0x2b5a4db8daf0>, <class 'bytearray'>: <function _deserialize_bytearray at 0x2b5a4db8db80>, <cl  93811 ass 'array.array'>: <function _deserialize_array at 0x2b5a4db8dca0>, <class 'memoryview'>: <function _deserialize_memoryview at 0x2b5a4db8ddc0>, <class 'dict'>: <bound method ObjectDictSerializer.deserialize of <distributed.protocol.serialize.ObjectDictSerialize  93811 r object at 0x2b5a4db87a60>>} and {'scipy': <function _register_scipy at 0x2b5a4db904c0>, 'h5py': <function _register_h5py at 0x2b5a4db90550>, 'netCDF4': <function _register_netcdf4 at 0x2b5a4db905e0>, 'keras': <function _register_keras at 0x2b5a4db90670>, 'spar  93811 se': <function _register_sparse at 0x2b5a4db90700>, 'pyarrow': <function _register_arrow at 0x2b5a4db90790>, 'torchvision': <function _register_torch at 0x2b5a4db90940>, 'torch': <function _register_torch at 0x2b5a4db90940>, 'cupyx': <function _register_cupy at   93811 0x2b5a4db90c10>, 'cupy': <function _register_cupy at 0x2b5a4db90c10>, 'numba': <function _register_numba at 0x2b5a4db909d0>, 'rmm': <function _register_rmm at 0x2b5a4db90a60>, 'dask_cudf': <function _register_cudf at 0x2b5a4db90dc0>, 'cudf': <function _register_  93811 cudf at 0x2b5a4db90dc0>, 'cuml': <function _register_cuml at 0x2b5a4db90af0>}
      93812 Traceback (most recent call last):
      93813   File "/home/sm_klazi/src/git/distributed/distributed/worker.py", line 3290, in gather_dep
      93814     response = await get_data_from_worker(
      93815   File "/home/sm_klazi/src/git/distributed/distributed/worker.py", line 4611, in get_data_from_worker
      93816     return await retry_operation(_get_data, operation="get_data_from_worker")
      93817   File "/home/sm_klazi/src/git/distributed/distributed/utils_comm.py", line 381, in retry_operation
      93818     return await retry(
      93819   File "/home/sm_klazi/src/git/distributed/distributed/utils_comm.py", line 366, in retry
      93820     return await coro()
      93821   File "/home/sm_klazi/src/git/distributed/distributed/worker.py", line 4591, in _get_data
      93822     response = await send_recv(
      93823   File "/home/sm_klazi/src/git/distributed/distributed/core.py", line 748, in send_recv
      93824     response = await comm.read(deserializers=deserializers)
      93825   File "/home/sm_klazi/src/git/distributed/distributed/comm/tcp.py", line 254, in read
      93826     msg = await from_frames(
      93827   File "/home/sm_klazi/src/git/distributed/distributed/comm/utils.py", line 96, in from_frames
      93828     res = await offload(_from_frames)
      93829   File "/home/sm_klazi/src/git/distributed/distributed/utils.py", line 1428, in offload
      93830     return await loop.run_in_executor(
      93831   File "/home/sm_klazi/.conda/envs/midas-production-be/lib/python3.8/concurrent/futures/thread.py", line 57, in run
      93832     result = self.fn(*self.args, **self.kwargs)
      93833   File "/home/sm_klazi/src/git/distributed/distributed/utils.py", line 1429, in <lambda>
      93834     _offload_executor, lambda: context.run(fn, *args, **kwargs)
      93835   File "/home/sm_klazi/src/git/distributed/distributed/comm/utils.py", line 81, in _from_frames
      93836     return protocol.loads(
      93837   File "/home/sm_klazi/src/git/distributed/distributed/protocol/core.py", line 159, in loads
      93838     return msgpack.loads(
      93839   File "msgpack/_unpacker.pyx", line 194, in msgpack._cmsgpack.unpackb
      93840   File "/home/sm_klazi/src/git/distributed/distributed/protocol/core.py", line 139, in _decode_default
      93841     return merge_and_deserialize(
      93842   File "/home/sm_klazi/src/git/distributed/distributed/protocol/serialize.py", line 487, in merge_and_deserialize
      93843     return deserialize(header, merged_frames, deserializers=deserializers)
      93844   File "/home/sm_klazi/src/git/distributed/distributed/protocol/serialize.py", line 416, in deserialize
      93845     return loads(header, frames)
      93846   File "/home/sm_klazi/src/git/distributed/distributed/protocol/serialize.py", line 57, in dask_loads
      93847     loads = dask_deserialize.dispatch(typ)
      93848   File "/home/sm_klazi/src/git/dask/dask/utils.py", line 600, in dispatch
      93849     raise TypeError(f"No dispatch for {cls}. Only have {self._lookup} and {self._lazy}")
      93850 TypeError: No dispatch for <class 'numpy.ma.core.MaskedArray'>. Only have {<class 'bytes'>: <function _deserialize_bytes at 0x2b5a4db8daf0>, <class 'bytearray'>: <function _deserialize_bytearray at 0x2b5a4db8db80>, <class 'array.array'>: <function _deserialize_a  93850 rray at 0x2b5a4db8dca0>, <class 'memoryview'>: <function _deserialize_memoryview at 0x2b5a4db8ddc0>, <class 'dict'>: <bound method ObjectDictSerializer.deserialize of <distributed.protocol.serialize.ObjectDictSerializer object at 0x2b5a4db87a60>>} and {'scipy':   93850 <function _register_scipy at 0x2b5a4db904c0>, 'h5py': <function _register_h5py at 0x2b5a4db90550>, 'netCDF4': <function _register_netcdf4 at 0x2b5a4db905e0>, 'keras': <function _register_keras at 0x2b5a4db90670>, 'sparse': <function _register_sparse at 0x2b5a4db  93850 90700>, 'pyarrow': <function _register_arrow at 0x2b5a4db90790>, 'torchvision': <function _register_torch at 0x2b5a4db90940>, 'torch': <function _register_torch at 0x2b5a4db90940>, 'cupyx': <function _register_cupy at 0x2b5a4db90c10>, 'cupy': <function _register  93850 _cupy at 0x2b5a4db90c10>, 'numba': <function _register_numba at 0x2b5a4db909d0>, 'rmm': <function _register_rmm at 0x2b5a4db90a60>, 'dask_cudf': <function _register_cudf at 0x2b5a4db90dc0>, 'cudf': <function _register_cudf at 0x2b5a4db90dc0>, 'cuml': <function _  93850 register_cuml at 0x2b5a4db90af0>}
    
    
  5. quasiben commented on Jun 15, 2022

    @quasiben
    Member

    @jakirkham do you have any thoughts about what could be going on here?

  6. jakirkham commented on Jun 16, 2022

    @jakirkham
    Member

    Not really. The suggestions above seem reasonable

  7. jsignell commented on Jun 21, 2022

    @jsignell
    Member

    Sorry for being slow to this. I'm wondering if there would be performance implications if we just change the pop to a get.

    diff --git a/dask/utils.py b/dask/utils.py
    index 2510e904e..6f305a783 100644
    --- a/dask/utils.py
    +++ b/dask/utils.py
    @@ -591,7 +591,7 @@ class Dispatch:
                 # Is a lazy registration function present?
                 toplevel, _, _ = cls2.__module__.partition(".")
                 try:
    -                register = self._lazy.pop(toplevel)
    +                register = self._lazy[toplevel]
                 except KeyError:
                     pass
                 else:

    It seems to me like that would solve the race condition right? It just means that the same dispatch might be assigned to the lookup dict multiple times, but only in rarely based on the description in this issue.

  8. jsignell commented on Jun 21, 2022

    @jsignell
    Member

    Well that leads to a recursion error, but I'll keep thinking.

  9. zklaus commented on Jun 21, 2022

    @zklaus
    Author

    Strange. Thanks for taking care of this, @jsignell!

  10. added
    bug
    and removed
    needs triage
    on Jun 28, 2022
  11. zklaus commented on Oct 5, 2022

    @zklaus
    Author

    I added a lock to the lazy dispatch and that seemed to solve the problem for me, though it's always tough to know for sure with this kind of problem. If you think that approach is worth pursuing, I'd be interested to know if adding another dependency (in this case readerwriterlock) is acceptable or if we should find a different way to implement the lock.

    Then again, perhaps you consider a lock inappropriate or belief that the problem isn't solved by this at all.

  12. self-assigned this
    on Oct 13, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

bugcore

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions