pcm: Fix shm initialization race-condition
PATCH 1/1 pcm: Fix shm initialization race-condition
Easily seen when two threads try at the same time, one of them will fail.
The bug was identified by using apulse with Skype.
--- src/pcm/pcm_direct.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-)
src/pcm/pcm_direct.c +3 -2
@@ -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 ) {
Signature · 1 line
2.9.2
Quoted 7 lines · Ismael Luceno
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")Signed-off-by Ismael Luceno <[email protected]>
<...>
Oops, subject should be: [PATCH v2]...
Quoted 8 lines · Ismael Luceno
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")Signed-off-by Ismael Luceno <[email protected]>
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
Quoted 26 lines
--- src/pcm/pcm_direct.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-)src/pcm/pcm_direct.c +3
-2@@ -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 ) {Signature · 1 line
2.9.2