-
-
Notifications
You must be signed in to change notification settings - Fork 86
fix(macos): fail cleanly without GUI services #4238
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
Merged
jensenpat
merged 2 commits into
aethersdr:main
from
rfoust:codex/macos-startup-gui-service-error
Jul 22, 2026
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,55 @@ | ||
| #include "MacStartupAbortGuard.h" | ||
|
|
||
| #include <unistd.h> | ||
|
|
||
| namespace { | ||
|
|
||
| constexpr char kStartupAbortMessage[] = | ||
| "AetherSDR startup error: macOS aborted GUI initialization. " | ||
| "GUI services may be unavailable (for example, in a restricted sandbox). " | ||
| "Launch AetherSDR from Finder or a normal Terminal session.\n"; | ||
|
|
||
| void handleStartupAbort(int) | ||
| { | ||
| // QApplication construction can abort from inside AppKit/HIServices before | ||
| // Qt logging exists. Only async-signal-safe operations are valid here. | ||
| const ssize_t ignored = write(STDERR_FILENO, | ||
| kStartupAbortMessage, | ||
| sizeof(kStartupAbortMessage) - 1); | ||
| (void)ignored; | ||
| _exit(AetherSDR::MacStartupAbortGuard::kFailureExitCode); | ||
| } | ||
|
|
||
| } // namespace | ||
|
|
||
| namespace AetherSDR { | ||
|
|
||
| MacStartupAbortGuard::MacStartupAbortGuard() | ||
| { | ||
| struct sigaction action {}; | ||
| action.sa_handler = handleStartupAbort; | ||
| sigemptyset(&action.sa_mask); | ||
|
|
||
| m_armed = sigaction(SIGABRT, &action, &m_previousAction) == 0; | ||
| } | ||
|
|
||
| MacStartupAbortGuard::~MacStartupAbortGuard() | ||
| { | ||
| (void)disarm(); | ||
| } | ||
|
|
||
| bool MacStartupAbortGuard::disarm() | ||
| { | ||
| if (!m_armed) { | ||
| return true; | ||
| } | ||
|
|
||
| if (sigaction(SIGABRT, &m_previousAction, nullptr) != 0) { | ||
| return false; | ||
| } | ||
|
|
||
| m_armed = false; | ||
| return true; | ||
| } | ||
|
|
||
| } // namespace AetherSDR |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| #pragma once | ||
|
|
||
| #include <signal.h> | ||
|
|
||
| namespace AetherSDR { | ||
|
|
||
| // Converts a macOS abort during QApplication construction into a normal | ||
| // startup failure. The guard must be disarmed as soon as construction succeeds | ||
| // so aborts from the running application retain their normal disposition. | ||
| class MacStartupAbortGuard final | ||
| { | ||
| public: | ||
| static constexpr int kFailureExitCode = 1; | ||
|
|
||
| MacStartupAbortGuard(); | ||
| ~MacStartupAbortGuard(); | ||
|
|
||
| MacStartupAbortGuard(const MacStartupAbortGuard&) = delete; | ||
| MacStartupAbortGuard& operator=(const MacStartupAbortGuard&) = delete; | ||
|
|
||
| bool isArmed() const { return m_armed; } | ||
| bool disarm(); | ||
|
|
||
| private: | ||
| struct sigaction m_previousAction {}; | ||
| bool m_armed{false}; | ||
| }; | ||
|
|
||
| } // namespace AetherSDR |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,116 @@ | ||
| #include "MacStartupAbortGuard.h" | ||
|
|
||
| #include <csignal> | ||
| #include <cstdlib> | ||
| #include <cstdio> | ||
| #include <string> | ||
|
|
||
| #include <sys/wait.h> | ||
| #include <unistd.h> | ||
|
|
||
| namespace { | ||
|
|
||
| volatile sig_atomic_t g_previousHandlerCalled = 0; | ||
|
|
||
| void previousAbortHandler(int) | ||
| { | ||
| g_previousHandlerCalled = 1; | ||
| } | ||
|
|
||
| int fail(const char* message) | ||
| { | ||
| std::fprintf(stderr, "FAIL: %s\n", message); | ||
| return 1; | ||
| } | ||
|
|
||
| } // namespace | ||
|
|
||
| int main() | ||
| { | ||
| int pipeFds[2] {}; | ||
| if (pipe(pipeFds) != 0) { | ||
| return fail("could not create stderr capture pipe"); | ||
| } | ||
|
|
||
| const pid_t child = fork(); | ||
| if (child < 0) { | ||
| close(pipeFds[0]); | ||
| close(pipeFds[1]); | ||
| return fail("could not fork guarded child"); | ||
| } | ||
|
|
||
| if (child == 0) { | ||
| close(pipeFds[0]); | ||
| if (dup2(pipeFds[1], STDERR_FILENO) < 0) { | ||
| _exit(90); | ||
| } | ||
| close(pipeFds[1]); | ||
|
|
||
| AetherSDR::MacStartupAbortGuard guard; | ||
| if (!guard.isArmed()) { | ||
| _exit(91); | ||
| } | ||
| std::abort(); | ||
| _exit(92); | ||
| } | ||
|
|
||
| close(pipeFds[1]); | ||
| std::string errorText; | ||
| char buffer[512]; | ||
| ssize_t bytesRead = 0; | ||
| while ((bytesRead = read(pipeFds[0], buffer, sizeof(buffer))) > 0) { | ||
| errorText.append(buffer, static_cast<std::size_t>(bytesRead)); | ||
| } | ||
| close(pipeFds[0]); | ||
|
|
||
| int childStatus = 0; | ||
| if (waitpid(child, &childStatus, 0) != child) { | ||
| return fail("could not wait for guarded child"); | ||
| } | ||
| if (!WIFEXITED(childStatus) | ||
| || WEXITSTATUS(childStatus) != AetherSDR::MacStartupAbortGuard::kFailureExitCode) { | ||
| return fail("guarded SIGABRT did not become exit code 1"); | ||
| } | ||
| if (errorText.find("AetherSDR startup error: macOS aborted GUI initialization") | ||
| == std::string::npos) { | ||
| return fail("guarded SIGABRT did not emit the startup error"); | ||
| } | ||
|
|
||
| struct sigaction originalAction {}; | ||
| if (sigaction(SIGABRT, nullptr, &originalAction) != 0) { | ||
| return fail("could not read the original SIGABRT action"); | ||
| } | ||
|
|
||
| struct sigaction previousAction {}; | ||
| previousAction.sa_handler = previousAbortHandler; | ||
| sigemptyset(&previousAction.sa_mask); | ||
| if (sigaction(SIGABRT, &previousAction, nullptr) != 0) { | ||
| return fail("could not install the previous SIGABRT action"); | ||
| } | ||
|
|
||
| int result = 0; | ||
| { | ||
| AetherSDR::MacStartupAbortGuard guard; | ||
| if (!guard.isArmed()) { | ||
| result = fail("startup guard did not arm"); | ||
| } else if (!guard.disarm()) { | ||
| result = fail("startup guard did not disarm"); | ||
| } | ||
| } | ||
|
|
||
| if (result == 0) { | ||
| raise(SIGABRT); | ||
| if (g_previousHandlerCalled != 1) { | ||
| result = fail("disarm did not restore the previous SIGABRT action"); | ||
| } | ||
| } | ||
|
|
||
| if (sigaction(SIGABRT, &originalAction, nullptr) != 0 && result == 0) { | ||
| result = fail("could not restore the original SIGABRT action"); | ||
| } | ||
|
|
||
| if (result == 0) { | ||
| std::puts("macOS startup abort guard: PASS"); | ||
| } | ||
| return result; | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.