Skip to content

Commit

Permalink
Fix indefinite wait when trying to stop pjmedia_clock (#3304)
Browse files Browse the repository at this point in the history
  • Loading branch information
sauwming committed Dec 8, 2022
1 parent eca0ae2 commit 950081c
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 4 deletions.
8 changes: 6 additions & 2 deletions pjmedia/include/pjmedia/clock.h
Original file line number Diff line number Diff line change
Expand Up @@ -272,11 +272,15 @@ PJ_DECL(pj_status_t) pjmedia_clock_start(pjmedia_clock *clock);


/**
* Stop the clock.
* Stop the clock. When the function returns PJ_SUCCESS, it is guaranteed
* that the clock thread (if any) has stopped and the callback has completed.
* But if the function is called from within the clock's callback itself,
* the callback will still be running until completion. In that case,
* the function will only stop future callbacks and returns PJ_EBUSY.
*
* @param clock The media clock.
*
* @return PJ_SUCCES on success.
* @return PJ_SUCCES on success, or PJ_EBUSY.
*/
PJ_DECL(pj_status_t) pjmedia_clock_stop(pjmedia_clock *clock);

Expand Down
18 changes: 16 additions & 2 deletions pjmedia/src/pjmedia/clock_thread.c
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,15 @@ PJ_DEF(pj_status_t) pjmedia_clock_start(pjmedia_clock *clock)
clock->running = PJ_TRUE;
clock->quitting = PJ_FALSE;

if ((clock->options & PJMEDIA_CLOCK_NO_ASYNC) == 0 && !clock->thread) {
if ((clock->options & PJMEDIA_CLOCK_NO_ASYNC) == 0) {
if (clock->thread) {
/* This is probably the leftover thread that failed to
* be cleaned up during the last clock stoppage.
*/
pj_thread_destroy(clock->thread);
clock->thread = NULL;
}

status = pj_thread_create(clock->pool, "clock", &clock_thread, clock,
0, 0, &clock->thread);
if (status != PJ_SUCCESS) {
Expand Down Expand Up @@ -247,7 +255,13 @@ PJ_DEF(pj_status_t) pjmedia_clock_stop(pjmedia_clock *clock)
clock->thread = NULL;
pj_pool_reset(clock->pool);
} else {
clock->quitting = PJ_FALSE;
/* We are probably called from the clock thread itself.
* Do not cancel the thread's quitting though, since it
* may cause the clock thread to run indefinitely.
*/
// clock->quitting = PJ_FALSE;

return PJ_EBUSY;
}
}

Expand Down

0 comments on commit 950081c

Please sign in to comment.