Skip to content

Commit

Permalink
fix #374: when terminate srs, cleanup to ensure FFMPEG quit.
Browse files Browse the repository at this point in the history
  • Loading branch information
winlinvip committed May 30, 2015
1 parent db57a51 commit 567d84e
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 11 deletions.
4 changes: 4 additions & 0 deletions trunk/src/app/srs_app_ffmpeg.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -403,6 +403,10 @@ int SrsFFMPEG::start()

// child process: ffmpeg encoder engine.
if (pid == 0) {
// ignore the SIGINT and SIGTERM
signal(SIGINT, SIG_IGN);
signal(SIGTERM, SIG_IGN);

// redirect logs to file.
int log_fd = -1;
int flags = O_CREAT|O_WRONLY|O_APPEND;
Expand Down
5 changes: 5 additions & 0 deletions trunk/src/app/srs_app_ingest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,11 @@ int SrsIngester::parse_engines(SrsConfDirective* vhost, SrsConfDirective* ingest
return ret;
}

void SrsIngester::dispose()
{
stop();
}

void SrsIngester::stop()
{
pthread->stop();
Expand Down
2 changes: 2 additions & 0 deletions trunk/src/app/srs_app_ingest.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@ class SrsIngester : public ISrsReusableThreadHandler, public ISrsReloadHandler
public:
SrsIngester();
virtual ~SrsIngester();
public:
virtual void dispose();
public:
virtual int start();
virtual void stop();
Expand Down
41 changes: 31 additions & 10 deletions trunk/src/app/srs_app_server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -480,6 +480,7 @@ SrsServer::SrsServer()
{
signal_reload = false;
signal_gmc_stop = false;
signal_gracefully_quit = false;
pid_fd = -1;

signal_manager = NULL;
Expand Down Expand Up @@ -519,7 +520,7 @@ void SrsServer::destroy()
close_listeners(SrsListenerHttpStream);

#ifdef SRS_AUTO_INGEST
ingester->stop();
ingester->dispose();
#endif

#ifdef SRS_AUTO_HTTP_API
Expand Down Expand Up @@ -555,6 +556,18 @@ void SrsServer::destroy()
// and segment fault.
}

void SrsServer::dispose()
{
_srs_config->unsubscribe(this);

#ifdef SRS_AUTO_INGEST
ingester->dispose();
srs_trace("gracefully cleanup ingesters");
#endif

srs_trace("terminate server");
}

int SrsServer::initialize(ISrsServerCycle* cycle_handler)
{
int ret = ERROR_SUCCESS;
Expand Down Expand Up @@ -831,6 +844,7 @@ int SrsServer::cycle()
srs_warn("system quit");
#else
srs_warn("main cycle terminated, system quit normally.");
dispose();
exit(0);
#endif

Expand Down Expand Up @@ -877,9 +891,9 @@ void SrsServer::on_signal(int signo)
return;
}

if (signo == SIGTERM) {
srs_trace("user terminate program");
exit(0);
if (signo == SIGTERM && !signal_gracefully_quit) {
srs_trace("user terminate program, gracefully quit.");
signal_gracefully_quit = true;
return;
}
}
Expand All @@ -903,7 +917,7 @@ int SrsServer::do_cycle()

// the deamon thread, update the time cache
while (true) {
if(handler && (ret = handler->on_cycle(conns.size())) != ERROR_SUCCESS){
if(handler && (ret = handler->on_cycle((int)conns.size())) != ERROR_SUCCESS){
srs_error("cycle handle failed. ret=%d", ret);
return ret;
}
Expand All @@ -917,19 +931,26 @@ int SrsServer::do_cycle()

for (int i = 0; i < temp_max; i++) {
st_usleep(SRS_SYS_CYCLE_INTERVAL * 1000);

// gracefully quit for SIGINT or SIGTERM.
if (signal_gracefully_quit) {
srs_trace("cleanup for gracefully terminate.");
return ret;
}

// for gperf heap checker,
// @see: research/gperftools/heap-checker/heap_checker.cc
// if user interrupt the program, exit to check mem leak.
// but, if gperf, use reload to ensure main return normally,
// because directly exit will cause core-dump.
// for gperf heap checker,
// @see: research/gperftools/heap-checker/heap_checker.cc
// if user interrupt the program, exit to check mem leak.
// but, if gperf, use reload to ensure main return normally,
// because directly exit will cause core-dump.
#ifdef SRS_AUTO_GPERF_MC
if (signal_gmc_stop) {
srs_warn("gmc got singal to stop server.");
return ret;
}
#endif

// do reload the config.
if (signal_reload) {
signal_reload = false;
srs_info("get signal reload, to reload the config.");
Expand Down
8 changes: 7 additions & 1 deletion trunk/src/app/srs_app_server.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -275,16 +275,22 @@ class SrsServer : virtual public ISrsReloadHandler
*/
bool signal_reload;
bool signal_gmc_stop;
bool signal_gracefully_quit;
public:
SrsServer();
virtual ~SrsServer();
public:
private:
/**
* the destroy is for gmc to analysis the memory leak,
* if not destroy global/static data, the gmc will warning memory leak.
* in service, server never destroy, directly exit when restart.
*/
virtual void destroy();
/**
* when SIGTERM, SRS should do cleanup, for example,
* to stop all ingesters, cleanup HLS and dvr.
*/
virtual void dispose();
// server startup workflow, @see run_master()
public:
virtual int initialize(ISrsServerCycle* cycle_handler);
Expand Down

0 comments on commit 567d84e

Please sign in to comment.