php/doc-en

sem_get add warning about freeing variable

Aberta

#3.612 aberto em 25 de jul. de 2024

 (1 comentário) (0 reação) (0 responsável)XML (882 forks)auto 404
enhancementgood first issue

Métricas do repositório

Stars
 (596 estrelas)
Métricas de merge de PR
 (Mesclagem média 99d 23h) (90 fundiu PRs em 30d)

Description

From manual page: https://php.net/function.sem-get

sem_get() has an important gotcha where if the variable is freed then the semaphore is released. I propose this is not quite a bug but could do with a red warning box e.g.

Warning: sem_get will also auto_release the semaphore when the variable which holds it is freed (rather than the process ending). Ensure you store the result in a variable which is not overwritten, unset, or goes out of scope while the semaphore should be alive.

This behaviour can be demonstrated with the following:

$semGet = sem_get(1);

var_dump(sem_acquire($semGet, true));
// true: semaphore(1) acquired

var_dump(sem_acquire(sem_get(1), true));
// false: semaphore(1) already acquired

unset($semGet);

var_dump(sem_acquire(sem_get(1), true));
// true: semaphore(1) released by variable unset() so can be acquired again

Guia do colaborador