pcm: Fix shm initialization race-condition
A message that is not in the archive (sent off-list or not yet received): [email protected]
Quoted 17 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]>--- src/pcm/pcm_direct.c | 2 ++ 1 file changed, 2 insertions(+)src/pcm/pcm_direct.c +0
-0
src/pcm/pcm_direct.c retryget: · line 105
Quoted 5 lines
src/pcm/pcm_direct.c +2
-0@@ -101,6 +101,8 @@ retryget:if ((dmix->shmid =shmget (dmix->ipc_key,sizeof (snd_pcm_direct_share_t),IPC_CREAT |IPC_EXCL | dmix->ipc_perm)) !=-1 ) first_instance =1 ; +if (dmix->shmid <0 && errno ==EEXIST ) + goto retryget;
Hrm, but this would result in an endless loop if the shm was already taken persistently.
Also, which call does give a negative shmid, actually? It's from the first shmget() or the second shmget()?
(The existing code is ugly and we need to fix the indentation
there...)
thanks,
Takashi
Quoted 14 lines · Takashi Iwai
Quoted 12 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]>--- src/pcm/pcm_direct.c | 2 ++ 1 file changed, 2 insertions(+)
Quoted 13 lines
src/pcm/pcm_direct.c retryget: · line 105
Quoted 10 lines
src/pcm/pcm_direct.c +2
-0@@ -101,6 +101,8 @@ retryget:if ((dmix->shmid =shmget (dmix->ipc_key,sizeof (snd_pcm_direct_share_t),IPC_CREAT |IPC_EXCL | dmix->ipc_perm)) !=-1 ) first_instance =1 ; +if (dmix->shmid <0 && errno ==EEXIST ) + goto retryget;Hrm, but this would result in an endless loop if the shm was already taken persistently.
If so, shouldn't the first call to shmget succeed?
To me it seems very unlikely that both calls continuosly fail.
Quoted 2 lines
Also, which call does give a negative shmid, actually? It's from the first
shmget()or the secondshmget()?
What happens is that both threads go down that path but, of course, only one succeeds in the second shmget call.
Quoted 30 lines · Ismael Luceno
Quoted 14 lines · Takashi Iwai
Quoted 12 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]>--- src/pcm/pcm_direct.c | 2 ++ 1 file changed, 2 insertions(+)Quoted 13 lines
src/pcm/pcm_direct.c retryget: · line 105
Quoted 10 lines
src/pcm/pcm_direct.c +2
-0@@ -101,6 +101,8 @@ retryget:if ((dmix->shmid =shmget (dmix->ipc_key,sizeof (snd_pcm_direct_share_t),IPC_CREAT |IPC_EXCL | dmix->ipc_perm)) !=-1 ) first_instance =1 ; +if (dmix->shmid <0 && errno ==EEXIST ) + goto retryget;Hrm, but this would result in an endless loop if the shm was already taken persistently.
Quoted 3 lines
If so, shouldn't the first call to shmget succeed?
To me it seems very unlikely that both calls continuosly fail.
Well, you are inserting a loop at the code below:
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 = shmget(dmix->ipc_key, sizeof(snd_pcm_direct_share_t),
IPC_CREAT | IPC_EXCL | dmix->ipc_perm)) != -1)
first_instance = 1;
==> if (dmix->shmid < 0 && errno == EEXIST)
==> goto retryget;
}It's in the if block when the first shmget() fails. If there was already a shm with the given id that had been assigned by another (not necessarily by alsa-lib but by whatever program), the first shmget returns an error with EEXIST. Then it goes back again in a loop; and it can be endless if another program doesn't release the shm.
Quoted 5 lines
Quoted 2 lines
Also, which call does give a negative shmid, actually? It's from the first
shmget()or the secondshmget()?What happens is that both threads go down that path but, of course, only one succeeds in the second shmget call.
This should have been protected by a sempahore beforehand. And if it's about threads, the application itself has to take care of the race. alsa-lib is no thread-safe, after all.
So, did you see the issue with multiple processes, or is it about the multi-threads?
Takashi
Quoted 51 lines · Takashi Iwai
Quoted 30 lines · Ismael Luceno
Quoted 14 lines · Takashi Iwai
Quoted 12 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]>--- src/pcm/pcm_direct.c | 2 ++ 1 file changed, 2 insertions(+)Quoted 13 lines
src/pcm/pcm_direct.c retryget: · line 105
Quoted 10 lines
src/pcm/pcm_direct.c +2
-0@@ -101,6 +101,8 @@ retryget:if ((dmix->shmid =shmget (dmix->ipc_key,sizeof (snd_pcm_direct_share_t),IPC_CREAT |IPC_EXCL | dmix->ipc_perm)) !=-1 ) first_instance =1 ; +if (dmix->shmid <0 && errno ==EEXIST ) + goto retryget;Hrm, but this would result in an endless loop if the shm was already taken persistently.
Quoted 3 lines
If so, shouldn't the first call to shmget succeed?
To me it seems very unlikely that both calls continuosly fail.
Well, you are inserting a loop at the code below:
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 = shmget(dmix->ipc_key, sizeof(snd_pcm_direct_share_t), IPC_CREAT | IPC_EXCL | dmix->ipc_perm)) != -1) first_instance = 1; ==> if (dmix->shmid < 0 && errno == EEXIST) ==> goto retryget; }
Quoted 5 lines
It's in the if block when the first
shmget()fails. If there was already a shm with the given id that had been assigned by another (not necessarily by alsa-lib but by whatever program), the first shmget returns an error with EEXIST. Then it goes back again in a loop; and it can be endless if another program doesn't release the shm.
I will fix my patch, I see a problem now, but the spirit of the idea should be right: if the first call fails with ENOENT, and the second with EEXIST, it should retry, and getting EEXIST again should be very unlikely.
Quoted 6 lines
Quoted 5 lines
Quoted 2 lines
Also, which call does give a negative shmid, actually? It's from the first
shmget()or the secondshmget()?What happens is that both threads go down that path but, of course, only one succeeds in the second shmget call.
Quoted 6 lines
This should have been protected by a sempahore beforehand. And if it's about threads, the application itself has to take care of the race. alsa-lib is no thread-safe, after all.
So, did you see the issue with multiple processes, or is it about the multi-threads?
Threads in this case, but could happen with processes as well.