Skip to content

Commit

Permalink
[apps] Fixed hangup when exitting on interrupt (#1787)
Browse files Browse the repository at this point in the history
  • Loading branch information
ethouris authored Feb 4, 2021
1 parent 5ec84d2 commit 44503cd
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 9 deletions.
16 changes: 8 additions & 8 deletions testing/srt-test-live.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -101,22 +101,19 @@ struct AlarmExit: public std::runtime_error
}
};

volatile bool int_state = false;
volatile bool timer_state = false;
void OnINT_ForceExit(int)
{
cerr << "\n-------- REQUESTED INTERRUPT!\n";
int_state = true;
if ( transmit_throw_on_interrupt )
throw ForcedExit("Requested exception interrupt");
transmit_int_state = true;
}

std::string g_interrupt_reason;

void OnAlarm_Interrupt(int)
{
cerr << "\n---------- INTERRUPT ON TIMEOUT: hang on " << g_interrupt_reason << "!\n";
int_state = false; // JIC
transmit_int_state = true; // JIC
timer_state = true;
throw AlarmExit("Watchdog bites hangup");
}
Expand Down Expand Up @@ -419,13 +416,16 @@ int main( int argc, char** argv )
if ( !SysInitializeNetwork() )
throw std::runtime_error("Can't initialize network!");

srt_startup();

// Symmetrically, this does a cleanup; put into a local destructor to ensure that
// it's called regardless of how this function returns.
struct NetworkCleanup
{
~NetworkCleanup()
{
SysCleanupNetwork();
srt_cleanup();
}
} cleanupobj;

Expand Down Expand Up @@ -835,7 +835,7 @@ int main( int argc, char** argv )
}
catch(std::exception& x)
{
if (::int_state)
if (::transmit_int_state)
{
// The application was terminated by SIGINT or SIGTERM.
// Don't print anything, just exit gently like ffmpeg.
Expand Down Expand Up @@ -939,7 +939,7 @@ int main( int argc, char** argv )

Verb() << "sent";

if ( int_state )
if (::transmit_int_state)
{
Verror() << "\n (interrupted on request)";
break;
Expand Down Expand Up @@ -985,7 +985,7 @@ int main( int argc, char** argv )
{
Verror() << "Exit on timeout.";
}
else if (::int_state)
else if (::transmit_int_state)
{
Verror() << "Exit on interrupt.";
// Do nothing.
Expand Down
25 changes: 24 additions & 1 deletion testing/testmedia.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ using srt_logging::MemberStatusStr;
#endif

volatile bool transmit_throw_on_interrupt = false;
volatile bool transmit_int_state = false;
int transmit_bw_report = 0;
unsigned transmit_stats_report = 0;
size_t transmit_chunk_size = SRT_LIVE_DEF_PLSIZE;
Expand Down Expand Up @@ -530,8 +531,15 @@ void SrtCommon::AcceptNewClient()

int len = 2;
SRTSOCKET ready[2];
if (srt_epoll_wait(srt_conn_epoll, 0, 0, ready, &len, -1, 0, 0, 0, 0) == -1)
while (srt_epoll_wait(srt_conn_epoll, 0, 0, ready, &len, 1000, 0, 0, 0, 0) == -1)
{
if (::transmit_int_state)
Error("srt_epoll_wait for srt_accept: interrupt");

if (srt_getlasterror(NULL) == SRT_ETIMEOUT)
continue;
Error("srt_epoll_wait(srt_conn_epoll)");
}

Verb() << "[EPOLL: " << len << " sockets] " << VerbNoEOL;
}
Expand Down Expand Up @@ -785,6 +793,11 @@ int SrtCommon::ConfigurePost(SRTSOCKET sock)

if (m_timeout)
result = srt_setsockopt(sock, 0, SRTO_RCVTIMEO, &m_timeout, sizeof m_timeout);
else
{
int timeout = 1000;
result = srt_setsockopt(sock, 0, SRTO_RCVTIMEO, &timeout, sizeof timeout);
}
if (result == -1)
return result;
}
Expand Down Expand Up @@ -2274,6 +2287,9 @@ MediaPacket SrtSource::Read(size_t chunk)
}
#endif

if (::transmit_int_state)
Error("srt_recvmsg2: interrupted");

::transmit_throw_on_interrupt = true;
stat = srt_recvmsg2(m_sock, data.data(), chunk, &mctrl);
::transmit_throw_on_interrupt = false;
Expand Down Expand Up @@ -2324,6 +2340,13 @@ MediaPacket SrtSource::Read(size_t chunk)
// If was -1, then passthru.
}
}
else
{
// In blocking mode it uses a minimum of 1s timeout,
// and continues only if interrupt not requested.
if (srt_getlasterror(NULL) == SRT_EASYNCRCV)
continue;
}
Error("srt_recvmsg2");
}

Expand Down
1 change: 1 addition & 0 deletions testing/testmedia.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

extern srt_listen_callback_fn* transmit_accept_hook_fn;
extern void* transmit_accept_hook_op;
extern volatile bool transmit_int_state;

extern std::shared_ptr<SrtStatsWriter> transmit_stats_writer;

Expand Down

0 comments on commit 44503cd

Please sign in to comment.