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?
FileHandleusesLock.withLock()to protectclosedandopenStreamCount, 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
FileHandlehas race conditions when used from multiple threads.openStreamCountcan be corruptedclose()races withsource()/sink()!closedbut state can change between check and useCan we use
AtomicBooleanandAtomicIntinstead ofLock?Or implement it on native using
pthread_mutex?