php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #39987 loss of resources
Submitted: 2006-12-29 17:01 UTC Modified: 2006-12-31 19:23 UTC
From: wojtekm86 at konto dot pl Assigned:
Status: Not a bug Package: Semaphore related
PHP Version: 5.2.0 OS: Linux
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If this is not your bug, you can add a comment by following this link.
If this is your bug, but you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: wojtekm86 at konto dot pl
New email:
PHP Version: OS:

 

 [2006-12-29 17:01 UTC] wojtekm86 at konto dot pl
Description:
------------
It's impossible to correct remove semaphores with sem_remove function when I use them to provide execution of concurrent processes.
When the last process releases the semaphore I should be able to remove it. But I don't know if another process haven't acquired the semaphore.
For safety reasons I don't remove released semaphores from system.
Now, If I get 128 semaphores and I execute sem_get one more time I will get warning.
So, I can't remove semaphores (because I don't know if another process is using it) and I can't get next semaphore.

Reproduce code:
---------------
$id = sem_get(SOME_ID);
sem_acquire($id);

HERE IS CRITICAL REGION

sem_release($id);

Expected result:
----------------
Expected result (and correct, I think) is removal of unused semaphore in sem_release function if another process didn't acquire it and isn't waiting for doing this.

Release and Removal of semaphore should be one chain of instructions without possibility of separation (e.g. by interruption).

Actual result:
--------------
System reaches the maximum number of semaphores and warning is reported.
The warning is: Warning: sem_get() [function.sem-get]: failed for key 0x5202e59f: No space 
left on device in /home/cicik/ftp/php-art/klasy/semafor.php on line 8


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2006-12-29 17:14 UTC] tony2001@php.net
Cannot reproduce.
sem_release() DOES remove the semaphor if no other process uses it.
 [2006-12-29 17:33 UTC] wojtekm86 at konto dot pl
I think it doesn't.
In my script I have class Semaphore.
There are methods like acquire() and release().
After each sem_acquire instruction I wrote:

echo('I\'ve  acquired semaphore key: ' . $this->key . '<br>');

After each  sem_release instruction I wrote:

echo('I\'ve released semaphore key: ' . $this->key . '<br>');

I have executed script only once.
The result I saw is:

I've acquired semaphore key: 1376159737
I've released semaphore key: 1376159737
I've acquired semaphore key: 1376159747
I've released semaphore key: 1376159747
I've acquired semaphore key: 1376159737
I've released semaphore key: 1376159737
I've acquired semaphore key: 1376159748
I've released semaphore key: 1376159748
I've acquired semaphore key: 1376159737
I've released semaphore key: 1376159737
I've acquired semaphore key: 1376159749
I've released semaphore key: 1376159749

As you can see each semaphore is released.
But when I run command ipcs -s on my server I get:

------ Semaphore Arrays --------
key        semid      owner      perms      nsems     
0x520687e6 131075     www_php-a 600        3         
0x520687f9 163844     www_php-a 600        3         
0x520687fa 196613     www_php-a 600        3         
0x520687fc 229382     www_php-a 600        3         
0x520687e9 262151     www_php-a 600        3         
0x520687fd 294920     www_php-a 600        3         
0x520687fe 327689     www_php-a 600        3         
0x520687ff 360458     www_php-a 600        3         
0x52068800 393227     www_php-a 600        3         
0x52068801 425996     www_php-a 600        3         
0x52068802 458765     www_php-a 600        3         
0x52068803 491534     www_php-a 600        3         
0x52068804 524303     www_php-a 600        3         
0x52068805 557072     www_php-a 600        3         

You can see that there are not removed semaphores.
 [2006-12-29 17:45 UTC] wojtekm86 at konto dot pl
Next code.

I have a test script with code as follows:

<?php
$id = sem_get(10);
sem_acquire($id);
for($ind = 0; $ind < 10000; $ind++) { }
sem_release($id);
?>

I run it only once.

The result I get after execute command ipcs -s on the server:

------ Semaphore Arrays --------
key        semid      owner      perms      nsems     
0x0000000a 131075     www_php-a 666        3         

It seems that there is a problem with semaphores' removal after release.
 [2006-12-30 10:54 UTC] wojtekm86 at konto dot pl
I think that there is also a problem with parameter max_acquire because value in column nsems always equals 3
 [2006-12-30 23:19 UTC] iliaa@php.net
Thank you for taking the time to write to us, but this is not
a bug. Please double-check the documentation available at
http://www.php.net/manual/ and the instructions on how to report
a bug at http://bugs.php.net/how-to-report.php

if you want to remove the semaphore you need to call the 
sem_remove() function. Closing the semaphore won't remove it 
no more so then closing a file will remove it from disk.
 [2006-12-31 08:24 UTC] wojtekm86 at konto dot pl
I think you don't understand me.
I know that sem_release doesn't remove the semaphore but I think it should because it's the one and only way to do it correct, of course if current process, which releases a semaphore, is the last one which acquired it.

Please, read one more time bug's description.
I think you didn't understand my intention.
 [2006-12-31 08:26 UTC] wojtekm86 at konto dot pl
I'm changing the status to Open
 [2006-12-31 09:13 UTC] wojtekm86 at konto dot pl
I will try to tell about my sugestion.

I assume that I have a function my_sem_remove($sem_id):

function my_sem_remove($id) {
  if(Semaphore $id isn't acquired and its FIFO is empty)
    sem_remove($id);
}
None of instructions in this function can be interrupted.
So, now the problem is simple to resolve:

$id = sem_get(ID);
sem_acquire($id);

CRITICAL REGION

sem_release($id);
my_sem_remove($id);

But now, two instructions: sem_release and my_sem_remove are not needed. It's simple to replace them by one function my_sem_release:

function my_sem_release($id) {
  sem_release($id);
  my_sem_remove($id);
}

For reasons which about I will write later those instructions can't be interrupted.

So, my example look like this:

$id = sem_get(ID);
sem_acquire($id);

CRITICAL REGION

my_sem_release($id);

But let's imagine two concurrent processes executing code above:

PROC1: $id = sem_get(ID);
<---  interrupt
PROC2: $id = sem_get(ID);
       sem_acquire($id);
       CRITICAL REGION
       my_sem_release($id);
<--- interrupt
PROC1: sem_acquire($id); <--- Uuups!! Error: semaphore doesn't exists, PROC2 has removed it.

My solution:
Instructions sem_get and sem_acquire can't be interrupted.
So, I assume that there is a function my_sem_acquire:

function my_sem_acquire($key, $max_acquire = 1, $perm = 0666, $auto_release = true) {

  $id = sem_get($key, $max_acquire, $perm, $auto_release);
  sem_acquire();
  return $id;
}

None of instructions in function above can be interrupted.

And finally correct example:

$id = my_sem_acquire(SEMKEY);

CRITICAL REGION

my_sem_release($id);

It's the best solutions without danger of operating on not existing semaphores. Also, unused semaphores are automaticcally removed. The problem is, that functions my_sem_acquire and my_sem_release can't be witten in PHP so it must be done in C inside source of PHP.
 [2006-12-31 19:23 UTC] iliaa@php.net
Thank you for taking the time to write to us, but this is not
a bug. Please double-check the documentation available at
http://www.php.net/manual/ and the instructions on how to report
a bug at http://bugs.php.net/how-to-report.php

If you want, please make a feature request. However, the 
current behavior is absolutely correct. 
 
PHP Copyright © 2001-2021 The PHP Group
All rights reserved.
Last updated: Thu Oct 21 11:03:34 2021 UTC