Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,15 @@ Release notes on GitHub are taken from the matching section here.

## [Unreleased]

### Fixed

- Crash reports are complete more often. When two threads crash at once, the second waits for the first
report instead of cutting it off, and a stack overflow on one of Roblox's threads now leaves a report.
- A closed pipe or socket, such as the session-log writer exiting, no longer closes Nightcap with no
crash report; the write fails and Nightcap carries on.
- Nightcap no longer quits when it can't start a helper thread, such as under a Flatpak or systemd
thread limit. It logs the failure and carries on.

## [0.5.7] - 2026-12-02

### Added
Expand Down
8 changes: 8 additions & 0 deletions include/compat/crash_reporter.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,14 @@ bool InstallCrashReporter();

bool CrashReporterOwns(int signal_number);

// Makes writes to a closed pipe or socket fail with EPIPE instead of killing
// the process. Unlike SIG_IGN, exec'd programs get the default action back.
bool IgnoreBrokenPipes();

// Without its own signal stack, a thread that overflows its stack dies with no
// crash report. The 64 KiB stack is freed when the thread exits.
void InstallThreadCrashStack();

int SetGuestCrashAction(int signal_number, const struct sigaction* action,
struct sigaction* old_action);

Expand Down
6 changes: 6 additions & 0 deletions include/runtime/owned_pthread.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include <pthread.h>

#include <cstddef>
#include <functional>

