Skip to content

Commit 23ece94

Browse files
committed
Fix #1206, dispose ingester while server quiting. 3.0.111
1 parent a6f8880 commit 23ece94

9 files changed

+77
-12
lines changed

README.md

+2
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,7 @@ For previous versions, please read:
146146

147147
## V3 changes
148148

149+
* v3.0, 2020-01-29, Fix [#1206][bug #1206], dispose ingester while server quiting. 3.0.111
149150
* v3.0, 2020-01-28, Fix [#1230][bug #1230], racing condition in source fetch or create. 3.0.110
150151
* v3.0, 2020-01-27, Fix [#1303][bug #1303], do not dispatch previous meta when not publishing. 3.0.109
151152
* v3.0, 2020-01-26, Allow use libst.so for ST is MPL license.
@@ -1632,6 +1633,7 @@ Winlin
16321633
[bug #607]: https://github.com/ossrs/srs/issues/607
16331634
[bug #1303]: https://github.com/ossrs/srs/issues/1303
16341635
[bug #1230]: https://github.com/ossrs/srs/issues/1230
1636+
[bug #1206]: https://github.com/ossrs/srs/issues/1206
16351637
[bug #xxxxxxxxxxxxx]: https://github.com/ossrs/srs/issues/xxxxxxxxxxxxx
16361638

16371639
[exo #828]: https://github.com/google/ExoPlayer/pull/828

trunk/src/app/srs_app_ffmpeg.cpp

+5
Original file line numberDiff line numberDiff line change
@@ -418,6 +418,11 @@ void SrsFFMPEG::fast_stop()
418418
process->fast_stop();
419419
}
420420

421+
void SrsFFMPEG::fast_kill()
422+
{
423+
process->fast_kill();
424+
}
425+
421426
#endif
422427

423428

trunk/src/app/srs_app_ffmpeg.hpp

+1
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ class SrsFFMPEG
8383
virtual void stop();
8484
public:
8585
virtual void fast_stop();
86+
virtual void fast_kill();
8687
};
8788

8889
#endif

trunk/src/app/srs_app_ingest.cpp

+29-3
Original file line numberDiff line numberDiff line change
@@ -97,11 +97,17 @@ void SrsIngesterFFMPEG::fast_stop()
9797
ffmpeg->fast_stop();
9898
}
9999

100+
void SrsIngesterFFMPEG::fast_kill()
101+
{
102+
ffmpeg->fast_kill();
103+
}
104+
100105
SrsIngester::SrsIngester()
101106
{
102107
_srs_config->subscribe(this);
103108

104109
expired = false;
110+
disposed = false;
105111

106112
trd = new SrsDummyCoroutine();
107113
pprint = SrsPithyPrint::create_ingester();
@@ -117,11 +123,18 @@ SrsIngester::~SrsIngester()
117123

118124
void SrsIngester::dispose()
119125
{
126+
if (disposed) {
127+
return;
128+
}
129+
disposed = true;
130+
120131
// first, use fast stop to notice all FFMPEG to quit gracefully.
121132
fast_stop();
133+
134+
srs_usleep(100 * SRS_UTIME_MILLISECONDS);
122135

123-
// then, use stop to wait FFMPEG quit one by one and send SIGKILL if needed.
124-
stop();
136+
// then, use fast kill to ensure FFMPEG quit.
137+
fast_kill();
125138
}
126139

127140
srs_error_t SrsIngester::start()
@@ -166,6 +179,19 @@ void SrsIngester::fast_stop()
166179
}
167180
}
168181

182+
void SrsIngester::fast_kill()
183+
{
184+
std::vector<SrsIngesterFFMPEG*>::iterator it;
185+
for (it = ingesters.begin(); it != ingesters.end(); ++it) {
186+
SrsIngesterFFMPEG* ingester = *it;
187+
ingester->fast_kill();
188+
}
189+
190+
if (!ingesters.empty()) {
191+
srs_trace("fast kill all ingesters ok.");
192+
}
193+
}
194+
169195
// when error, ingester sleep for a while and retry.
170196
// ingest never sleep a long time, for we must start the stream ASAP.
171197
#define SRS_AUTO_INGESTER_CIMS (3 * SRS_UTIME_SECONDS)
@@ -174,7 +200,7 @@ srs_error_t SrsIngester::cycle()
174200
{
175201
srs_error_t err = srs_success;
176202

177-
while (true) {
203+
while (!disposed) {
178204
if ((err = do_cycle()) != srs_success) {
179205
srs_warn("Ingester: Ignore error, %s", srs_error_desc(err).c_str());
180206
srs_freep(err);

trunk/src/app/srs_app_ingest.hpp

+6
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ class SrsIngesterFFMPEG
6060
virtual srs_error_t cycle();
6161
// @see SrsFFMPEG.fast_stop().
6262
virtual void fast_stop();
63+
virtual void fast_kill();
6364
};
6465

6566
// Ingest file/stream/device,
@@ -75,6 +76,8 @@ class SrsIngester : public ISrsCoroutineHandler, public ISrsReloadHandler
7576
// Whether the ingesters are expired, for example, the listen port changed,
7677
// all ingesters must be restart.
7778
bool expired;
79+
// Whether already disposed.
80+
bool disposed;
7881
public:
7982
SrsIngester();
8083
virtual ~SrsIngester();
@@ -84,7 +87,10 @@ class SrsIngester : public ISrsCoroutineHandler, public ISrsReloadHandler
8487
virtual srs_error_t start();
8588
virtual void stop();
8689
private:
90+
// Notify FFMPEG to fast stop.
8791
virtual void fast_stop();
92+
// When SRS quit, directly kill FFMPEG after fast stop.
93+
virtual void fast_kill();
8894
// Interface ISrsReusableThreadHandler.
8995
public:
9096
virtual srs_error_t cycle();

trunk/src/app/srs_app_process.cpp

+25
Original file line numberDiff line numberDiff line change
@@ -327,3 +327,28 @@ void SrsProcess::fast_stop()
327327
return;
328328
}
329329

330+
void SrsProcess::fast_kill()
331+
{
332+
int ret = ERROR_SUCCESS;
333+
334+
if (!is_started) {
335+
return;
336+
}
337+
338+
if (pid <= 0) {
339+
return;
340+
}
341+
342+
if (kill(pid, SIGKILL) < 0) {
343+
ret = ERROR_SYSTEM_KILL;
344+
srs_warn("ignore fast kill process failed, pid=%d. ret=%d", pid, ret);
345+
return;
346+
}
347+
348+
// Try to wait pid to avoid zombie FFMEPG.
349+
int status = 0;
350+
waitpid(pid, &status, WNOHANG);
351+
352+
return;
353+
}
354+

trunk/src/app/srs_app_process.hpp

+2
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,8 @@ class SrsProcess
9191
// when use stop without fast_stop, we spend maybe [0, SRS_PROCESS_QUIT_TIMEOUT_MS * N]
9292
// but use fast_stop then stop, the time is almost [0, SRS_PROCESS_QUIT_TIMEOUT_MS].
9393
virtual void fast_stop();
94+
// Directly kill process, never use it except server quiting.
95+
virtual void fast_kill();
9496
};
9597

9698
#endif

trunk/src/app/srs_app_server.cpp

+6-8
Original file line numberDiff line numberDiff line change
@@ -520,7 +520,8 @@ void SrsServer::dispose()
520520
close_listeners(SrsListenerRtsp);
521521
close_listeners(SrsListenerFlv);
522522

523-
// @remark don't dispose ingesters, for too slow.
523+
// Fast stop to notify FFMPEG to quit, wait for a while then fast kill.
524+
ingester->dispose();
524525

525526
// dispose the source for hls and dvr.
526527
_srs_sources->dispose();
@@ -856,17 +857,14 @@ void SrsServer::on_signal(int signo)
856857
srs_trace("gmc is on, main cycle will terminate normally.");
857858
signal_gmc_stop = true;
858859
#else
859-
srs_trace("user terminate program");
860-
#ifdef SRS_AUTO_MEM_WATCH
860+
#ifdef SRS_AUTO_MEM_WATCH
861861
srs_memory_report();
862+
#endif
862863
#endif
863-
exit(0);
864-
#endif
865-
return;
866864
}
867865

868-
if (signo == SRS_SIGNAL_GRACEFULLY_QUIT && !signal_gracefully_quit) {
869-
srs_trace("user terminate program, gracefully quit.");
866+
if ((signo == SIGINT || signo == SRS_SIGNAL_GRACEFULLY_QUIT) && !signal_gracefully_quit) {
867+
srs_trace("sig=%d, user terminate program, gracefully quit", signo);
870868
signal_gracefully_quit = true;
871869
return;
872870
}

trunk/src/core/srs_core.hpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
// The version config.
2828
#define VERSION_MAJOR 3
2929
#define VERSION_MINOR 0
30-
#define VERSION_REVISION 110
30+
#define VERSION_REVISION 111
3131

3232
// The macros generated by configure script.
3333
#include <srs_auto_headers.hpp>

0 commit comments

Comments
 (0)