Coder Social home page Coder Social logo

Comments (7)

elizarov avatar elizarov commented on September 23, 2024 1

Fixed in develop branch

from kotlinx.coroutines.

elizarov avatar elizarov commented on September 23, 2024

Hm... ConflatedBroadcastChannel is well, conflated. If you have two back-to-back invocations to put with different keys, then the second offer will replace the previous one (it will be conflated) and the waiter will never wake up.

There is no await/notify-like not event-like mechanism in coroutines out-of-the-bix yet. Mutex should support conditions but it does not do so now, so you'll leave this ticket open.

If wonder what is the larger context of the problem that you are trying to solve with AwaitableMap. What are you planning to use it for?

from kotlinx.coroutines.

chrisly42 avatar chrisly42 commented on September 23, 2024

Thanks for your quick reply. As far as I understood, there is no problem for me to use the conflated version as I am not using the value transmitted for anything. The offer() should just wake up very listener that currently is "subscribed" by having a channel open. I'm not sure why the waiter would not wake up if any put is broadcasted to all the receivers.

Moreover, I still think there is a bug in the implementation of ConflatedBroadcastChannel somewhere because it works well in most cases but in higher loads, the put-replacement routine gets stuck in an infinite loop in offer which is considered to be a non-blocking call. In no case, offer() should do that.

My use case is the following. I have an object that several child objects that are to be loaded asynchronously via HTTP (slow) but I want to update new items in the UI as soon as they are completely fetched by the coroutine. There is another coroutine that already knows that the will be there as some point in the future, waiting for its arrival (this one is calling the await() method). It will then process the item data before displaying it on the UI.

I wrote a workaround that uses a map of channels, that works for my use-case right now, but is not generic.

I'm fairly new to Kotlin and Android programming, but I think Kotlin and coroutines are just wonderful :) Maybe there are already implementations that I'm not aware of (I haven't checked out RxJava yet)...

from kotlinx.coroutines.

elizarov avatar elizarov commented on September 23, 2024

The stuck invocation to offer is definitely a bug. Thanks for the report!

I've just cursory glanced over your code and how I see how you are actually using it. I was misled by the fact that you send a key to the channel, but you don't actually use this key in await. Indeed, you use ConflatedBroadcastChannel as a simple event mechanism to notify waiter that something had changed. It should work this way, though a more efficient event-like mechanism is definitely needed (but is missing now).

from kotlinx.coroutines.

elizarov avatar elizarov commented on September 23, 2024

Good news. I can reproduce your problem (offer hangs) with a stress test.

from kotlinx.coroutines.

chrisly42 avatar chrisly42 commented on September 23, 2024

Thanks a lot for the quick fix. Will stick to the alternate solution until next release though and then test it again. Your changes don't seem trivial, though :) Feel free to close the issue, if you think the stress test covers the problem.

from kotlinx.coroutines.

elizarov avatar elizarov commented on September 23, 2024

It turned out to be a whole complex of problems that happen when you concurrently close/send/close channels. It was also independently discovered by @cy6erGn0m

from kotlinx.coroutines.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.