[go: nahoru, domu]

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

[tool] when writing to openssl as a part of macOS/iOS code-signing, flush the stdin stream before closing it #150120

Prev Previous commit
Next Next commit
handle any error from flush
  • Loading branch information
andrewkolos committed Jun 28, 2024
commit 386cf4aa41ff1368620a1bf3e182bc9f3b7703ca
19 changes: 13 additions & 6 deletions packages/flutter_tools/lib/src/base/process.dart
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,17 @@ abstract class ProcessUtils {
}) async {
final Completer<void> completer = Completer<void>();

// Future.onError specifically requires a return type of FutureOr<Null>, so
// we can't use void here.
// ignore: prefer_void_to_null
Null handleError(Object error, StackTrace stackTrace) {
try {
onError(error, stackTrace);
} on Exception catch (e) {
completer.completeError(e);
}
}

void writeFlushAndComplete() {
if (isLine) {
stdin.writeln(content);
Expand All @@ -294,17 +305,13 @@ abstract class ProcessUtils {
}
stdin.flush().then((_) {
completer.complete();
});
}).onError(handleError);
}

runZonedGuarded(
writeFlushAndComplete,
(Object error, StackTrace stackTrace) {
try {
onError(error, stackTrace);
} on Exception catch (e) {
completer.completeError(e);
}
handleError(error, stackTrace);

// We may have already completed with an error in the above catch block.
if (!completer.isCompleted) {
Expand Down
53 changes: 53 additions & 0 deletions packages/flutter_tools/test/general.shard/base/process_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

import 'dart:async';

import 'package:flutter_tools/src/base/io.dart';
import 'package:flutter_tools/src/base/logger.dart';
import 'package:flutter_tools/src/base/platform.dart';
Expand Down Expand Up @@ -444,4 +446,55 @@ void main() {
);
});
});

group('writeToStdinGuarded', () {
testWithoutContext('handles any error thrown by stdin.flush', () async {
final _ThrowsOnFlushIOSink stdin = _ThrowsOnFlushIOSink();
final Completer<bool> fooCommandCompleter = Completer<bool>();
final Future<bool> >

void fooOnRun(List<String> command) {
ProcessUtils.writeToStdinGuarded(
stdin: stdin,
content: 'message to stdin',
onError: (Object error, StackTrace stackTrace) {
fooCommandCompleter.complete(true);
},
).then((_) => fooCommandCompleter.complete(false))
.onError((_, __) {
// Fail the test.
throw Exception("writeToStdinGuarded shouldn't have thrown.");
});
}

final FakeProcessManager processManager = FakeProcessManager.list(
<FakeCommand>[
FakeCommand(
command: const <String>['foo'],
stdin: stdin,
onRun: fooOnRun,
completer: fooCommandCompleter,
),
],
);

await processManager.run(<String>['foo']);

expect(
await onErrorCalled,
isTrue,
reason: 'onError argument to writeToStdinGuarded should have been called',
);
});
});
}

class _ThrowsOnFlushIOSink extends MemoryIOSink {
@override
Future<Object?> flush() async {
throw const SocketException(
'Write failed',
osError: OSError('Broken pipe', 32),
);
}
}