Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUG] srt_rendezvous hangs after calling srt_close in different thread #2029

Open
alexpokotilo opened this issue Jun 1, 2021 · 2 comments · May be fixed by #2032
Open

[BUG] srt_rendezvous hangs after calling srt_close in different thread #2029

alexpokotilo opened this issue Jun 1, 2021 · 2 comments · May be fixed by #2032
Labels
[core] Area: Changes in SRT library core Type: Bug Indicates an unexpected problem or unintended behavior
Milestone

Comments

@alexpokotilo
Copy link
Contributor

alexpokotilo commented Jun 1, 2021

Describe the bug
Problem reproduced only for SRT socket working in blocking mode. When srt_rendezvous call is blocked trying to connect to the peer and srt_close is called for the socket in parallel thread then both srt_rendezvous and srt_close calls hung till SRTO_CONNTIMEO.

This problem is introduced by this commit 03dafd8 and particularly by removing this line:
03dafd8#diff-70625c5a9e97bc3670abd5bfa05dd3cb0f847b9c6a81618cde939d6f104b0a86L1901

To Reproduce

#include <srt.h>

#include <thread>
#include <chrono>

TEST(SRT, SyncRendezvousHangs) {
    ASSERT_EQ(srt_startup(), 0);

    int yes = 1;

    SRTSOCKET m_bindsock = srt_socket(AF_INET, SOCK_DGRAM, 0);
    ASSERT_NE(m_bindsock, SRT_ERROR);

    ASSERT_NE(srt_setsockopt(m_bindsock, 0, SRTO_TSBPDMODE, &yes, sizeof yes), SRT_ERROR);
    ASSERT_NE(srt_setsockflag(m_bindsock, SRTO_SENDER, &yes, sizeof yes), SRT_ERROR);
    ASSERT_EQ(srt_setsockopt(m_bindsock, 0, SRTO_RENDEZVOUS, &yes, sizeof yes), 0);

    const int connection_timeout_ms = 500; // rendezvous timeout is x10 hence 5seconds
    ASSERT_EQ(srt_setsockopt(m_bindsock, 0, SRTO_CONNTIMEO, &connection_timeout_ms, sizeof connection_timeout_ms), 0);

    sockaddr_in local_sa={};
    local_sa.sin_family = AF_INET;
    local_sa.sin_port = htons(9999);
    local_sa.sin_addr.s_addr = INADDR_ANY;

    sockaddr_in peer_sa= {};
    peer_sa.sin_family = AF_INET;
    peer_sa.sin_port = htons(9998);
    ASSERT_EQ(inet_pton(AF_INET, "127.0.0.1", &peer_sa.sin_addr), 1);

    uint64_t duration = 0;

    std::thread close_thread([&m_bindsock, &duration] {
        std::this_thread::sleep_for(std::chrono::seconds(1)); // wait till srt_rendezvous is called
        auto start = std::chrono::steady_clock::now();
        srt_close(m_bindsock);
        auto end = std::chrono::steady_clock::now();

        duration = std::chrono::duration_cast<std::chrono::seconds>(end - start).count();
    });

    ASSERT_EQ(srt_rendezvous(m_bindsock, (sockaddr*)&local_sa, sizeof local_sa,
              (sockaddr*)&peer_sa, sizeof peer_sa), SRT_ERROR);

    close_thread.join();
    ASSERT_LE(duration, 1);
    srt_close(m_bindsock);
    srt_cleanup();
}

Expected behavior
srt_close should return right after call.
srt_rendezvous should return immediately after srt_close is called for the socket in parallel thread.

Desktop (please provide the following information):

  • OS: all OSes including Android/IOS where it was found
  • SRT Version / commit ID: 1.4.3/03dafd8981e680fec8ddb6437144227ca39284ff
@alexpokotilo alexpokotilo added the Type: Bug Indicates an unexpected problem or unintended behavior label Jun 1, 2021
@maxsharabayko maxsharabayko added the [core] Area: Changes in SRT library core label Jun 1, 2021
@maxsharabayko maxsharabayko added this to the v1.4.4 milestone Jun 1, 2021
@ethouris
Copy link
Collaborator

ethouris commented Jun 1, 2021

Could you please check with the fix in #2032? I tested it in the environment with other fixes related to "thread insanity" problems, but I made the fix as little invasive as possible.

@alexpokotilo
Copy link
Contributor Author

@ethouris,
we've checked on several OSes and fix works fine. Thank you!

@alexpokotilo alexpokotilo changed the title [BUG] srt_rendezvous hungs after calling srt_close in different thread [BUG] srt_rendezvous hangs after calling srt_close in different thread Jun 2, 2021
@maxsharabayko maxsharabayko modified the milestones: v1.4.4, v1.4.5 Aug 18, 2021
@maxsharabayko maxsharabayko modified the milestones: v1.5.0, Backlog May 17, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[core] Area: Changes in SRT library core Type: Bug Indicates an unexpected problem or unintended behavior
Projects
None yet
3 participants