atmacola
atmacola

Reputation: 387

php process never dies, after ZMQContext-based $socket->send() How to fix it?

I need my docker container, that contains a php script, to send a message via a web socket before dying, but the process never stops ( even after an exit, die or throw new exception ).

If the process never stops, the container never stops too ...

I made an example, to illustrate my problem:

test.php :

<?php
echo "begin\n";

$dsn = 'tcp://127.0.0.1:5555';
$context = new ZMQContext();
$socket = $context->getSocket(ZMQ::SOCKET_REQ);
$socket->connect($dsn);
$socket->send("test", ZMQ::MODE_DONTWAIT); // this line cause the zombie process
$socket->disconnect($dsn);

echo "end\n";

Dockerfile :

FROM php:7.2.4-cli-alpine3.7

# PHPIZE
ENV PHPIZE_DEPS \
    autoconf \
    cmake \
    file \
    g++ \
    gcc \
    libc-dev \
    pcre-dev \
    make \
    git \
    pkgconf \
    re2c \
    zlib-dev \
    icu-dev
RUN set -xe \
    && apk add --update icu \
    && apk add --no-cache --virtual .build-deps $PHPIZE_DEPS

# Module ZMQ
RUN apk add --no-cache --virtual .persistent-deps \
    libsodium-dev \
    zeromq-dev \
    zeromq
RUN git clone https://github.com/mkoppanen/php-zmq /tmp/php-zmq \
    && cd /tmp/php-zmq \
    && phpize  \
    && ./configure  \
    && make  \
    && make install \
    && make test \
    && docker-php-ext-enable zmq

# Cleanup
RUN apk del .build-deps \
    && rm -rf /tmp/* /usr/local/lib/php/doc/* /var/cache/apk/*.

COPY test.php /tmp/test.php

CMD ["php", "/tmp/test.php"]

How to prevent the blocking or how to kill the process from the script? ( I tried exec( 'kill 1' ) without a success )

Upvotes: 1

Views: 359

Answers (1)

atmacola
atmacola

Reputation: 387

Ok, I found it !

I tried a setSockOpt() I seen on an example, and now the process ends.

Added before connect():

$socket->setSockOpt(ZMQ::SOCKOPT_LINGER, 30);

http://php.net/manual/en/class.zmq.php#zmq.constants.sockopt-linger

Upvotes: 3

Related Questions