Skip to content

Truly independent web browser

LibRequests: Keep the response read buffer off static TLS

Andreas Kling8525ed78110d
diff --git a/Libraries/LibRequests/Request.cpp b/Libraries/LibRequests/Request.cpp
index 6042c88fbc2..19a766d701a 100644
--- a/Libraries/LibRequests/Request.cpp
+++ b/Libraries/LibRequests/Request.cpp
@@ -416,7 +416,10 @@ void Request::set_up_internal_stream_data(DataReceived on_data_available)
m_internal_stream_data->read_notifier->on_activation = [this]() {
static constexpr size_t buffer_size = 256 * KiB;
- static thread_local char buffer[buffer_size];
+ // NB: The buffer lives on the heap, as a thread_local array this large would be static TLS that every thread
+ // of the process zeroes as it starts, reading requests or not. It is allocated the first time a thread
+ // reads a response and kept for the life of that thread's event loop.
+ static thread_local u8* buffer = nullptr;
// If the request was stopped while this IPC was in-flight, just bail.
if (!m_internal_stream_data || !m_internal_stream_data->read_stream)
@@ -424,6 +427,9 @@ void Request::set_up_internal_stream_data(DataReceived on_data_available)
NonnullRefPtr protector { *this };
+ if (!buffer)
+ buffer = new u8[buffer_size];
+
do {
auto bytes_to_read = buffer_size;
if (m_internal_stream_data->body_delivery_remaining_byte_count.has_value()) {