Skip to content

Bug report: FHIRModels is not concurrency safe, leading to crashes at runtime #36

Closed

Description

@lukaskollmer

Note: I'm not asking that the library be upgraded to Swift 6, or that all the types be made Sendable. I know why they aren't.


The specific issue here is that in FHIRDateComponents, there is a static var calendar = Calendar(identifier: .gregorian), which seems to be used only by some of the static methods on the type.

we observe regular crashes in our codebase, when attempting to have multiple threads perform (independent!) operations that use the FHIRModels types (some of which in turn internally use the FHIRDateComponents, which then cause a crash, presumably due to the concurrent accesses to the global mutable variable)

since creating Calendars isn't overly expensive, IMO, these static functions should simply obtain a temporary Calendar object and use that, instead of having a global variable.
(unless there was a specific reason for not implementing it this way in the first place, though even then there probably exists a better solution than the current approach.)

Activity

  1. PSchmiedmayer commented on Apr 26, 2025

    @PSchmiedmayer

    Thank you for reporting this here @lukaskollmer 🚀

  2. p2-apple commented on Apr 26, 2025

    @p2-apple
    Member

    Thanks for the report. We need to audit the library for Swift Concurrency; static vars are indeed one of the classic causes of issues in this new world.

  3. lukaskollmer commented on May 14, 2025

    @lukaskollmer
    Author

    @p2-apple do you have a (rough) estimate when this will be fixed? we keep running into this issue, and it keeps causing crashes in our codebase.

    i feel like it should be feasible to have a small release that addresses only this bug, rather than keeping it around until the entire library is audited for concurrency.
    (also, and i might be wrong on this, but given the fact that (I) none of the FHIR types can be made Sendable anyway and (II) apart from this calendar variable there is no other global mutable state in the library, this probably is the only major concurrency issue anyway)

  4. p2-apple commented on May 14, 2025

    @p2-apple
    Member

    Let me try to get to this this week

  5. p2-apple commented on May 14, 2025

    @p2-apple
    Member

    Thanks, I can reproduce the crash in the testAllParsing() unit tests. Adding the FHIRDateComponents.dateComponents(...) call into 100 queue.async block. Not however when I use a TaskGroup. Fix incoming.

  6. added a commit that references this issue on May 22, 2025
  7. added a commit that references this issue on Nov 16, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions