Loading src/quic.cpp +52 −6 Changes for src/quic.cpp: 52 added lines, 6 removed lines. Original line number Diff line number Diff line Loading @@ -930,6 +930,8 @@ void quic::pushReceivedData(const uint8_t* data, size_t len) { // ============================================================================ void quic::handshake_after_accept() { std::vector<PendingDispatch> dispatches; { std::lock_guard<std::mutex> lock(_quic_mutex); Loading Loading @@ -1009,6 +1011,18 @@ void quic::handshake_after_accept() { _last_activity = now; } } // Collect pending dispatches before releasing lock dispatches = std::move(_pending_dispatches); _pending_dispatches.clear(); } // Release _quic_mutex // Dispatch stream callbacks outside the lock if (!dispatches.empty() && _stream_callback) { for (auto& pd : dispatches) { _stream_callback(this, pd.stream_id, pd.data, pd.fin); } } } // ============================================================================ Loading Loading @@ -1339,6 +1353,20 @@ void quic::accept(std::unique_ptr<socket>& csock, bool nonblock) { // Process the Initial packet to start handshake child_ptr->processIncomingPacket(dgram.data(), dgram.size()); // Dispatch any pending child callbacks outside the parent lock { std::vector<PendingDispatch> child_dispatches; child_dispatches = std::move(child_ptr->_pending_dispatches); child_ptr->_pending_dispatches.clear(); if (!child_dispatches.empty() && child_ptr->_stream_callback) { lock.unlock(); for (auto& pd : child_dispatches) { child_ptr->_stream_callback(child_ptr, pd.stream_id, pd.data, pd.fin); } lock.lock(); } } // Child connection is owned by parent's _child_owned_connections. // Don't transfer ownership to csock (would cause double-free). // QUIC connections share the parent socket and are routed via accept(). Loading Loading @@ -2456,6 +2484,8 @@ void quic::pumpNetwork(int flags) { int recv_flags = (flags & MSG_DONTWAIT) ? MSG_DONTWAIT : 0; size_t recv_len = udp::recvData(_pump_buf, recv_flags); if (recv_len > 0) { std::vector<PendingDispatch> dispatches; { std::lock_guard<std::mutex> lock(_quic_mutex); processIncomingPacket(reinterpret_cast<const uint8_t*>(_pump_buf.data.buf), recv_len); Loading @@ -2470,6 +2500,18 @@ void quic::pumpNetwork(int flags) { _pending_max_data = false; } } // Collect pending dispatches before releasing lock dispatches = std::move(_pending_dispatches); _pending_dispatches.clear(); } // Dispatch stream callbacks outside the lock if (!dispatches.empty() && _stream_callback) { for (auto& pd : dispatches) { _stream_callback(this, pd.stream_id, pd.data, pd.fin); } } } } Loading Loading @@ -2938,9 +2980,9 @@ void quic::processStreamFrame(const uint8_t* data, size_t len, size_t& offset) { } // Queue stream dispatch — will be invoked OUTSIDE _quic_mutex // so that long-running callbacks (e.g. import) don't block PING ACKs. // For bidirectional request streams (stream_id % 4 == 0): wait until complete // For unidirectional streams: notify immediately (control/QPACK streams) // so that long-running callbacks (e.g. import) don't block PING ACKs // and don't deadlock when the callback calls sendStreamData/flush_out. // ALL stream types are deferred via _pending_dispatches. if (_is_server && _stream_callback) { bool is_bidi_request = (stream_id % 4 == 0); if (is_bidi_request && stream.recv_complete && !stream.recv_dispatched) { Loading @@ -2952,9 +2994,13 @@ void quic::processStreamFrame(const uint8_t* data, size_t len, size_t& offset) { stream.recv_buffer.clear(); stream.recv_buffer.shrink_to_fit(); stream.recv_ranges.clear(); } else if (!is_bidi_request) { // Unidirectional stream - notify immediately (control/QPACK, small) _stream_callback(this, stream_id, stream.recv_buffer, stream.recv_fin); } else if (!is_bidi_request && stream.recv_fin && !stream.recv_dispatched) { // Unidirectional stream with FIN — defer via _pending_dispatches // to avoid deadlock (callback may call sendStreamData which locks _quic_mutex) stream.recv_dispatched = true; _pending_dispatches.push_back({stream_id, std::vector<uint8_t>(stream.recv_buffer), true}); } } } Loading Loading
src/quic.cpp +52 −6 Changes for src/quic.cpp: 52 added lines, 6 removed lines. Original line number Diff line number Diff line Loading @@ -930,6 +930,8 @@ void quic::pushReceivedData(const uint8_t* data, size_t len) { // ============================================================================ void quic::handshake_after_accept() { std::vector<PendingDispatch> dispatches; { std::lock_guard<std::mutex> lock(_quic_mutex); Loading Loading @@ -1009,6 +1011,18 @@ void quic::handshake_after_accept() { _last_activity = now; } } // Collect pending dispatches before releasing lock dispatches = std::move(_pending_dispatches); _pending_dispatches.clear(); } // Release _quic_mutex // Dispatch stream callbacks outside the lock if (!dispatches.empty() && _stream_callback) { for (auto& pd : dispatches) { _stream_callback(this, pd.stream_id, pd.data, pd.fin); } } } // ============================================================================ Loading Loading @@ -1339,6 +1353,20 @@ void quic::accept(std::unique_ptr<socket>& csock, bool nonblock) { // Process the Initial packet to start handshake child_ptr->processIncomingPacket(dgram.data(), dgram.size()); // Dispatch any pending child callbacks outside the parent lock { std::vector<PendingDispatch> child_dispatches; child_dispatches = std::move(child_ptr->_pending_dispatches); child_ptr->_pending_dispatches.clear(); if (!child_dispatches.empty() && child_ptr->_stream_callback) { lock.unlock(); for (auto& pd : child_dispatches) { child_ptr->_stream_callback(child_ptr, pd.stream_id, pd.data, pd.fin); } lock.lock(); } } // Child connection is owned by parent's _child_owned_connections. // Don't transfer ownership to csock (would cause double-free). // QUIC connections share the parent socket and are routed via accept(). Loading Loading @@ -2456,6 +2484,8 @@ void quic::pumpNetwork(int flags) { int recv_flags = (flags & MSG_DONTWAIT) ? MSG_DONTWAIT : 0; size_t recv_len = udp::recvData(_pump_buf, recv_flags); if (recv_len > 0) { std::vector<PendingDispatch> dispatches; { std::lock_guard<std::mutex> lock(_quic_mutex); processIncomingPacket(reinterpret_cast<const uint8_t*>(_pump_buf.data.buf), recv_len); Loading @@ -2470,6 +2500,18 @@ void quic::pumpNetwork(int flags) { _pending_max_data = false; } } // Collect pending dispatches before releasing lock dispatches = std::move(_pending_dispatches); _pending_dispatches.clear(); } // Dispatch stream callbacks outside the lock if (!dispatches.empty() && _stream_callback) { for (auto& pd : dispatches) { _stream_callback(this, pd.stream_id, pd.data, pd.fin); } } } } Loading Loading @@ -2938,9 +2980,9 @@ void quic::processStreamFrame(const uint8_t* data, size_t len, size_t& offset) { } // Queue stream dispatch — will be invoked OUTSIDE _quic_mutex // so that long-running callbacks (e.g. import) don't block PING ACKs. // For bidirectional request streams (stream_id % 4 == 0): wait until complete // For unidirectional streams: notify immediately (control/QPACK streams) // so that long-running callbacks (e.g. import) don't block PING ACKs // and don't deadlock when the callback calls sendStreamData/flush_out. // ALL stream types are deferred via _pending_dispatches. if (_is_server && _stream_callback) { bool is_bidi_request = (stream_id % 4 == 0); if (is_bidi_request && stream.recv_complete && !stream.recv_dispatched) { Loading @@ -2952,9 +2994,13 @@ void quic::processStreamFrame(const uint8_t* data, size_t len, size_t& offset) { stream.recv_buffer.clear(); stream.recv_buffer.shrink_to_fit(); stream.recv_ranges.clear(); } else if (!is_bidi_request) { // Unidirectional stream - notify immediately (control/QPACK, small) _stream_callback(this, stream_id, stream.recv_buffer, stream.recv_fin); } else if (!is_bidi_request && stream.recv_fin && !stream.recv_dispatched) { // Unidirectional stream with FIN — defer via _pending_dispatches // to avoid deadlock (callback may call sendStreamData which locks _quic_mutex) stream.recv_dispatched = true; _pending_dispatches.push_back({stream_id, std::vector<uint8_t>(stream.recv_buffer), true}); } } } Loading