Avoid a potential race when shutting down

tus_file_shutdown() deletes all files.

This could interfere with the exp thread if it expired a file at the
same time.

Solve this race by first stopping the exp thread, then deleting all files
and then running final checks that exp is actually cleaned up.
parent bdba2965
......@@ -202,19 +202,29 @@ tus_file_exp_new(void)
return (e);
}
/* stop the expiry thread */
void
tus_file_exp_stop(struct tus_exp *e)
{
CHECK_OBJ_NOTNULL(e, TUS_EXP_MAGIC);
e->die = 1;
AZ(pthread_cond_signal(&e->cond));
AZ(pthread_join(e->thread, NULL));
e->thread = 0;
}
void
tus_file_exp_destroy(struct tus_exp **ep)
{
struct tus_exp *e;
TAKE_OBJ_NOTNULL(e, ep, TUS_EXP_MAGIC);
assert(e->die == 1);
assert(e->thread == 0);
AN(e->heap);
AZ(VBH_root(e->heap));
e->die = 1;
AZ(pthread_cond_signal(&e->cond));
AZ(pthread_join(e->thread, NULL));
AZ(pthread_cond_destroy(&e->cond));
AZ(pthread_mutex_destroy(&e->mtx));
......
......@@ -27,4 +27,5 @@
*/
struct tus_exp *tus_file_exp_new(void);
void tus_file_exp_stop(struct tus_exp *);
void tus_file_exp_destroy(struct tus_exp **);
......@@ -284,6 +284,7 @@ tus_server__fini(struct VPFX(tus_server) **tussrvp)
assert(tussrv->refcnt >= 1);
if (--tussrv->refcnt == 0) {
tus_file_exp_stop(tussrv->exp);
tus_file_shutdown(tussrv);
tus_file_exp_destroy(&tussrv->exp);
AZ(tussrv->exp);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment