Skip to content

Clarification on Master.execute(threadsafe=False) - when is it safe to bypass the global RLock? #195

@tiaanvow

Description

@tiaanvow

Hi, thanks for maintaining modbus_tk.

I'm trying to understand the intended use of the threadsafe argument on Master.execute() and the process-global RLock in modbus_tk/utils.py.

My setup:

  • Multiple Master instances (TCP and RTU), each owning a separate physical link.
  • Each Master is only ever called from a single dedicated IO thread — no Master is shared between threads.
  • With ~6–7 concurrent Master instances, threads serialise behind the global RLock in execute(), causing dropouts on unrelated devices.

Questions:

  1. Is the RLock in utils.threadsafe_function intended to protect a single Master/socket from concurrent access, or is there shared global state across Master instances that it's also guarding?
  2. If each Master is confined to one thread (i.e. the socket/serial handle is never touched concurrently), is it safe to pass threadsafe=False to execute()? Are there any known failure modes — e.g. shared buffers, transaction ID generation, or logging — that could still cause frame interleaving or corruption?
  3. Has there been any consideration of making the lock per-Master rather than process-global? Happy to contribute a PR if that direction is welcome.

For context: we're hitting this in a real-time industrial control loop where lock contention on one link causes timing failures on others, so getting this right (rather than just patching it out) matters to us.

Thanks!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions