Skip to content

Make BinaryStorage thread-safe #65

Description

@imurashka

The package described itself as "thread-safe", but the current implementation has no thread-safety guarantees. The misleading wording has been removed from the package description for now; this issue tracks actually making the storage safe for concurrent use.

Current state

There is no synchronization anywhere in the runtime:

  • No lock / Monitor / Interlocked / ReaderWriterLock, no System.Collections.Concurrent, no volatile.
  • State is held in plain Dictionary<string, Record> (src/Runtime/BinaryStorage.cs), which is not safe for concurrent reads/writes.
  • Get / Set / Remove, the reactive collections, the dirty flag and the change-scope counter are all mutated without guarding.

What does exist (and is unrelated to thread-safety) is atomic disk writes: data is written to a temp file and then swapped in via delete + move (src/Runtime/BinaryStorageIO.cs), which protects the file from corruption on an interrupted save.

Goal

Make concurrent access to a single BinaryStorage instance safe, or explicitly define and document the supported threading model.

Things to decide / cover

  • Locking strategy (single lock vs reader/writer) and its scope across Get/Set/Remove/Save/MultipleChangeScope.
  • Thread-safety of the reactive collections returned by GetListOf / GetSetOf / GetDictionaryOf (they mutate the parent storage on change).
  • Behavior of Save / auto-save when changes happen concurrently.
  • Whether change events (OnKeyAdded/OnKeyChanged/OnKeyRemoved) are raised on the caller thread or marshalled.
  • Tests covering concurrent access.
  • Restore the "thread-safe" wording in the package description once guarantees actually hold.

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions