wind: Fix client references being out-of-date in windows when disconnecting other clients

Classic "keeping a pointer to an element inside a vector after the vector is updated" bug, ah yes.
This commit is contained in:
apio 2023-08-16 20:02:54 +02:00
parent d43d06604d
commit b656ceedfe
Signed by: apio
GPG Key ID: B8A7D06E42258954
2 changed files with 15 additions and 7 deletions

View File

@ -8,4 +8,11 @@ struct Client
Vector<Window*> windows;
bool rpc_in_progress { false };
u8 rpc_id { 0 };
Client(os::LocalServer::Client&& client)
#ifdef CLIENT_IMPLEMENTATION
: conn(move(client)), windows() {}
#else
;
#endif
};

View File

@ -1,3 +1,4 @@
#define CLIENT_IMPLEMENTATION
#include "Client.h"
#include "IPC.h"
#include "Mouse.h"
@ -18,7 +19,7 @@
#include <time.h>
#include <unistd.h>
static void debug(const Vector<Client>& clients)
static void debug(const Vector<OwnedPtr<Client>>& clients)
{
os::println("--- wind: DEBUG OUTPUT ---");
@ -26,7 +27,7 @@ static void debug(const Vector<Client>& clients)
for (const auto& client : clients)
{
os::println("Client with fd %d, owns %zu windows", client.conn.fd(), client.windows.size());
os::println("Client with fd %d, owns %zu windows", client->conn.fd(), client->windows.size());
}
os::println("-- wind: Listing windows --");
@ -113,7 +114,7 @@ Result<int> luna_main(int argc, char** argv)
ui::Color background = ui::BLACK;
Vector<Client> clients;
Vector<OwnedPtr<Client>> clients;
Vector<struct pollfd> fds;
TRY(fds.try_append({ .fd = mouse->fd(), .events = POLLIN, .revents = 0 }));
TRY(fds.try_append({ .fd = keyboard->fd(), .events = POLLIN, .revents = 0 }));
@ -149,14 +150,14 @@ Result<int> luna_main(int argc, char** argv)
}
for (usize i = 0; i < clients.size(); i++)
{
if (fds[i + 3].revents & POLLIN) wind::handle_ipc(clients[i]);
if (fds[i + 3].revents & POLLIN) wind::handle_ipc(*clients[i]);
if (fds[i + 3].revents & POLLHUP)
{
os::println("wind: Client %d disconnected", i);
fds.remove_at(i + 3);
auto client = clients.remove_at(i);
client.conn.disconnect();
for (auto& window : client.windows)
client->conn.disconnect();
for (auto& window : client->windows)
{
if (window)
{
@ -171,7 +172,7 @@ Result<int> luna_main(int argc, char** argv)
auto client = TRY(server->accept());
os::println("wind: New client connected!");
TRY(fds.try_append({ .fd = client.fd(), .events = POLLIN, .revents = 0 }));
Client c { move(client), {} };
OwnedPtr<Client> c = TRY(adopt_owned_if_nonnull(new Client(move(client))));
TRY(clients.try_append(move(c)));
}
}