Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Try and refcount the returned zval to avoid it being GCed #24

Open
wants to merge 21 commits into
base: return-socket-resources
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
language: c

before_install:
- sudo apt-get update -qq
- sudo apt-get -y install libc-ares-dev libssl-dev uuid-dev

before_script:
- ./travis/compile-mosquitto.sh
- ./travis/compile-php.sh
- ./travis/compile-ext.sh

script:
- ./travis/run-tests.sh

2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

This is an extension to allow using the [Mosquitto MQTT library](http://mosquitto.org) with PHP. See the examples/ directory for usage.

[![Build Status](https://travis-ci.org/mgdm/Mosquitto-PHP.svg?branch=master)](https://travis-ci.org/mgdm/Mosquitto-PHP)

## Requirements

* PHP 5.3+
Expand Down
16 changes: 16 additions & 0 deletions examples/socket-refcount-test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

$client = new Mosquitto\Client;
$client->onConnect(function() use ($client) {
$client->subscribe('#', 0);
});

$client->connect('localhost');

while (true) {
$socket = $client->getSocket();
var_dump($socket);
$r = $w = $e = [$socket];
$changedSockets = socket_select($r, $w, $e, 30);
$client->loop();
}
14 changes: 14 additions & 0 deletions mosquitto.c
Original file line number Diff line number Diff line change
Expand Up @@ -611,6 +611,13 @@ PHP_METHOD(Mosquitto_Client, getSocket)

object = (mosquitto_client_object *) mosquitto_client_object_get(getThis() TSRMLS_CC);

if (object->socket_zval != NULL) {
zval_dtor(return_value);
*return_value = *object->socket_zval;
zval_copy_ctor(return_value);
return;
}

socket = mosquitto_socket(object->client);
if (socket < 0) {
zend_throw_exception_ex(mosquitto_ce_exception, 0 TSRMLS_CC, "Unable to create socket resource");
Expand All @@ -625,6 +632,8 @@ PHP_METHOD(Mosquitto_Client, getSocket)
php_sock->zstream = NULL;

ZEND_REGISTER_RESOURCE(return_value, php_sock, php_sockets_le_socket());
object->socket_zval = return_value;
Z_ADDREF_P(object->socket_zval);
}
/* }}} */

Expand Down Expand Up @@ -859,6 +868,10 @@ static void mosquitto_client_object_destroy(void *object TSRMLS_DC)
efree(MQTTG(client_key));
}

if (client->socket_zval != NULL) {
zval_ptr_dtor(&(client->socket_zval));
}

PHP_MOSQUITTO_FREE_CALLBACK(connect);
PHP_MOSQUITTO_FREE_CALLBACK(subscribe);
PHP_MOSQUITTO_FREE_CALLBACK(unsubscribe);
Expand All @@ -885,6 +898,7 @@ static zend_object_value mosquitto_client_object_new(zend_class_entry *ce TSRMLS
client = ecalloc(1, sizeof(mosquitto_client_object));
client->std.ce = ce;
client->client = NULL;
client->socket_zval = NULL;

#ifdef ZTS
client->TSRMLS_C = TSRMLS_C;
Expand Down
2 changes: 2 additions & 0 deletions php_mosquitto.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ typedef struct _mosquitto_client_object {
zend_fcall_info log_callback;
zend_fcall_info_cache log_callback_cache;

zval *socket_zval;

int looping;

#ifdef ZTS
Expand Down
15 changes: 7 additions & 8 deletions tests/Client/__construct.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,12 @@ $client = new Mosquitto\Client(null, true);
var_dump($client);

/* Null ID and no clean session should fail */
$client = new Mosquitto\Client(null, false);
var_dump($client);
/* Behaviour varies between OSX and Linux in Mosquitto 1.4 */
try {
$client = new Mosquitto\Client(null, false);
} catch (Exception $e) {
echo "Caught exception.";
}

?>
--EXPECTF--
Expand All @@ -37,9 +41,4 @@ object(Mosquitto\Client)#%d (%d) {
}
object(Mosquitto\Client)#%d (%d) {
}

Fatal error: Uncaught exception 'Mosquitto\Exception' with message 'Invalid argument' in %s
Stack trace:
#0 %s: Mosquitto\Client->__construct(NULL, false)
#1 {main}
thrown in %s
Caught exception.
2 changes: 1 addition & 1 deletion tests/Client/onDisconnect.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ for ($i = 0; $i < 5; $i++) {

?>
--EXPECTF--
Caught error 4096 (Argument 1 passed to Mosquitto\Client::onDisconnect() must be callable, string given) in /Users/michael/Code/Mosquitto-PHP/tests/Client/onDisconnect.php on line 6
Caught error 4096 (Argument 1 passed to Mosquitto\Client::onDisconnect() must be callable, string given) in %s on line 6
Caught Mosquitto\Exception with code 0 and message: Mosquitto\Client::onDisconnect() expects parameter 1 to be a valid callback, function 'foo' not found or invalid function name
Triggering disconnect
Disconnected
Expand Down
2 changes: 1 addition & 1 deletion tests/Client/onSubscribe.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ $client->loopForever();

?>
--EXPECTF--
Caught error 4096 (Argument 1 passed to Mosquitto\Client::onSubscribe() must be callable, string given) in /Users/michael/Code/Mosquitto-PHP/tests/Client/onSubscribe.php on line 6
Caught error 4096 (Argument 1 passed to Mosquitto\Client::onSubscribe() must be callable, string given) in %s on line 6
Caught Mosquitto\Exception with code 0 and message: Mosquitto\Client::onSubscribe() expects parameter 1 to be a valid callback, function 'foo' not found or invalid function name
array(3) {
[0]=>
Expand Down
7 changes: 7 additions & 0 deletions travis/compile-ext.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#!/bin/sh -x
set -e
$HOME/bin/phpize
./configure --with-php-config=$HOME/bin/php-config
make -j2 --quiet
make install
echo "extension=mosquitto.so" > $HOME/php.d/mosquitto.ini
8 changes: 8 additions & 0 deletions travis/compile-mosquitto.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#!/bin/bash

wget http://mosquitto.org/files/source/mosquitto-1.4.tar.gz
tar xvzf mosquitto-1.4.tar.gz
cd mosquitto-1.4
make
sudo make install

21 changes: 21 additions & 0 deletions travis/compile-php.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#!/usr/bin/env sh
set -e
mkdir -p $HOME/php
git clone https://github.com/php/php-src $HOME/php/src
cd $HOME/php/src
git checkout PHP-5.6
./buildconf --force
./configure \
--prefix=$HOME \
--disable-all \
--enable-debug \
--with-config-file-path=$HOME \
--with-config-file-scan-dir=$HOME/php.d \
--enable-json \
--enable-phar \
--with-openssl

make -j2 --quiet install
cp php.ini-development $HOME
mkdir $HOME/php.d

12 changes: 12 additions & 0 deletions travis/run-tests.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#!/bin/sh

set -e

cd $TRAVIS_BUILD_DIR

cd tests
./makeTestCerts.sh
mosquitto -c mosquitto.conf -d
cd ..

REPORT_EXIT_STATUS=1 TEST_PHP_ARGS="-q --show-diff" make test