assessWithin method
- Duration timeout = assessTimeout,
- WayfinderCancellation? cancel,
assess, bounded: it runs on an isolate of its own, which is killed
once it runs past timeout or cancel is cancelled. A host passes it
as its KnowledgeAssessor to KnowledgeWriter and KnowledgeAdoption:
assess: (input, cancel) => input.assessWithin(cancel: cancel),
Returns what assess returns. Throws WayfinderException with the code
assessment-timeout when the assessment ran past timeout,
cancelled (WayfinderException.cancelled) when cancel stopped it,
and assessment-failed when it stopped on an error, such as a stack
overflow in a rule, or no isolate could be started for it, as for an
input with a validator that cannot be sent to one. Unlike
ProfilePackage.parseWithin, it never falls back to the calling
isolate, since its caller asked for a bound.
The assessment isolate receives a copy of this input and reads no
bundle file but the targets path-targets-exist checks, so killing it
leaves no lock held and nothing written. Each call starts one isolate.
Implementation
Future<KnowledgeAssessment> assessWithin({
Duration timeout = assessTimeout,
WayfinderCancellation? cancel,
}) async {
throwIfCancelled(cancel);
final port = ReceivePort();
// The assessment, or the failure that stopped it.
final outcome = Completer<Object>();
void finish(Object result) {
if (!outcome.isCompleted) outcome.complete(result);
}
// Sent on [port]: the assessment, the error report of an uncaught error
// as a list, and null when the isolate exits.
port.listen((Object? message) {
switch (message) {
case final KnowledgeAssessment assessment:
finish(assessment);
case [final error, _]:
finish(
WayfinderException(
'assessment-failed',
'The Profile assessment stopped on an error: $error',
),
);
default:
finish(
const WayfinderException(
'assessment-failed',
'The Profile assessment stopped without a result.',
),
);
}
});
Timer? deadline;
final hook = _CancelHook();
try {
final Isolate isolate;
try {
isolate = await Isolate.spawn(
_assessOnIsolate,
(port.sendPort, this),
onExit: port.sendPort,
onError: port.sendPort,
debugName: 'wayfinder-assessment',
);
} on Object catch (error) {
// Spawning refuses an input it cannot send, such as a validator
// holding a port, as well as a failed start.
throw WayfinderException(
'assessment-failed',
'No isolate could be started for the Profile assessment: $error',
);
}
void stop(WayfinderException failure) {
if (outcome.isCompleted) return;
isolate.kill(priority: Isolate.immediate);
finish(failure);
}
deadline = Timer(
timeout,
() => stop(
WayfinderException(
'assessment-timeout',
'The Profile assessment did not finish within '
'${_describe(timeout)}, so it was stopped. A rule of the '
'Profile ran too long on this bundle, as a pattern that '
'backtracks without end can.',
),
),
);
// A cancellation outlives this call, so what it holds is only [hook],
// which forgets the isolate and the outcome once the call ends.
hook.action = () => stop(const WayfinderException.cancelled());
if (cancel != null) unawaited(cancel.whenCancelled.then(hook.fire));
final result = await outcome.future;
if (result is KnowledgeAssessment) return result;
throw result;
} finally {
hook.action = null;
deadline?.cancel();
port.close();
}
}