Add Handles::clear() and run it on exit

This commit is contained in:
2025-10-09 16:48:33 -06:00
parent e185629d19
commit 0c2c0f653b
7 changed files with 18 additions and 7 deletions

View File

@@ -62,7 +62,6 @@ struct ProcessObject final : WaitableObject {
explicit ProcessObject(pid_t pid, int pidfd) : WaitableObject(kType), pid(pid), pidfd(pidfd) {}
~ProcessObject() override {
std::lock_guard lk(m);
if (pidfd != -1) {
close(pidfd);
pidfd = -1;
@@ -81,7 +80,6 @@ struct ThreadObject final : WaitableObject {
explicit ThreadObject(pthread_t thread) : WaitableObject(kType), thread(thread) {}
~ThreadObject() override {
std::lock_guard lk(m);
// Threads are detached at creation; we can safely drop
if (tib) {
wibo::destroyTib(tib);

View File

@@ -39,7 +39,6 @@ struct MappingObject : ObjectBase {
};
MappingObject::~MappingObject() {
std::lock_guard lk(m);
if (fd != -1) {
close(fd);
fd = -1;

View File

@@ -314,8 +314,7 @@ DWORD_PTR WIN_FUNC SetThreadAffinityMask(HANDLE hThread, DWORD_PTR dwThreadAffin
[[noreturn]] void exitInternal(DWORD exitCode) {
DEBUG_LOG("exitInternal(%u)\n", exitCode);
// We have some problems cleaning up when shutting down with exit;
// temporarily use _exit to terminate without running cleanup
wibo::handles().clear();
_exit(static_cast<int>(exitCode));
}

View File

@@ -1625,7 +1625,7 @@ namespace msvcrt {
void WIN_ENTRY _exit(int status) {
HOST_CONTEXT_GUARD();
DEBUG_LOG("_exit(%d)\n", status);
_Exit(status);
kernel32::exitInternal(status);
}
int WIN_ENTRY strcpy_s(char *dest, size_t dest_size, const char *src) {

View File

@@ -17,7 +17,6 @@
#include <utility>
kernel32::FsObject::~FsObject() {
std::lock_guard lk(m);
int fd = std::exchange(this->fd, -1);
if (fd >= 0 && closeOnDestroy) {
close(fd);

View File

@@ -28,6 +28,20 @@ inline bool isPseudo(HANDLE h) noexcept { return reinterpret_cast<int32_t>(h) <
} // namespace
Handles::~Handles() { clear(); }
void Handles::clear() {
for (auto &entry : mSlots) {
if (entry.obj) {
detail::deref(entry.obj);
}
}
mSlots.clear();
mFreeBelow.clear();
mFreeAbove.clear();
nextIndex = 0;
}
HANDLE Handles::alloc(Pin<> obj, uint32_t grantedAccess, uint32_t flags) {
std::unique_lock lk(m);

View File

@@ -189,7 +189,9 @@ class Handles {
public:
using OnHandleZeroFn = void (*)(ObjectBase *);
explicit Handles(OnHandleZeroFn cb) : mOnHandleZero(cb) {}
~Handles();
void clear();
HANDLE alloc(Pin<> obj, uint32_t grantedAccess, uint32_t flags);
bool release(HANDLE h);
Pin<> get(HANDLE h, HandleMeta *metaOut = nullptr);