Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Temporal kills #169

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 39 additions & 19 deletions src/Effect/Aff.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,12 @@ var Aff = function () {
var FIBER = "Fiber"; // Actual fiber reference
var THUNK = "Thunk"; // Primed effect, ready to invoke

// Error used for early cancelation on Alt branches.
// This is initialized here (rather than in the Fiber constructor) because
// otherwise, in V8, this Error object indefinitely hangs on to memory that
// otherwise would be garbage collected.
var early = new Error("[ParAff] Early exit");
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good find. Any thoughts as to why this happens? Just curious.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would guess it has something to do with stack frames and traces or whatever.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

<symbol> in Error is what is hanging onto the memory, whatever that is, and I do not know why Error is being referenced from the root. I figure it is some special debugging sauce in V8 being quirky.


function Aff(tag, _1, _2, _3) {
this.tag = tag;
this._1 = _1;
Expand Down Expand Up @@ -160,7 +166,7 @@ var Aff = function () {
delete fibers[fid];
};
}
});
})();
fibers[fid] = fiber;
count++;
},
Expand Down Expand Up @@ -320,24 +326,41 @@ var Aff = function () {

case ASYNC:
status = PENDING;
step = runAsync(util.left, step._1, function (result) {
return function () {
if (runTick !== localRunTick) {
return;
}
runTick++;
Scheduler.enqueue(function () {
// It's possible to interrupt the fiber between enqueuing and
// resuming, so we need to check that the runTick is still
// valid.
if (runTick !== localRunTick + 1) {
tmp = step._1;
step = nonCanceler;
Scheduler.enqueue(function () {
if (runTick !== localRunTick) {
return;
}
var skipRun = true;
var canceler = runAsync(util.left, tmp, function (result) {
return function () {
if (runTick !== localRunTick) {
return;
}
++runTick;
status = STEP_RESULT;
step = result;
run(runTick);
});
};
step = result;
// Do not recurse on run if we are synchronous with runAsync.
if (skipRun) {
skipRun = false;
} else {
run(runTick);
}
};
});
// Only update the canceler if the asynchronous action has not
// resolved synchronously. If it has, then the next status and
// step have already been set.
if (skipRun) {
step = canceler;
skipRun = false;
}
// If runAsync already resolved then the next step needs to be
// run.
else {
run(runTick);
}
});
return;

Expand Down Expand Up @@ -643,9 +666,6 @@ var Aff = function () {
var killId = 0;
var kills = {};

// Error used for early cancelation on Alt branches.
var early = new Error("[ParAff] Early exit");

// Error used to kill the entire tree.
var interrupt = null;

Expand Down
15 changes: 9 additions & 6 deletions src/Effect/Aff.purs
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,11 @@ instance monoidAff ∷ Monoid a ⇒ Monoid (Aff a) where
instance altAff ∷ Alt Aff where
alt a1 a2 = catchError a1 (const a2)

alwaysFailsError ∷ Error
alwaysFailsError = error "Always fails"

instance plusAff ∷ Plus Aff where
empty = throwError (error "Always fails")
empty = throwError alwaysFailsError

-- | This instance is provided for compatibility. `Aff` is always stack-safe
-- | within a given fiber. This instance will just result in unnecessary
Expand Down Expand Up @@ -306,21 +309,21 @@ type Supervised a =
, supervisor ∷ Supervisor
}

parentOutlivedError ∷ Error
parentOutlivedError = error "[Aff] Child fiber outlived parent"

-- | Creates a new supervision context for some `Aff`, guaranteeing fiber
-- | cleanup when the parent completes. Any pending fibers forked within
-- | the context will be killed and have their cancelers run.
supervise ∷ ∀ a. Aff a → Aff a
supervise aff =
generalBracket (liftEffect acquire)
{ killed: \err sup → parSequence_ [ killFiber err sup.fiber, killAll err sup ]
, failed: const (killAll killError)
, completed: const (killAll killError)
, failed: const (killAll parentOutlivedError)
, completed: const (killAll parentOutlivedError)
}
(joinFiber <<< _.fiber)
where
killError ∷ Error
killError =
error "[Aff] Child fiber outlived parent"

killAll ∷ Error → Supervised a → Aff Unit
killAll err sup = makeAff \k →
Expand Down