9

I'm using semaphore to synchronize some parts in my application.

On releasing of the semaphore (sem_release) I'm getting this warning:

sem_release(): failed to release key 0xc: Invalid argument

First I don't know whether the semaphore released, but since I'm not getting "true" as result, I guess it's not releasing.

PHP Version: 5.6.30

ipcs -V => ipcs from util-linux 2.25.2

Here is the semaphore:

key        semid      owner      perms      nsems 
0x0000000c 4124122    myUser      666        3

Here is part of the code (class Synchronization):

...
if ( !( $this->semaphoreId = sem_get( $this->id, 1 ) ) )
    throw new RuntimeException( 'Error getting Semaphore.');
...

if ( !sem_acquire( $this->semaphoreId ) )
   throw new RuntimeException( 'Error acquiring Semaphore.');
...
if ( !sem_release( $this->semaphoreId ) )
    throw new RuntimeException( 'Error releasing Semaphore.');

P.S. I'm getting this error only in my productive environment and I'm not able to reproduce/debug in in my test environment.

I searched on Internet for this error message, but I found nothing.

Does anyone knows what this message means?

Edited:

  1. The error message does not appear every time the script is running.
  2. I indeed get sometimes the error 'Error acquiring Semaphore' with the similar warining 'sem_acquire(): failed to acquire key 0xc: Identifier removed', BUT not at the same time(day) I get the 'Error releasing Semaphore'
  3. The class above is used all over the place with different keys to synchronize part of application code. I'm not having ANY issues with other keys. And yes, this key "12"/"0xc" is beeing used ONLY in one place and from the same user.
  4. Issues with permission should not occure, because if you check the permisson of the semaphore "0xc" is "666"
dritan
  • 882
  • 2
  • 8
  • 22
  • Did you find a solution for this? I am getting the same warning. – c0dehunter Mar 03 '21 at 10:36
  • 1
    Unfortunately no. I think I made some workarounds. I think removed some usages of sem_acquire. But it's long time ago and I can't remember what exactly I did. – dritan Mar 04 '21 at 11:07

1 Answers1

1

Could you follow the steps from the beginning:

  • Getting the semaphore resource sem_get ( int $key [, int $max_acquire = 1 [, int $perm = 0666 [, int $auto_release = 1 ]]] )
  • Acquiring the semaphore bool sem_acquire ( resource $sem_identifier [, bool $nowait = false ] )

and add sanity checks to make sure that the above function are returning the expected value.

Could you also check if the other part of your application runs under the same user to avoid permission issues.

  • I edited the question with parts of my code and information that somehow answer your "questions" – dritan May 23 '17 at 05:58
  • Could it be that your program is creating deadlocks and then you are unable to release them? – Rares Harnagea May 25 '17 at 05:13
  • no I don't think so, because if deadlock is happening I would see the blocked processes in the process list – dritan May 25 '17 at 07:03
  • It must be something very contextual and related to the production environment. Make sure the IPC is configured the same way as on development server and also check to see if any caching mechanism you might use is not affecting this behavior. – bluehipy May 25 '17 at 12:58