Skip to content

[BUG] srt_accept() in blocking mode always prints an error when the socket is closed. #3318

Description

@fchasse-rd

Describe the bug
I have a thread that continuously accepts connections from a listener socket. The socket is configured in blocking mode, hence the only way to exit an srt_accept call is to close the socket. This results in this log always appearing when I shutdown my app:

17:38:38.926767/eveSrtTests*E:SRT.cn: srt_accept: listener socket @899642338 is already closed

To Reproduce

#include <chrono>
#include <future>
#include <iostream>
#include <thread>

#include <catch2/catch_test_macros.hpp>
#include <srt/srt.h>

TEST_CASE("srt_accept error")
{
    REQUIRE(srt_startup() >= 0);

    auto listener = srt_create_socket();
    REQUIRE(listener != SRT_INVALID_SOCK);

    sockaddr_in bindTarget;
    bindTarget.sin_family      = AF_INET;
    bindTarget.sin_addr.s_addr = inet_addr("127.0.0.1");
    bindTarget.sin_port        = htons(0);

    REQUIRE(srt_bind(listener, (sockaddr*)&bindTarget, sizeof(bindTarget)) == 0);
    REQUIRE(srt_listen(listener, 1) == 0);

    std::future<int> acceptFuture = std::async([listener] {
        sockaddr_in sa;
        int         saLen = sizeof(sa);
        std::ignore       = srt_accept(listener, (sockaddr*)&sa, &saLen);
        return srt_getlasterror(nullptr);
    });

    // Wait a bit to ensure srt_accept() is waiting on the accept.
    std::this_thread::sleep_for(std::chrono::milliseconds(1'000));

    REQUIRE(srt_close(listener) == 0);
    int acceptError = acceptFuture.get();
    CHECK((acceptError == SRT_EINVSOCK || acceptError == SRT_ESCLOSED));

    std::ignore = srt_cleanup();
}

Expected behavior
As this is the intended use, I would not expect an error log to appear.

Suggestion
Could the log level be reduced to info (or removed since we already have the throw) if we're in blocking mode?

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type: BugIndicates an unexpected problem or unintended behavior

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions