Commit 2f40ef61 authored by Alberto Miranda's avatar Alberto Miranda ♨️
Browse files

Fix race condition testing/setting m_is_paused

Two new private member functions pause_accept() and resume_accept() have
been added to urd, which internally use the compare_exchange_weak()
method to atomically test and set m_is_paused.

Fixes #15.
parent 753b05f7
Loading
Loading
Loading
Loading
Loading
+22 −6
Original line number Diff line number Diff line
@@ -586,19 +586,20 @@ urd::command_handler(const request_ptr base_request) {
        case command_type::ping:
            break; // nothing special to do here
        case command_type::pause_accept:
            if(!m_is_paused) {
                m_is_paused = true;
            }
            pause_accept();
            break;
        case command_type::resume_accept:
            if(m_is_paused) {
                m_is_paused = false;
            }
            resume_accept();
            break;
        case command_type::shutdown:
        {
            // TODO
            LOGGER_WARN("Shutdown requested!");
            pause_accept();


            break;
        }
        case command_type::unknown:
            resp->set_error_code(urd_error::bad_args);
            break;
@@ -1027,6 +1028,21 @@ void urd::print_farewell() {
    LOGGER_INFO("{}", fsep);
}

void urd::pause_accept() {
    bool expected = false;
    while(!m_is_paused.compare_exchange_weak(expected, true) && !expected);

    LOGGER_WARN("Daemon locked: incoming requests will be rejected");
}

void urd::resume_accept() {
    bool expected = true;
    while(!m_is_paused.compare_exchange_weak(expected, false) && expected);

    LOGGER_WARN("Daemon unlocked: incoming requests will be processed");
}


int urd::run() {

    // initialize logging facilities
+3 −0
Original line number Diff line number Diff line
@@ -122,6 +122,9 @@ private:
                               bool track, const bfs::path& mount, 
                               uint32_t quota);

    void pause_accept();
    void resume_accept();

private:
    std::atomic<bool> m_is_paused;

+17 −0
Original line number Diff line number Diff line
@@ -68,6 +68,15 @@ SCENARIO("send control commands to urd", "[api::nornsctl_send_command]") {
                        REQUIRE(task.t_id == 1);
                    }
                }

                AND_WHEN("further NORNSCTL_COMMAND_PAUSE_ACCEPT commands are sent") {

                    rv = nornsctl_send_command(NORNSCTL_COMMAND_PAUSE_ACCEPT, NULL);

                    THEN("nornsctl_send_command() returns NORNS_SUCCESS") {
                        REQUIRE(rv == NORNS_SUCCESS);
                    }
                }
            }
        }

@@ -77,6 +86,14 @@ SCENARIO("send control commands to urd", "[api::nornsctl_send_command]") {

            THEN("nornsctl_send_command() returns NORNS_SUCCESS") {
                REQUIRE(rv == NORNS_SUCCESS);

                AND_WHEN("further NORNSCTL_COMMAND_RESUME_ACCEPT commands are sent") {
                    rv = nornsctl_send_command(NORNSCTL_COMMAND_RESUME_ACCEPT, NULL);

                    THEN("nornsctl_send_command() returns NORNS_SUCCESS") {
                        REQUIRE(rv == NORNS_SUCCESS);
                    }
                }
            }
        }