From 450bf6dbf17be76da166b0fc29427f70a1ddc6d4 Mon Sep 17 00:00:00 2001 From: Andrew Noyes Date: Tue, 19 Aug 2025 16:03:33 -0400 Subject: [PATCH] Add Connection::reset There was an issue where the connection deque was referring to old arena memory --- design.md | 6 +++--- src/connection.cpp | 4 ---- src/connection.hpp | 7 +++++++ 3 files changed, 10 insertions(+), 7 deletions(-) diff --git a/design.md b/design.md index 44dd28f..474a095 100644 --- a/design.md +++ b/design.md @@ -121,7 +121,6 @@ Key features: Key features: - Arena allocator per connection for efficient memory management -- **Request/Response arena lifecycle**: Arena resets after each complete request/response cycle - Weak reference to server for safe cleanup after server destruction - Private networking details accessible only to Server via friend relationship - Public handler interface: appendMessage(), closeAfterSend(), getArena(), getId() @@ -180,9 +179,10 @@ Arena Memory Lifecycle: 2. **Response Generation**: Handler uses arena for temporary response construction (headers, JSON, etc.) 3. **Response Queuing**: Handler calls `conn->appendMessage()` which copies data to arena-backed message queue 4. **Response Writing**: Server writes all queued messages to socket via `writeBytes()` -5. **Arena Reset**: After successful write completion, arena resets to reclaim all memory from the request/response cycle -This design assumes request/response pairs (HTTP-like protocols) but works for any protocol where there's a clear completion point for memory reclamation. +The user is responsible for calling `conn->reset()` periodically to reclaim the +arena memory. A good time to do this for a request/response protocol would be +after all outgoing bytes have been written. ### API Design diff --git a/src/connection.cpp b/src/connection.cpp index 7d8ce4d..9d474d6 100644 --- a/src/connection.cpp +++ b/src/connection.cpp @@ -118,10 +118,6 @@ bool Connection::writeBytes() { } assert(messages_.empty()); - // Reset arena after completing request/response cycle - // This reclaims memory from request parsing and response generation - arena_.reset(); - return false; } diff --git a/src/connection.hpp b/src/connection.hpp index 50f3301..9b693b3 100644 --- a/src/connection.hpp +++ b/src/connection.hpp @@ -52,6 +52,13 @@ struct Connection { return result; } void *user_data = nullptr; + void reset() { + assert(messages_.empty()); + arena_.reset(); + messages_ = + std::deque>{ + ArenaStlAllocator{&arena_}}; + } // Note: To release connection back to server, use // Server::releaseBackToServer(std::move(connection_ptr))