When sending a message with a trailing space the PHP function pack()
includes the space in the length
header, but the unpack()
function strips trailing spaces and causes a deadlock. The deadlock is due to the $msg['length']
header being 1 byte longer than strlen($msg['payload'])
.
This can be demonstrated by running php bin/packer.php
.
The deadlock can be reproduced by opening two terminals. In the first run php bin/receiver.php
. In the second run php bin/send.php "message with trailing space "
. The "message with trailing space "
will cause a deadlock. Sending ctrl-c
to php bin/send.php
will get things moving again, and new messages can be sent.
In this example look at the timestamps on the logged messages. You can see the message "message with trailing space "
was sent at 2025-08-28T14:04:31+00:00
and then we deadlock until ctrl-c
was sent to bin/send.php
. At that point bin/receiver.php
continues and logs 2025-08-28T14:04:39+00:00 :: received: 'message with trailing space'
without the trailing space.
at 14:03:25 ❯ php bin/receiver.php
using socket path: /run/user/502/sockit
num_msg 0
2025-08-28T14:04:22+00:00 :: received: 'message without trailing space'
num_msg 1
num_msg 1
num_msg 0
2025-08-28T14:04:39+00:00 :: received: 'message with trailing space'
PHP Warning: socket_send(): Unable to write to socket [32]: Broken pipe in /home/larntz.linux/repos/php-symplib-testing/vendor/simple-ipc/php-symplib/src/SocketStreamsServer.php on line 253
num_msg 2
num_msg 0
num_msg 0
num_msg 0
2025-08-28T14:04:55+00:00 :: received: 'message without trailing space'
num_msg 1
num_msg 1
^C
at 14:03:53 ❯ php bin/send.php "message without trailing space"
using socket path: /run/user/502/sockit
2025-08-28T14:04:22+00:00 :: sendMessage complete, waiting for receiveMessage
2025-08-28T14:04:22+00:00 :: receiveMessage got: ok
at 14:04:22 ❯ php bin/send.php "message with trailing space "
using socket path: /run/user/502/sockit
2025-08-28T14:04:31+00:00 :: sendMessage complete, waiting for receiveMessage
^C
at 14:04:39 ❯ php bin/send.php "message without trailing space"
using socket path: /run/user/502/sockit
2025-08-28T14:04:55+00:00 :: sendMessage complete, waiting for receiveMessage
2025-08-28T14:04:55+00:00 :: receiveMessage got: ok