namespace mocktail {
namespace runtime {
Expand Down Expand Up @@ -62,6 +63,11 @@ class OwnedPthread final {

const char* OwnedPthreadWaitStatusName(OwnedPthreadWaitStatus status);

// Runs `work` on a new detached thread. Returns false when the thread cannot
// be created; std::thread aborts instead, since this builds without
// exceptions.
bool StartDetachedThread(std::function<void()> work);

} // namespace runtime
} // namespace mocktail

Expand Down
2 changes: 2 additions & 0 deletions src/compat/bionic_pthread_create_runtime.cc
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include "compat/bionic_pthread_create_runtime.h"

#include "compat/bionic_abi_exports.h"
#include "compat/crash_reporter.h"

#include <errno.h>
#include <limits.h>
Expand Down Expand Up @@ -176,6 +177,7 @@ void* RunGuestThread(void* raw_context) noexcept {
const ThreadStartContext values = *context;
delete context;

InstallThreadCrashStack(); // Bionic gives every thread one, too.
if (values.initializer != nullptr) {
values.initializer();
}
Expand Down
83 changes: 83 additions & 0 deletions src/compat/crash_reporter.cc
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include "compat/crash_reporter.h"

#include <fcntl.h>
#include <sys/mman.h>
#include <sys/prctl.h>
#include <sys/syscall.h>
#include <ucontext.h>
Expand All @@ -12,6 +13,7 @@
#include <cstddef>
#include <cstdint>
#include <cstring>
#include <ctime>

namespace mocktail::compat {
namespace {
Expand All @@ -34,6 +36,8 @@ struct Mapping {

std::atomic<bool> g_installed{false};
std::atomic<bool> g_reported{false};
std::atomic<bool> g_report_done{false};
std::atomic<long> g_reporter_tid{0};
std::atomic<int> g_guest_calls{0};
struct sigaction g_guest[kSignalSlots];
std::atomic<bool> g_guest_set[kSignalSlots];
Expand Down Expand Up @@ -333,10 +337,26 @@ void ResetAndResend(int signal_number, const siginfo_t* info) {
}
}

// A second crashing thread would otherwise kill the process mid-report.
void WaitForReport() {
constexpr timespec kStep{0, 10'000'000};
for (int step = 0; step < 200; ++step) {
if (g_report_done.load(std::memory_order_acquire)) {
return;
}
nanosleep(&kStep, nullptr);
}
}

void HandleCrash(int signal_number, siginfo_t* info, void* context) {
const int saved_errno = errno;
const long tid = static_cast<long>(syscall(SYS_gettid));
if (!g_reported.exchange(true, std::memory_order_acq_rel)) {
g_reporter_tid.store(tid, std::memory_order_release);
Report(signal_number, info, context);
g_report_done.store(true, std::memory_order_release);
} else if (g_reporter_tid.load(std::memory_order_acquire) != tid) {
WaitForReport();
}
if (signal_number > 0 && signal_number < kSignalSlots &&
g_guest_set[signal_number].load(std::memory_order_acquire) &&
Expand Down Expand Up @@ -440,4 +460,67 @@ bool InstallCrashReporter() {
return true;
}

namespace {

void IgnoreSignal(int) {}

constexpr std::size_t kThreadAltStackSize = 64 * 1024;

class ThreadAltStack {
public:
ThreadAltStack() {
stack_t current{};
if (sigaltstack(nullptr, &current) != 0 ||
(current.ss_flags & SS_DISABLE) == 0) {
return;
}
void* const memory =
mmap(nullptr, kThreadAltStackSize, PROT_READ | PROT_WRITE,
MAP_PRIVATE | MAP_ANONYMOUS | MAP_STACK, -1, 0);
if (memory == MAP_FAILED) {
return;
}
stack_t stack{};
stack.ss_sp = memory;
stack.ss_size = kThreadAltStackSize;
if (sigaltstack(&stack, nullptr) != 0) {
munmap(memory, kThreadAltStackSize);
return;
}
memory_ = memory;
}

~ThreadAltStack() {
if (memory_ == nullptr) {
return;
}
stack_t off{};
off.ss_flags = SS_DISABLE;
sigaltstack(&off, nullptr);
munmap(memory_, kThreadAltStackSize);
}

ThreadAltStack(const ThreadAltStack&) = delete;
ThreadAltStack& operator=(const ThreadAltStack&) = delete;

private:
void* memory_ = nullptr;
};

}

void InstallThreadCrashStack() {
// Freed at thread exit, by return or pthread_exit alike.
thread_local ThreadAltStack stack;
(void)stack;
}

bool IgnoreBrokenPipes() {
struct sigaction action {};
action.sa_handler = IgnoreSignal; // execve resets a handler; not SIG_IGN.
action.sa_flags = SA_RESTART;
sigemptyset(&action.sa_mask);
return sigaction(SIGPIPE, &action, nullptr) == 0;
}

}
17 changes: 15 additions & 2 deletions src/main.cc
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
#include "runtime/graphics_launch_policy.h"
#include "runtime/luau_codegen_switch.h"
#include "runtime/memory_limit.h"
#include "runtime/owned_pthread.h"
#include "runtime/payload_update_preflight.h"
#include "runtime/performance_policy.h"
#include "runtime/platform_cache_migration.h"
Expand Down Expand Up @@ -199,7 +200,8 @@ void RequestAccountSwitchRestart(void* vm, bool signed_in) {
if (signed_in) {
const auto since =
std::filesystem::file_time_type::clock::now() - std::chrono::seconds(2);
std::thread([app_storage = g_app_storage_file, since] {
const bool started = mocktail::runtime::StartDetachedThread(
[app_storage = g_app_storage_file, since] {
const mocktail::runtime::SavedAccountBlob result =
mocktail::runtime::WaitForSavedRobloxAccountBlob(
app_storage, since, std::chrono::seconds(5));
Expand All @@ -210,7 +212,14 @@ void RequestAccountSwitchRestart(void* vm, bool signed_in) {
<< std::flush;
g_account_switch_restart = true;
kill(getpid(), SIGTERM);
}).detach();
});
if (!started) {
std::cout << " [auth] account switched; restarting without waiting "
"for the account blob\n"
<< std::flush;
g_account_switch_restart = true;
kill(getpid(), SIGTERM);
}
return;
}
g_account_switch_restart = true;
Expand Down Expand Up @@ -919,6 +928,10 @@ int main(int argc, char* argv[]) {
if (!mocktail::compat::InstallCrashReporter()) {
std::cerr << " [crash] reporter unavailable\n";
}
if (!mocktail::compat::IgnoreBrokenPipes()) {
std::cerr << " [crash] cannot ignore SIGPIPE; a closed pipe will "
"kill the process\n";
}
mocktail::runtime::InstallCpuLimitDiagnostics();
mocktail::runtime::LogProcessDiagnostics(
mocktail::runtime::ProcessDiagnosticStage::kStartup);
Expand Down
10 changes: 8 additions & 2 deletions src/runtime/desktop_notification.cc
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#include "runtime/desktop_notification.h"

#include "runtime/owned_pthread.h"

#include <gio/gio.h>

#include <cstdlib>
Expand Down Expand Up @@ -48,7 +50,8 @@ DesktopNotificationCall BuildDesktopNotificationCall(bool sandboxed,
}

void ShowDesktopNotification(std::string title, std::string body) {
std::thread([title = std::move(title), body = std::move(body)] {
const bool started = StartDetachedThread([title = std::move(title),
body = std::move(body)] {
DesktopNotificationCall call = BuildDesktopNotificationCall(
std::getenv("FLATPAK_ID") != nullptr, title, body);
GError* error = nullptr;
Expand All @@ -70,7 +73,10 @@ void ShowDesktopNotification(std::string title, std::string body) {
g_clear_error(&error);
}
g_variant_unref(call.parameters);
}).detach();
});
if (!started) {
std::cerr << " [notify] cannot start the notification thread\n";
}
}

void NotifyWebRobuxPurchase(bool browser_opened) {
Expand Down
31 changes: 31 additions & 0 deletions src/runtime/owned_pthread.cc
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
#include <cerrno>
#include <chrono>
#include <cstdlib>
#include <memory>
#include <new>
#include <thread>

namespace mocktail {
Expand Down Expand Up @@ -121,5 +123,34 @@ const char* OwnedPthreadWaitStatusName(OwnedPthreadWaitStatus status) {
return "Unknown";
}

bool StartDetachedThread(std::function<void()> work) {
auto* owned = new (std::nothrow) std::function<void()>(std::move(work));
if (owned == nullptr) {
return false;
}
pthread_attr_t attributes;
if (pthread_attr_init(&attributes) != 0) {
delete owned;
return false;
}
pthread_attr_setdetachstate(&attributes, PTHREAD_CREATE_DETACHED);
pthread_t thread;
const int result = pthread_create(
&thread, &attributes,
[](void* context) -> void* {
std::unique_ptr<std::function<void()>> run(
static_cast<std::function<void()>*>(context));
(*run)();
return nullptr;
},
owned);
pthread_attr_destroy(&attributes);
if (result != 0) {
delete owned;
return false;
}
return true;
}

} // namespace runtime
} // namespace mocktail
9 changes: 7 additions & 2 deletions src/runtime/roblox_store_purchase.cc
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include <utility>

#include "runtime/desktop_notification.h"
#include "runtime/owned_pthread.h"

namespace mocktail {
namespace runtime {
Expand Down Expand Up @@ -69,13 +70,17 @@ void HandOffRobuxPurchaseToBrowser() {
std::cerr << " [store] repeated Robux purchase ignored\n";
return;
}
std::thread([] {
const bool started = StartDetachedThread([] {
const bool opened = OpenUrlInDefaultBrowser(kWebRobuxPurchaseUrl);
std::cerr << (opened ? " [store] opened the Robux page in the browser\n"
: " [store] could not open the Robux page in the "
"browser\n");
NotifyWebRobuxPurchase(opened);
}).detach();
});
if (!started) {
std::cerr << " [store] could not open the Robux page in the browser\n";
NotifyWebRobuxPurchase(false);
}
}

std::vector<EnvironmentChange> BrowserEnvironmentChanges(
Expand Down
6 changes: 4 additions & 2 deletions src/window/window.cc
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
#include "mocktail/platform/sdl_event_converter.h"
#include "mocktail/platform/sdl_window_icon.h"
#include "runtime/memory_limit.h"
#include "runtime/owned_pthread.h"
#include "window/host_surface_commit_gate.h"
#include "window/input_pump_pacer.h"
#include "window/main_thread_command_gate.h"
Expand Down Expand Up @@ -1597,7 +1598,8 @@ void MaybeLogProcessSample(std::uint64_t now_ns) {
due, now_ns + kProcessSampleIntervalNs, std::memory_order_relaxed)) {
return;
}
std::thread([] {
// Skipped, not retried, when the thread can't start; the next one is 60 s out.
(void)runtime::StartDetachedThread([] {
const runtime::ProcessMemoryUsage usage =
runtime::ReadCurrentProcessMemory();
if (!usage.valid) {
Expand All @@ -1609,7 +1611,7 @@ void MaybeLogProcessSample(std::uint64_t now_ns) {
static_cast<unsigned long long>(usage.swap_bytes >> 20),
static_cast<unsigned long long>(usage.threads),
runtime::CountOpenFileDescriptors());
}).detach();
});
}

} // namespace
Expand Down
Loading