You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I'm seeing unexpected behavior from channel.produce where it seems to be exiting / closing immediately. I can only produce this on my multiplatform project inside the JS side, while similar GlobalScope.produce works fine on the jvm. Simply swapping to manual use of a Channel seems to solve the problem.
Below is the context of the usage, the problem, the output, and a workaround. Also here is the project for a full repro: repro2.zip
Usage
classDeviceRepository internal constructor(
privatevaldataSource:DataSource
) : Repository() {
suspendfunfindDevices(): List<Device> = withRepositoryContext {
println("FindDevices Map")
dataSource.getConnectedDevices().map { device ->
async {
val data = dataSource.getDeviceInfo(device.id)
println("getDeviceInfo Returned")
Device(
id = device.id,
connectionType = device.type,
data = data
)
}
}.awaitAll()
}
}
classCommandLineDataSource : DataSource {
companionobject {
privatevalCONNECTED_DEVICES_REGEX=Regex("(\\S+) \\((\\S+)\\)")
}
overridesuspendfungetConnectedDevices(): List<ConnectedDevice> {
val output =CommandRunner.run("idevice_id")
.toList()
println("DataSource: $output")
return output.map {
val result =CONNECTED_DEVICES_REGEX.matchEntire(it)!!ConnectedDevice(
id = result.groupValues[1],
type = result.groupValues[2]
)
}
}
// ...
}
FindDevices Map
On Data: 59cbbfb669c0f2c72692ed25d9e31b271c2ef9c4 (USB)
On End
Invoke on Close
DataSource: [59cbbfb669c0f2c72692ed25d9e31b271c2ef9c4 (USB)
]
The text was updated successfully, but these errors were encountered:
The channel is closed when the coroutine completes.
In your Java code, we have a different story entirely. The reading of the data from the stream happens inside the produce coroutine, so the channel does not close until the data is wholly read.
Kotlin 1.4.0
Kotlinx.coroutines 1.3.9
I'm seeing unexpected behavior from
channel.produce
where it seems to be exiting / closing immediately. I can only produce this on my multiplatform project inside the JS side, while similarGlobalScope.produce
works fine on the jvm. Simply swapping to manual use of aChannel
seems to solve the problem.Below is the context of the usage, the problem, the output, and a workaround. Also here is the project for a full repro:
repro2.zip
Usage
Problematic Code
Output
Notice that invoke on close is called immediately
Equivalent code that fixes it:
Output:
The text was updated successfully, but these errors were encountered: