From: Takashi Iwai Subject: Re: [PATCH] pcm: Fix shm initialization race-condition Date: Mon, 22 Aug 2016 18:11:59 +0200 Message-ID: References: <20160822160433.27598-1-ismael@iodev.co.uk> Mime-Version: 1.0 (generated by SEMI 1.14.6 - "Maruoka") Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Return-path: Received: from mx2.suse.de (mx2.suse.de [195.135.220.15]) by alsa0.perex.cz (Postfix) with ESMTP id 64FBB266A7A for ; Mon, 22 Aug 2016 18:12:00 +0200 (CEST) In-Reply-To: <20160822160433.27598-1-ismael@iodev.co.uk> List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: alsa-devel-bounces@alsa-project.org Sender: alsa-devel-bounces@alsa-project.org To: Ismael Luceno Cc: alsa-devel@alsa-project.org List-Id: alsa-devel@alsa-project.org On Mon, 22 Aug 2016 18:04:33 +0200, Ismael Luceno wrote: > > Easily seen when two threads try at the same time, one of them will fail. > > The bug was identified by using apulse with Skype. > > Fixes: dec428c35221 ("pcm: fix 'unable to create IPC shm instance' caused by fork from a thread") > Fixes: https://github.com/i-rinat/apulse/issues/38 > Signed-off-by: Ismael Luceno OK, I'll apply this one, as this change alone is fine. But in general, this implies that the application hitting this is buggy. You can't reliably call snd_pcm_open() for the same PCM stream concurrently via multi threads. You have to introduce the proper protection in the caller side. thanks, Takashi > --- > src/pcm/pcm_direct.c | 5 +++-- > 1 file changed, 3 insertions(+), 2 deletions(-) > > diff --git a/src/pcm/pcm_direct.c b/src/pcm/pcm_direct.c > index c3925cc20fd3..643498375b34 100644 > --- a/src/pcm/pcm_direct.c > +++ b/src/pcm/pcm_direct.c > @@ -96,11 +96,12 @@ int snd_pcm_direct_shm_create_or_connect(snd_pcm_direct_t *dmix) > retryget: > dmix->shmid = shmget(dmix->ipc_key, sizeof(snd_pcm_direct_share_t), > dmix->ipc_perm); > - if (dmix->shmid < 0) { > - if (errno == ENOENT) > + if (dmix->shmid < 0 && errno == ENOENT) { > if ((dmix->shmid = shmget(dmix->ipc_key, sizeof(snd_pcm_direct_share_t), > IPC_CREAT | IPC_EXCL | dmix->ipc_perm)) != -1) > first_instance = 1; > + else if (errno == EEXIST) > + goto retryget; > } > err = -errno; > if (dmix->shmid < 0) { > -- > 2.9.2 >