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?
FileHandleusesLock.withLock()to protectclosedandopenStreamCount, but on native platforms the Lock implementation does nothing:okio/okio/src/nonJvmMain/kotlin/okio/NonJvmPlatform.kt
Lines 42 to 48 in 79aa267
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?