Commit e34a0de4 authored by jan.koester's avatar jan.koester
Browse files

test

parent c0c0b24b
Loading
Loading
Loading
Loading
+34 −11
Changes for src/event/kqueue.cpp: 34 added lines, 11 removed lines.
Original line number Diff line number Diff line
@@ -69,6 +69,9 @@ namespace netplus {
        try {
            c->csock->close();
        } catch (...) {}
        // Reset to nullptr so concurrent guards (KqueueArmGuard)
        // detect the closed state and skip re-arming.
        c->csock.reset();
    }

    // ------------------------------------------------------------
@@ -138,10 +141,21 @@ namespace netplus {
    };

    // RAII Rearm guard - handles event re-enabling for EV_ONESHOT
    // Modelled after EpollArmGuard: always re-arms READ on scope exit
    // and auto-detects whether WRITE is needed by inspecting the
    // connection state (SendData, hasPendingWrite, hasBufferedData).
    // Call disarm() to suppress re-arming (e.g. before CloseEventHandler).
    //
    // CRITICAL: Arms only ONE filter at a time — either WRITE (when
    // data is pending) or READ (idle / waiting for next request).
    //
    // Unlike epoll (which combines EPOLLIN|EPOLLOUT into one event
    // delivered to one thread), kqueue fires EVFILT_READ and
    // EVFILT_WRITE as independent events that can be delivered to
    // DIFFERENT threads.  If both filters are armed simultaneously,
    // two threads may process the same fd concurrently.  When one
    // thread closes the connection while the other's guard still
    // tries to re-arm, the stale change corrupts the batch
    // (kevent returns -1, dropping all later changes).
    //
    // Single-filter approach prevents this race entirely and
    // matches epoll's one-event-per-arm semantic.
    class KqueueArmGuard {
    public:
        KqueueArmGuard(int fd, std::shared_ptr<con> c, KeventBatch* batch)
@@ -149,16 +163,25 @@ namespace netplus {

        ~KqueueArmGuard() {
            if (!_armed) return;
            if (!_batch || !_con || !_con->csock) return;
            if (!_batch || !_con) return;
            try {
                if (!_con->csock) return;

            // Always re-arm READ (matches EpollArmGuard EPOLLIN)
            _batch->add_change(_fd, EVFILT_READ, EV_ADD | EV_ONESHOT);
                bool needWrite = _con->SendData.pos < _con->SendData.size()
                              || _con->csock->hasPendingWrite();

            // Auto-detect if WRITE is needed
            if (_con->SendData.pos < _con->SendData.size()
                || _con->csock->hasPendingWrite()
                || _con->csock->hasBufferedData()) {
                if (needWrite) {
                    // Data pending → arm WRITE only.
                    // When WRITE fires, Step 4 sends the data.
                    // Step 3 also checks hasBufferedData() so any
                    // buffered TLS records are processed too.
                    _batch->add_change(_fd, EVFILT_WRITE, EV_ADD | EV_ONESHOT);
                } else {
                    // Idle → arm READ for next request / data.
                    _batch->add_change(_fd, EVFILT_READ, EV_ADD | EV_ONESHOT);
                }
            } catch (...) {
                // Connection was closed concurrently — skip re-arm
            }
        }