Skip to content

Writing fields on multiple IDSs might not be thread safe #130

@Nush395

Description

@Nush395

Trying to do a write operation to multiple IDS in multiple threads e.g

setattr(core_profiles_ids.profiles_1d.ion[0], 'name', 'D')

results in

AttributeError: type object 'IDSMetadata' has no attribute '__setattr__'. Did you mean: '__delattr__'?

Using imas-core. I believe this is because get_toplevel_metadata() temporarily deletes
IDSMetadata.__setattr__

I was able to avoid the error by adding a lock to get_toplevel_metadata

import threading
from imas import ids_metadata as _ids_metadata
from imas import ids_toplevel as _ids_toplevel

def _patch_imas_metadata_lock() -> None:
  """Patches IMAS metadata construction to be thread-safe.

  The upstream ``get_toplevel_metadata()`` temporarily *deletes*
  ``IDSMetadata.__setattr__`` from the class to allow construction,
  then restores it.  This is not thread-safe.
  """
  _imas_metadata_lock = threading.Lock()
  _orig_get_toplevel_metadata = _ids_metadata.get_toplevel_metadata

  def _thread_safe_get_toplevel_metadata(structure_xml):
    with _imas_metadata_lock:
      return _orig_get_toplevel_metadata(structure_xml)

  _ids_metadata.get_toplevel_metadata = _thread_safe_get_toplevel_metadata
  # Also patch the reference that ids_toplevel already imported.
  _ids_toplevel.get_toplevel_metadata = _thread_safe_get_toplevel_metadata

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions