[ Cocoon ] Wait for task results to be received by the task runner before shutting down the task process (#156002)

Prior to this fix, `_TaskRunner.run` would immediately cleanup the
keep-alive port once the task completed, which would result in the
isolate shutting down as soon as the task result was returned from
`ext.cocoonRunTask` callback in the form of a
`ServiceExtensionResponse`. Since the service extension response is
processed by the service isolate, it was possible for the VM to start
shutting down before the service isolate could send the task result data
back to the task runner.

This change introduces a new service extension,
`ext.cocoonTaskResultReceived`, that the task runner invokes after it
receives the task result from `ext.cocoonRunTask`, notifying the task
process that it can close the keep-alive port and shutdown.

Fixes https://github.com/flutter/flutter/issues/155475
This commit is contained in:
Ben Konyi
2024-10-02 17:01:24 -04:00
committed by GitHub
parent d877d2875e
commit 2c84e63ba7
3 changed files with 79 additions and 36 deletions

View File

@@ -66,6 +66,12 @@ Future<TaskResult> task(TaskFunction task, { ProcessManager? processManager }) a
class _TaskRunner {
_TaskRunner(this.task, this.processManager) {
final String successResponse = json.encode(
const <String, String>{
'result': 'success',
},
);
registerExtension('ext.cocoonRunTask',
(String method, Map<String, String> parameters) async {
final Duration? taskTimeout = parameters.containsKey('timeoutInMinutes')
@@ -82,11 +88,25 @@ class _TaskRunner {
localEngine: localEngine,
localEngineHost: localEngineHost,
);
const Duration taskResultReceivedTimeout = Duration(seconds: 30);
_taskResultReceivedTimeout = Timer(
taskResultReceivedTimeout,
() {
logger.severe('Task runner did not acknowledge task results in $taskResultReceivedTimeout.');
_closeKeepAlivePort();
exitCode = 1;
}
);
return ServiceExtensionResponse.result(json.encode(result.toJson()));
});
registerExtension('ext.cocoonRunnerReady',
(String method, Map<String, String> parameters) async {
return ServiceExtensionResponse.result('"ready"');
return ServiceExtensionResponse.result(successResponse);
});
registerExtension('ext.cocoonTaskResultReceived',
(String method, Map<String, String> parameters) async {
_closeKeepAlivePort();
return ServiceExtensionResponse.result(successResponse);
});
}
@@ -104,6 +124,7 @@ class _TaskRunner {
// TODO(ianh): workaround for https://github.com/dart-lang/sdk/issues/23797
RawReceivePort? _keepAlivePort;
Timer? _startTaskTimeout;
Timer? _taskResultReceivedTimeout;
bool _taskStarted = false;
final Completer<TaskResult> _completer = Completer<TaskResult>();
@@ -210,7 +231,6 @@ class _TaskRunner {
} finally {
await checkForRebootRequired();
await forceQuitRunningProcesses();
_closeKeepAlivePort();
}
}
@@ -269,6 +289,7 @@ class _TaskRunner {
/// Disables the keepalive port, allowing the VM to exit.
void _closeKeepAlivePort() {
_startTaskTimeout?.cancel();
_taskResultReceivedTimeout?.cancel();
_keepAlivePort?.close();
}