Are Blocks thread-safe and have locking mechanism to avoid race conditions?

Block’s state is stored in the API, which is not “thread-safe” since you could mutate the value in one thread while the other thread expects a different value.

JSON Block as an example

If your JSON block was a list and you were appending items from two threads to this list, we do not perform any locking.

Local memory

Threads also share local memory, so you could corrupt the block if the object is owned by multiple threads. For example, if you have a single block object at the global level and two threads working with it.

  • When the first thread saves the block, the id will be added to the block object.
  • If the second thread tries to save the same block object, issues may occur as the block is already linked to a saved id.

In theory, if you saved them both at the same time in two separate threads, you could corrupt the block locally with some sort of mixed state.

Conclusion

You should treat blocks as a secure key-value store for relatively static configuration data rather than a highly transactional data store to which you would write data from multiple processes.