Code Monkey home page Code Monkey logo

Comments (8)

Avaq avatar Avaq commented on May 9, 2024

Terminology

Problem

After some further delving, I realized the memory leak only exists because I insist on the other Future in chain to be cleared when the main Future is cleared. This allows for the cancellation of the entire tree underlying a Future. The only time this is a problem is when we're dealing with infinite branches, (or branches with >~ 100.000 nodes because the cancel function will exceed the maximum callstack size), because the clear function will grow infinitely large.

Solutions

  1. A rigorous solution would be to ignore the other Future in chains entirely in our clear function. This would mean that only Futures on the "main" branch of a chain would be cleared, and cancellation of Futures on the "other" branch wouldn't be possible. This is bad news for recursive programs, because the recursion is always on the "other" branch, causing the program to continue running after the main resources have been disposed, probably leading to an eventual crash.
  2. Another solution I've tried is to introduce a specialized chain, which unlike the regular chain does not include the clear function of the other Future into its own clear function. This new chain, probably called recurse, could then be used to created infinite recursive processes which are still cancellable. I like this solution, but I'm not sure if it's acceptable to burden users with the possibility of memory leaks that have to be avoided caused by a feature I'm adding.
  3. A combination of 1 and 2: Ignore the other Future in the regular chain, but add a specialized chain which does clear the other Future when it is cleared. My problem with this is that this is the function you would want to use in 99% of cases.

from fluture.

Avaq avatar Avaq commented on May 9, 2024

I went for solution 2.

from fluture.

rpominov avatar rpominov commented on May 9, 2024

Just for my better understanding: what happens when user cancels a future after completion? Is underlying computation cancel callback executed in this case?

from fluture.

Avaq avatar Avaq commented on May 9, 2024

In most cases; yes. Though sometimes (specifically in the case of chain) one of the two is not called after its computation completes due to a memory optimization I did.

from fluture.

rpominov avatar rpominov commented on May 9, 2024

In most cases; yes.

Ok, this is different from fun-task. In fun-task if a computation completed it cannot be canceled (since it already completed). I understand in Fluture cancelation have a bit different meaning. Just wanted to point it out and clear for myself. Not saying which meaning is better :)

Though sometimes (specifically in the case of chain) one of the two is not called after its computation completes due to a memory optimization I did.

That's interesting though. Wouldn't you want the guarantee that cancelation always propagates to all sources (event after completion)? Also I thought that you did it in Future#recur() not in Future#chain() as pointed out in https://github.com/Avaq/Fluture/releases/tag/2.0.0

from fluture.

rpominov avatar rpominov commented on May 9, 2024

Or is it just unfortunate implementation detail? I.e. users are not supposed to cancel after completion and use this "feature".

from fluture.

Avaq avatar Avaq commented on May 9, 2024

The cancel function is expected to be idempotent and a noop after completion (like clearTimeout is). Though you have a point. I'll probably patch it so that user-land cancel functions are never called after the computation they were created in has called a continuation. This means that "In most cases; yes" will change to "no, not for user-created Futures, only internal Futures are trusted to contain safe cancel functions".

I thought that you did it in Future#recur() not in Future#chain()

Both contain different optimizations. Future#recur() only keeps the cancel function of the parent Future, whereas Future#chain() only keeps the cancel function of the child Future (purely in order to save on memory consumption).

from fluture.

rpominov avatar rpominov commented on May 9, 2024

I'll probably patch it so that user-land cancel functions are never called after the computation they were created in has called a continuation.

Cool! Seems like a logical step after you've made Future.Guard() the default behavior.

from fluture.

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.