Skip to content

FileHandle is not thread-safe on native due to no-op Lock implementation. #1778

Description

@MohammedKHC

FileHandle uses Lock.withLock() to protect closed and openStreamCount, but on native platforms the Lock implementation does nothing:

https://github.com/square/okio/blob/79aa26755c77df8c4d0233926c7308fd353ad697/okio/src/nonJvmMain/kotlin/okio/NonJvmPlatform.kt#L42-L48

This means FileHandle has race conditions when used from multiple threads.

  • openStreamCount can be corrupted
  • Resource leaks possible when close() races with source()/sink()
  • Methods check !closed but state can change between check and use

Can we use AtomicBoolean and AtomicInt instead of Lock?
Or implement it on native using pthread_mutex?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions