Commit 0f29ca4d authored by jan.koester's avatar jan.koester
Browse files

test

parent 353eeea8
Loading
Loading
Loading
Loading
+1 −1
Changes for src/connection.h: 1 added line, 1 removed line.
Original line number Diff line number Diff line
@@ -184,7 +184,7 @@ namespace netplus {
            // Allows frameworks to attach per-connection state without external maps.
            std::unique_ptr<void, void(*)(void*)> extension{nullptr, +[](void*){}};

            std::mutex event_mutex;
            std::recursive_mutex event_mutex;

             time_t lasteventime;
        protected:
+4 −4
Changes for src/event/epoll.cpp: 4 added lines, 4 removed lines.
Original line number Diff line number Diff line
@@ -243,7 +243,7 @@ namespace netplus {

                // Serialize connection setup with I/O for this fd
                {
                    std::lock_guard<std::mutex> lk(ccon->event_mutex);
                    std::lock_guard<std::recursive_mutex> lk(ccon->event_mutex);

                    epoll_event setevent{};
                    setevent.events = EPOLLIN | EPOLLRDHUP | EPOLLONESHOT;
@@ -290,7 +290,7 @@ namespace netplus {

            bool doClose = false;
            {
                std::lock_guard<std::mutex> lk(c->event_mutex);
                std::lock_guard<std::recursive_mutex> lk(c->event_mutex);
                if (!c->csock) return;

                EpollArmGuard rearm(fd, c, this);
@@ -540,7 +540,7 @@ namespace netplus {

            // now lock per connection
            {
                std::lock_guard<std::mutex> lk(ccon->event_mutex);
                std::lock_guard<std::recursive_mutex> lk(ccon->event_mutex);

                // remove from epoll
                epoll_ctl(_pollFD, EPOLL_CTL_DEL, fd, nullptr);
@@ -701,7 +701,7 @@ namespace netplus {

        for (auto& c : conns) {
            if (!c) continue;
            std::lock_guard<std::mutex> lk(c->event_mutex);
            std::lock_guard<std::recursive_mutex> lk(c->event_mutex);
            safeCloseSocket(c);
        }
    }
+3 −3
Changes for src/event/poll.cpp: 3 added lines, 3 removed lines.
Original line number Diff line number Diff line
@@ -372,7 +372,7 @@ namespace netplus {
            bool needClose = false;

            {
                std::lock_guard<std::mutex> lk(c->event_mutex);
                std::lock_guard<std::recursive_mutex> lk(c->event_mutex);
                if (!c->csock) return;

                try {
@@ -547,7 +547,7 @@ namespace netplus {
            if (!ccon) return;

            {
                std::lock_guard<std::mutex> lk(ccon->event_mutex);
                std::lock_guard<std::recursive_mutex> lk(ccon->event_mutex);

                // Remove from select master sets
                FD_CLR(fd, &_masterReadSet);
@@ -731,7 +731,7 @@ namespace netplus {

        for (auto& c : conns) {
            if (!c) continue;
            std::lock_guard<std::mutex> lk(c->event_mutex);
            std::lock_guard<std::recursive_mutex> lk(c->event_mutex);
            safeCloseSocket(c);
        }

+42 −79
Changes for src/quic.cpp: 42 added lines, 79 removed lines.
Original line number Diff line number Diff line
@@ -115,17 +115,15 @@ quic::quic(const std::map<std::string, ssl::CertificateBundle>& certs,
}

quic::~quic() {
    // Remove from parent's routing map before taking our own lock
    // Use try_to_lock to avoid deadlock if parent is being destroyed
    // quic_mtx() returns parent's mutex for children, our own for the parent.
    // recursive_mutex allows re-entry when parent already holds the lock
    // (e.g. during _child_owned_connections cleanup).
    std::lock_guard<std::recursive_mutex> lock(quic_mtx());
    
    if (_parent != nullptr) {
        std::unique_lock<std::recursive_mutex> parent_lock(_parent->_quic_mutex, std::try_to_lock);
        if (parent_lock.owns_lock()) {
        _parent->_child_connections.erase(_original_dcid);
        _parent->_child_connections.erase(_local_cid);
    }
    }
    
    std::lock_guard<std::recursive_mutex> lock(_quic_mutex);
    
    // Clean up streams
    _streams.clear();
@@ -316,7 +314,7 @@ bool quic::validateRetryToken(const std::vector<uint8_t>& token) {
// ============================================================================

void quic::setCertificates(const std::map<std::string, ssl::CertificateBundle>& certs) {
    std::lock_guard<std::recursive_mutex> lock(_quic_mutex);
    std::lock_guard<std::recursive_mutex> lock(quic_mtx());
    _cert_map = certs;
}

@@ -921,7 +919,7 @@ bool quic::unprotectPacket(const uint8_t* data, size_t len,
// ============================================================================

void quic::pushReceivedData(const uint8_t* data, size_t len) {
    std::lock_guard<std::recursive_mutex> lock(_quic_mutex);
    std::lock_guard<std::recursive_mutex> lock(quic_mtx());
    _recv_buffer.insert(_recv_buffer.end(), data, data + len);
}

@@ -932,7 +930,7 @@ void quic::pushReceivedData(const uint8_t* data, size_t len) {
void quic::handshake_after_accept() {
    std::vector<PendingDispatch> dispatches;
    {
    std::lock_guard<std::recursive_mutex> lock(_quic_mutex);
    std::lock_guard<std::recursive_mutex> lock(quic_mtx());
    
    
    // === HANDSHAKE TIMEOUT CHECK (SERIAL) ===
@@ -1030,7 +1028,7 @@ void quic::handshake_after_accept() {
// ============================================================================

void quic::accept(std::unique_ptr<socket>& csock, bool nonblock) {
    std::unique_lock<std::recursive_mutex> lock(_quic_mutex);
    std::unique_lock<std::recursive_mutex> lock(quic_mtx());
    
    
    // Purge stale (Closed) connections from ownership vector
@@ -1131,34 +1129,23 @@ void quic::accept(std::unique_ptr<socket>& csock, bool nonblock) {
                existing->_conn_state.load() == ConnectionState::Draining) {
                _child_connections.erase(it);
            } else {
                
                // Release parent's mutex before acquiring child's mutex
                lock.unlock();
                {
                    std::lock_guard<std::recursive_mutex> child_lock(existing->_quic_mutex);
                // Parent lock covers child state — no separate child lock needed
                existing->processIncomingPacket(dgram.data(), dgram.size());
                }
                // Dispatch stream callbacks inline (lock already released).
                // Avoids per-packet thread creation overhead and use-after-free
                // risk from detached threads holding raw pointers.
                {
                    std::vector<PendingDispatch> dispatches;
                    {
                        std::lock_guard<std::recursive_mutex> child_lock(existing->_quic_mutex);
                        dispatches = std::move(existing->_pending_dispatches);

                // Dispatch stream callbacks outside the lock
                if (!existing->_pending_dispatches.empty() && existing->_stream_callback) {
                    std::vector<PendingDispatch> dispatches = std::move(existing->_pending_dispatches);
                    existing->_pending_dispatches.clear();
                    }
                    if (!dispatches.empty() && existing->_stream_callback) {
                    lock.unlock();
                    for (auto& pd : dispatches) {
                        existing->_stream_callback(existing, pd.stream_id, pd.data, pd.fin);
                    }
                    }
                    lock.lock();
                }
            }
        }
        
        // Re-lock parent and drain next datagram
        lock.lock();
        // lock is always held here — just continue to next datagram
        continue;
    }
    
@@ -1233,31 +1220,19 @@ void quic::accept(std::unique_ptr<socket>& csock, bool nonblock) {
            
            // Fall through to create a new connection
        } else {
            // Active connection - route packet to it
            
            // Release parent's mutex before acquiring child's mutex to avoid deadlock
            lock.unlock();
            {
                std::lock_guard<std::recursive_mutex> child_lock(existing->_quic_mutex);
            // Active connection - parent lock covers child state
            existing->processIncomingPacket(dgram.data(), dgram.size());
            }
            // Dispatch stream callbacks inline (lock already released).
            {
                std::vector<PendingDispatch> dispatches;
                {
                    std::lock_guard<std::recursive_mutex> child_lock(existing->_quic_mutex);
                    dispatches = std::move(existing->_pending_dispatches);

            // Dispatch stream callbacks outside the lock
            if (!existing->_pending_dispatches.empty() && existing->_stream_callback) {
                std::vector<PendingDispatch> dispatches = std::move(existing->_pending_dispatches);
                existing->_pending_dispatches.clear();
                }
                if (!dispatches.empty() && existing->_stream_callback) {
                lock.unlock();
                for (auto& pd : dispatches) {
                    existing->_stream_callback(existing, pd.stream_id, pd.data, pd.fin);
                }
                }
            }
            
            // Re-lock parent and drain next datagram
                lock.lock();
            }
            continue;
        }
    }
@@ -1693,13 +1668,8 @@ void quic::processApplicationPacket(const uint8_t* data, size_t len) {
// ============================================================================

void quic::close() {
    // Collect parent info and CIDs before locking parent
    quic* parent_ptr = nullptr;
    std::vector<uint8_t> orig_dcid;
    std::vector<uint8_t> local_cid;
    
    {
        std::lock_guard<std::recursive_mutex> lock(_quic_mutex);
    // quic_mtx() returns parent's mutex for children — one lock covers both
    std::lock_guard<std::recursive_mutex> lock(quic_mtx());
    
    // Don't close if already closed
    if (_conn_state.load() == ConnectionState::Closed) {
@@ -1750,11 +1720,6 @@ void quic::close() {
    }
    _streams.clear();
    
        // Save parent info for removal outside our lock
        parent_ptr = _parent;
        orig_dcid = _original_dcid;
        local_cid = _local_cid;
        
    // Clear send queue
    _send_queue.clear();
    
@@ -1808,13 +1773,11 @@ void quic::close() {
    
    _conn_state.store(ConnectionState::Closed);
    _handshake_complete = false;
    }  // Release _quic_mutex BEFORE accessing parent
    
    // Remove from parent's child map without holding our own mutex (prevents deadlock)
    if (parent_ptr != nullptr) {
        std::lock_guard<std::recursive_mutex> parent_lock(parent_ptr->_quic_mutex);
        parent_ptr->_child_connections.erase(orig_dcid);
        parent_ptr->_child_connections.erase(local_cid);
    // Remove from parent's child map (same lock — quic_mtx() is parent's mutex)
    if (_parent != nullptr) {
        _parent->_child_connections.erase(_original_dcid);
        _parent->_child_connections.erase(_local_cid);
    }
}

@@ -1991,7 +1954,7 @@ std::vector<uint8_t> quic::buildShortHeaderPacket(const std::vector<uint8_t>& pa
// ============================================================================

void quic::connect(const std::string& addr, int port, bool nonblock) {
    std::lock_guard<std::recursive_mutex> lock(_quic_mutex);
    std::lock_guard<std::recursive_mutex> lock(quic_mtx());
    
    // Ensure we're in idle state
    if (_conn_state.load() != ConnectionState::Idle) {
@@ -2382,7 +2345,7 @@ size_t quic::recvData(buffer& data, int flags) {
    size_t result = 0;

    {   // Scoped lock — released before invoking stream callbacks
        std::lock_guard<std::recursive_mutex> lock(_quic_mutex);
        std::lock_guard<std::recursive_mutex> lock(quic_mtx());

        // Check connection state
        if (_conn_state.load() == ConnectionState::Closed) {
@@ -2486,7 +2449,7 @@ void quic::pumpNetwork(int flags) {
    if (recv_len > 0) {
        std::vector<PendingDispatch> dispatches;
        {
            std::lock_guard<std::recursive_mutex> lock(_quic_mutex);
            std::lock_guard<std::recursive_mutex> lock(quic_mtx());
            processIncomingPacket(reinterpret_cast<const uint8_t*>(_pump_buf.data.buf), recv_len);

            // Flush pending flow control frames so the peer doesn't stall
@@ -2520,7 +2483,7 @@ void quic::pumpNetwork(int flags) {
// ============================================================================

size_t quic::recvStreamData(uint64_t stream_id, uint8_t* data, size_t max_len) {
    std::lock_guard<std::recursive_mutex> lock(_quic_mutex);
    std::lock_guard<std::recursive_mutex> lock(quic_mtx());
    
    auto it = _streams.find(stream_id);
    if (it == _streams.end()) {
@@ -2548,7 +2511,7 @@ size_t quic::recvStreamData(uint64_t stream_id, uint8_t* data, size_t max_len) {
// ============================================================================

bool quic::hasStreamData(uint64_t stream_id) const {
    std::lock_guard<std::recursive_mutex> lock(_quic_mutex);
    std::lock_guard<std::recursive_mutex> lock(quic_mtx());
    
    auto it = _streams.find(stream_id);
    if (it == _streams.end()) {
@@ -2558,7 +2521,7 @@ bool quic::hasStreamData(uint64_t stream_id) const {
}

bool quic::isStreamComplete(uint64_t stream_id) const {
    std::lock_guard<std::recursive_mutex> lock(_quic_mutex);
    std::lock_guard<std::recursive_mutex> lock(quic_mtx());

    auto it = _streams.find(stream_id);
    if (it == _streams.end()) {
@@ -3053,7 +3016,7 @@ void quic::processAckFrame(const uint8_t* data, size_t len, size_t& offset) {
// ============================================================================

void quic::sendStreamData(uint64_t stream_id, const std::vector<uint8_t>& data, bool fin) {
    std::lock_guard<std::recursive_mutex> lock(_quic_mutex);
    std::lock_guard<std::recursive_mutex> lock(quic_mtx());

    // Maximum QUIC datagram size minus short header overhead:
    //   1 (flags) + dcid_len + 4 (PN) + 16 (GCM tag) + frame header (~11 max)
@@ -3678,7 +3641,7 @@ void quic::sendHandshakeDone() {
// ============================================================================

size_t quic::sendData(buffer& data, int flags) {
    std::lock_guard<std::recursive_mutex> lock(_quic_mutex);
    std::lock_guard<std::recursive_mutex> lock(quic_mtx());
    
    // Check connection state
    if (_conn_state.load() == ConnectionState::Closed) {
@@ -3782,7 +3745,7 @@ size_t quic::sendData(buffer& data, int flags) {
// ============================================================================

size_t quic::sendStreamData(uint64_t stream_id, const uint8_t* data, size_t len, bool fin) {
    std::lock_guard<std::recursive_mutex> lock(_quic_mutex);
    std::lock_guard<std::recursive_mutex> lock(quic_mtx());
    
    if (_conn_state.load() != ConnectionState::Connected && !_handshake_complete) {
        return 0;
@@ -3974,7 +3937,7 @@ std::vector<uint8_t> quic::buildAckFrame(uint64_t largest_ack, uint64_t ack_dela
// ============================================================================

void quic::flush_out() {
    std::lock_guard<std::recursive_mutex> lock(_quic_mutex);
    std::lock_guard<std::recursive_mutex> lock(quic_mtx());
    
    // Send any queued packets
    while (!_send_queue.empty()) {
@@ -4012,7 +3975,7 @@ void quic::flush_out() {
// ============================================================================

uint64_t quic::openStream(bool bidirectional) {
    std::lock_guard<std::recursive_mutex> lock(_quic_mutex);
    std::lock_guard<std::recursive_mutex> lock(quic_mtx());
    
    uint64_t stream_id;
    
@@ -4048,7 +4011,7 @@ uint64_t quic::openStream(bool bidirectional) {
// ============================================================================

void quic::closeStream(uint64_t stream_id) {
    std::lock_guard<std::recursive_mutex> lock(_quic_mutex);
    std::lock_guard<std::recursive_mutex> lock(quic_mtx());
    
    auto it = _streams.find(stream_id);
    if (it == _streams.end()) {
+6 −0
Changes for src/socket.h: 6 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -888,6 +888,12 @@ namespace netplus {
		// callbacks re-enter locking functions like sendStreamData)
		mutable std::recursive_mutex _quic_mutex;

		// Children delegate to parent's mutex so there is only ONE lock
		// per QUIC listener — eliminates lock-ordering complexity.
		std::recursive_mutex& quic_mtx() const {
			return _parent ? _parent->_quic_mutex : _quic_mutex;
		}

		// Friend classes for event handling
		friend class poll;
		friend class event;