adb: win32: fix Ctrl-C of adb server nodaemon
On Windows, when running adb server nodaemon and pressing Ctrl-C,
adb_server_cleanup (an atexit handler) would call kick_all_transports()
which would eventually fail a CHECK because the current thread was not
equal to the main thread. This is because Ctrl-C is implemented in
Windows by the OS creating a new thread in the process and calling the
Ctrl-C handler from there.
The CHECK fail would print out the CHECK expression and call abort()
which would record a crash in the Windows Event Log, plus would
potentially upload a crashdump to Microsoft's Watson service.
This might be a regression from d51c6df1ef98f2b57916ddefc80afda5f1eecb59.
The fix is to share more code between platforms, removing the call to
Win32 SetConsoleCtrlHandler() and just use the C Runtime's signal()
implementation which is built upon SetConsoleCtrlHandler(). The signal
handler still ends up being called from another thread, but the handler
is thread-safe enough so this seems to work.
Test: On Win10 and Vista, run adb server nodaemon and then try Ctrl-C,
Ctrl-Break and close console window.
Change-Id: I6603970616098d2b3ce68f2a3d4e5515ec859811
Signed-off-by: Spencer Low <CompareAndSwap@gmail.com>
diff --git a/adb/client/main.cpp b/adb/client/main.cpp
index 095ad98..a7e454d 100644
--- a/adb/client/main.cpp
+++ b/adb/client/main.cpp
@@ -56,15 +56,6 @@
LOG(INFO) << adb_version();
}
-#if defined(_WIN32)
-static BOOL WINAPI ctrlc_handler(DWORD type) {
- // TODO: Consider trying to kill a starting up adb server (if we're in
- // launch_server) by calling GenerateConsoleCtrlEvent().
- exit(STATUS_CONTROL_C_EXIT);
- return TRUE;
-}
-#endif
-
void adb_server_cleanup() {
// Upon exit, we want to clean up in the following order:
// 1. close_smartsockets, so that we don't get any new clients
@@ -97,12 +88,16 @@
}
}
- SetConsoleCtrlHandler(ctrlc_handler, TRUE);
-#else
+ // TODO: On Ctrl-C, consider trying to kill a starting up adb server (if we're in
+ // launch_server) by calling GenerateConsoleCtrlEvent().
+
+ // On Windows, SIGBREAK is when Ctrl-Break is pressed or the console window is closed. It should
+ // act like Ctrl-C.
+ signal(SIGBREAK, [](int) { raise(SIGINT); });
+#endif
signal(SIGINT, [](int) {
fdevent_run_on_main_thread([]() { exit(0); });
});
-#endif
char* leak = getenv("ADB_LEAK");
if (leak && strcmp(leak, "1") == 0) {