Implement semaphore retry. Make not getting the semaphore an error

pull/4202/head
Isaac Connor 2021-12-17 09:41:45 -05:00
parent da0cf59144
commit 6479a1ec99
1 changed files with 14 additions and 5 deletions

View File

@ -7,7 +7,7 @@ define('MSG_TIMEOUT', ZM_WEB_AJAX_TIMEOUT/2);
define('MSG_DATA_SIZE', 4+256); define('MSG_DATA_SIZE', 4+256);
if ( !($_REQUEST['connkey'] && $_REQUEST['command']) ) { if ( !($_REQUEST['connkey'] && $_REQUEST['command']) ) {
ajaxError("Unexpected received message type '$type'"); ajaxError('No connkey or no command in stream ajax');
} }
mkdir(ZM_PATH_SOCKS); mkdir(ZM_PATH_SOCKS);
@ -15,7 +15,17 @@ mkdir(ZM_PATH_SOCKS);
# The file that we point ftok to has to exist, and only exist if zms is running, so we are pointing it at the .sock # The file that we point ftok to has to exist, and only exist if zms is running, so we are pointing it at the .sock
$key = ftok(ZM_PATH_SOCKS.'/zms-'.sprintf('%06d', $_REQUEST['connkey']).'s.sock', 'Z'); $key = ftok(ZM_PATH_SOCKS.'/zms-'.sprintf('%06d', $_REQUEST['connkey']).'s.sock', 'Z');
$semaphore = sem_get($key, 1); $semaphore = sem_get($key, 1);
if (sem_acquire($semaphore, 1) !== false) { $semaphore_tries = 10;
$have_semaphore = false;
while ($semaphore_tries) {
$have_semaphore = sem_acquire($semaphore, 1);
if ($have_semaphore !== false) break;
ZM\Debug('Failed to get semaphore, trying again');
usleep(100000);
$semaphore_tries -= 1;
}
if ($have_semaphore) {
if ( !($socket = @socket_create(AF_UNIX, SOCK_DGRAM, 0)) ) { if ( !($socket = @socket_create(AF_UNIX, SOCK_DGRAM, 0)) ) {
ajaxError('socket_create() failed: '.socket_strerror(socket_last_error())); ajaxError('socket_create() failed: '.socket_strerror(socket_last_error()));
} }
@ -148,12 +158,11 @@ if (sem_acquire($semaphore, 1) !== false) {
ajaxResponse(array('status'=>$data)); ajaxResponse(array('status'=>$data));
break; break;
default : default :
ajaxError("Unexpected received message type '$type'"); ajaxError('Unexpected received message type '.$data['type']);
} }
sem_release($semaphore); sem_release($semaphore);
} else { } else {
ZM\Debug('Couldn\'t get semaphore'); ajaxError("Unable to get semaphore.");
ajaxResponse(array());
} }
ajaxError('Unrecognised action or insufficient permissions in ajax/stream'); ajaxError('Unrecognised action or insufficient permissions in ajax/stream');