0

iossim: Invoke xcrun simctl launch with --console-pty, not --console

When running content_shell and hitting certain (D)CHECKs with a long
stack trace, the entire process was blocking and freezing indefinitely
on the write() call to stderr when the length of the string was larger
than 8192 bytes (half of vm.pagesize on a 2023 Mac Studio).

It is hard to know what exactly is happening in `xcrun simctl` given
its (extremely) concise public documentation, but any write to stderr (I
have not checked stdout) larger than 8kb would cause the same hang on
the iOS and tvOS simulators.

Using `--console-pty` instead of `--console` fixes the issue, possibly
due to some different kind of buffering inside xcrun.

blinkpy's IOSSimulatorServerProcess had to be adjusted accordingly, as
with the change stderr was being lost and all web test runs were timing
out.

The solution is to make the code more similar to FuchsiaServerProcess's,
which is what it was originally based upon anyway: since iOS uses the
same trick as Fuchsia and redirects stdin and stdout to a Unix domain
socket, we can merge stderr and stdout in the pipe() call and use that
Popen object's stdout as its stderr, as the real stdout was actually
sent to the socket.

Bug: 391990604, 40254930, 40259174
Change-Id: Ifb9f6f6d99df928ed19913c66da7bfd9d87b7650
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/6339850
Reviewed-by: Jonathan Lee <jonathanjlee@google.com>
Reviewed-by: Rohit Rao <rohitrao@chromium.org>
Commit-Queue: Raphael Kubo da Costa <kubo@igalia.com>
Cr-Commit-Position: refs/heads/main@{#1430864}
This commit is contained in:
Raphael Kubo da Costa
2025-03-11 08:13:10 -07:00
committed by Chromium LUCI CQ
parent e639150259
commit d623c44351
2 changed files with 27 additions and 8 deletions
testing/iossim
third_party/blink/tools/blinkpy/web_tests/port

@ -53,6 +53,14 @@ void PrintUsage() {
const int kExitSuccess = EXIT_SUCCESS;
const int kExitInvalidArguments = 2;
// As of XCode 16.2, passing --console causes `xcode simctl launch` to block
// indefinitely if there is stderr output longer than 8192 bytes (e.g. a long
// stack trace if a (D)CHECK is hit).
// Apple's documentation is very vague about the differences between --console
// and --console-pty, but the latter seems to work fine including in the case
// above.
constexpr NSString* kSimCtlLaunchConsoleArg = @"--console-pty";
void LogError(NSString* format, ...) {
va_list list;
va_start(list, format);
@ -380,7 +388,7 @@ int RunWebTest(NSString* app_path,
NSMutableArray* arguments = [NSMutableArray array];
[arguments addObject:@"simctl"];
[arguments addObject:@"launch"];
[arguments addObject:@"--console"];
[arguments addObject:kSimCtlLaunchConsoleArg];
[arguments addObject:@"--terminate-running-process"];
[arguments addObject:udid];
[arguments addObject:GetBundleIdentifierFromPath(app_path)];
@ -436,7 +444,8 @@ int SimpleRunApplication(NSString* app_path,
RunSimCtl(@[ @"install", udid, app_path ], verbose);
NSArray* command = [@[
@"launch", @"--console", @"--terminate-running-process", udid, bundle_id
@"launch", kSimCtlLaunchConsoleArg, @"--terminate-running-process", udid,
bundle_id
] arrayByAddingObjectsFromArray:cmd_args];
return RunSimCtl(command, verbose);
}

@ -53,7 +53,7 @@ class IOSSimulatorServerProcess(ServerProcess):
proc = self._host.executive.popen(self._cmd,
stdin=self._host.executive.PIPE,
stdout=self._host.executive.PIPE,
stderr=self._host.executive.PIPE,
stderr=self._host.executive.STDOUT,
env=self._env)
# Wait for incoming connection from the iOS content_shell.
@ -70,13 +70,17 @@ class IOSSimulatorServerProcess(ServerProcess):
# launching the content shell occasionally fails. To resolve the
# issue, consider repeatedly attempting to launch the content shell
# until it successfully launches.
proc = self._host.executive.popen(self._cmd,
stdin=self._host.executive.PIPE,
stdout=self._host.executive.PIPE,
stderr=self._host.executive.PIPE,
env=self._env)
proc = self._host.executive.popen(
self._cmd,
stdin=self._host.executive.PIPE,
stdout=self._host.executive.PIPE,
stderr=self._host.executive.STDOUT,
env=self._env)
read_fds, _, _ = select.select([fd], [], [], CONN_WAITING_TIMEOUT)
# Python's interfaces for sockets and pipes are different. To masquerade
# the socket as a pipe dup() the file descriptor and pass it to
# os.fdopen().
stdio_socket, _ = listen_socket.accept()
fd = stdio_socket.fileno() # pylint: disable=no-member
stdin_pipe = os.fdopen(os.dup(fd), 'wb', 0)
@ -84,6 +88,12 @@ class IOSSimulatorServerProcess(ServerProcess):
stdio_socket.close()
proc.stdin = stdin_pipe
# stdout from `proc` is the merged stdout/stderr produced by the
# popen() invocation above, which contains only stderr since we run
# stdout through the socket above.
merged_stdout_stderr = proc.stdout
proc.stdout = stdout_pipe
proc.stderr = merged_stdout_stderr
self._set_proc(proc)