Skip to content

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

@MohammedKHC

Description

@MohammedKHC

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

actual inline fun <T> Lock.withLock(action: () -> T): T {
contract {
callsInPlace(action, InvocationKind.EXACTLY_ONCE)
}
return action()
}

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