mailing list of musl libc
 help / color / mirror / code / Atom feed
From: Rich Felker <dalias@libc.org>
To: Alexey Izbyshev <izbyshev@ispras.ru>
Cc: musl@lists.openwall.com
Subject: Re: [musl] [PATCH] fix thread leak on timer_create(SIGEV_THREAD) failure
Date: Mon, 12 Sep 2022 10:22:31 -0400	[thread overview]
Message-ID: <20220912142231.GJ9709@brightrain.aerifal.cx> (raw)
In-Reply-To: <20220908091856.194976-1-izbyshev@ispras.ru>

On Thu, Sep 08, 2022 at 12:18:56PM +0300, Alexey Izbyshev wrote:
> After commit 5b74eed3b301e2227385f3bf26d3bb7c2d822cf8 the timer thread
> doesn't check whether timer_create() actually created the timer,
> proceeding to wait for a signal that might never arrive.  We can't fix
> this by simply checking for a negative timer_id after
> pthread_barrier_wait() because we have no way to distinguish a timer
> creation failure and a request to delete a timer with INT_MAX id if it
> happens to arrive quickly (a variation of this bug existed before
> 5b74eed3b301e2227385f3bf26d3bb7c2d822cf8, where the timer would be
> leaked in this case).  So (ab)use cancel field of pthread_t instead.
> ---
>  src/time/timer_create.c | 6 +++++-
>  1 file changed, 5 insertions(+), 1 deletion(-)
> 
> diff --git a/src/time/timer_create.c b/src/time/timer_create.c
> index 4bef2390..cd32c945 100644
> --- a/src/time/timer_create.c
> +++ b/src/time/timer_create.c
> @@ -43,6 +43,8 @@ static void *start(void *arg)
>  	union sigval val = args->sev->sigev_value;
>  
>  	pthread_barrier_wait(&args->b);
> +	if (self->cancel)
> +		return 0;
>  	for (;;) {
>  		siginfo_t si;
>  		while (sigwaitinfo(SIGTIMER_SET, &si) < 0);
> @@ -113,8 +115,10 @@ int timer_create(clockid_t clk, struct sigevent *restrict evp, timer_t *restrict
>  		ksev.sigev_signo = SIGTIMER;
>  		ksev.sigev_notify = SIGEV_THREAD_ID;
>  		ksev.sigev_tid = td->tid;
> -		if (syscall(SYS_timer_create, clk, &ksev, &timerid) < 0)
> +		if (syscall(SYS_timer_create, clk, &ksev, &timerid) < 0) {
>  			timerid = -1;
> +			td->cancel = 1;
> +		}
>  		td->timer_id = timerid;
>  		pthread_barrier_wait(&args.b);
>  		if (timerid < 0) return -1;
> -- 
> 2.37.2

I'm not really happy with overloading td->cancel like this, but it's
probably the best fix at present. The long-term direction for this
functionality is hopefully removing the use of kernel timer objects
entirely for SIGEV_THREAD timers and instead implementing them with
clock_nanosleep, which would eliminate the possibility of
SYS_timer_create failure, the possibility of leak, the td->timer_id
member, the SIGTIMER reserved signal, and basically all the hacks
going on here.

Rich

  reply	other threads:[~2022-09-12 14:22 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-09-08  9:18 Alexey Izbyshev
2022-09-12 14:22 ` Rich Felker [this message]
2022-09-12 14:57   ` Alexey Izbyshev

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20220912142231.GJ9709@brightrain.aerifal.cx \
    --to=dalias@libc.org \
    --cc=izbyshev@ispras.ru \
    --cc=musl@lists.openwall.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/musl/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).