Commit df45d1ab authored by Bruce Dawson's avatar Bruce Dawson Committed by Chromium LUCI CQ

Use TerminateProcess in headless browsers

Exiting a process cleanly is difficult and expensive. Avoiding race
conditions becomes almost impossible with complex software and a clean
exit can be quite slow. Waiting on all threads, paging in code and data,
and race conditions make it not worthwhile.

We have been gradually moving towards using TerminateProcess for more
process types. Although the majority of the shutdown processes tracked
by this bug are in utility processes this change uses TerminateProcess
for all process types. We should not need to let destructors run so
it should be safe to never use exit() (or to make it opt-in).

This change also adds TerminateProcess to windows_types.h to avoid
having to add a Windows.h include to another source file.

Bug: 896565
Change-Id: I720c692b3315544c09fe9ae438fb65f6f77fc657
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2598043Reviewed-by: default avatarAndrey Kosyakov <caseq@chromium.org>
Commit-Queue: Bruce Dawson <brucedawson@chromium.org>
Cr-Commit-Position: refs/heads/master@{#838732}
parent 1165b4c7
......@@ -236,6 +236,9 @@ WINBASEAPI _Check_return_ _Post_equals_last_error_ DWORD WINAPI
WINBASEAPI VOID WINAPI SetLastError(_In_ DWORD dwErrCode);
WINBASEAPI BOOL WINAPI TerminateProcess(_In_ HANDLE hProcess,
_In_ UINT uExitCode);
#ifdef __cplusplus
}
#endif
......
......@@ -51,6 +51,7 @@
#include "ui/gfx/geometry/size.h"
#if defined(OS_WIN)
#include <base/win/windows_types.h>
#include "components/crash/core/app/crash_switches.h"
#include "components/crash/core/app/run_as_crashpad_handler_win.h"
#include "sandbox/win/src/sandbox_types.h"
......@@ -836,8 +837,15 @@ void RunChildProcessIfNeeded(int argc, const char** argv) {
builder.SetUserAgent(ua);
}
exit(RunContentMain(builder.Build(),
base::OnceCallback<void(HeadlessBrowser*)>()));
int rc = RunContentMain(builder.Build(),
base::OnceCallback<void(HeadlessBrowser*)>());
#if defined(OS_WIN)
// Use TerminateProcess instead of exit to avoid shutdown crashes and
// slowdowns on shutdown.
::TerminateProcess(::GetCurrentProcess(), rc);
#else // defined(OS_WIN)
exit(rc);
#endif // defined(OS_WIN)
}
int HeadlessBrowserMain(
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment