Using msg_send() in PHP results in error 11… Cannot find a solution

Issue

I am currently building a small application that uses the message queue built in PHP.
I have 1 "server" process and 1 "client" process. Messages flow from server to client.

They are simple JSON objects, that are serialised, then send.

This code is used

<?php
$send = msg_send($q, MESSAGE_TYPE_EXECUTION, $update, true, false, $error);

if (isset($error) && $error != 0) {
    echo 'Execution error: ' . $error . PHP_EOL;
}

// $q is the message queue integer
// MESSAGE_TYPE_EXECUTION is integer 1
// $update is the JSON string
// true is that the JSON string is serialised
// false is that it is blocking (which it is not)
// $error get's filled when an error occurs (see below)

This works without issue, until it does not.
Sometimes after a couple of minutes, sometimes after a couple of hours the following error appears:

PHP Warning:  msg_send(): msgsnd failed: Resource temporarily unavailable in 
/var/www/server.php on line 57

The value of the $error variable is the integer 11.
All messages that follow this error will have error 11, until I restart the process and all is working again (for a while, until the same error appears again)

I have been searching but cannot find any explanation what error 11 is, how this can be managed and fixed without restarting the process.

Any clue, information, example etc is welcome. I would really like for server.php to be reliable.

— edit —
client.php is the process that fetches the messages (which are all more or less the same, but with other values)

it uses this fetch the messages from the queue (filled in server.php)

<?php
$update = msg_receive($q, 0, $messagetype, 1024, $message, true, MSG_IPC_NOWAIT && MSG_NOERROR, $error);

if ($update) {
    // Do stuff
}
usleep(1000000);

I have not yet checked memory usage, will look into that

Platform used

  • PHP 7.1.3
  • Centos 7

Solution

So, Solution was found after some information and leads (read the comments on my original question), brought up by @ChrisHaas (Thanks again!). After some tinkering all is running smoothly now, without error 11 for msg_send().

PHP msg_send() call is basically a wrapper of msgsnd
So a lot of information can be found there, also about errors you might encounter (in combination with flags used when reading messages with msg_receive() )

The queue is limited in total size and total messages it can hold (I, however, have not found a way to increase the total size of the queue).

The reason I was getting error 11 was due to a couple of things:

  • The client I created was too slow fetching messages from the queue, causing it to run into the max limit and crapping out. I did not find a solution for fixing this situation, other than restarting all processes involved. To repeat the same over and over again.
  • I also increased the size of reading messages in msg_receive() as sometimes the messages where big (most where small). But when you declare a too small size the big messages will remain in queue and clog it up until it craps out. Increasing the max_size helped with fetching the bigger messages too.

Long story short: error 11 is related to a full message queue in my perspective (I still do not have a 100% clear documented answer though).

Pointers to fix the issue:

  • Be sure you fetch all messages that are big.
  • Be sure to read out at least as fast as you send the messages in the queue.
  • Check your queue(s) with the command ipcs -q in the terminal. It allows you to see the queues currently active. Keeping an eye on that allows you to see it slowly filling up on problems.

Wish the documentation on php.net was better in this case…

Answered By – MrG

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published