Skip to content
Merged
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
20 changes: 19 additions & 1 deletion php_phongo.c
Original file line number Diff line number Diff line change
Expand Up @@ -853,7 +853,25 @@ int phongo_execute_command(mongoc_client_t* client, php_phongo_command_type_t ty
return false;
}
if (!result) {
phongo_throw_exception_from_bson_error_t(&error TSRMLS_CC);
if (error.domain == MONGOC_ERROR_SERVER || error.domain == MONGOC_ERROR_WRITE_CONCERN) {
#if PHP_VERSION_ID >= 70000
zval zv;
#else
zval* zv;
#endif

zend_throw_exception(php_phongo_commandexception_ce, error.message, error.code TSRMLS_CC);
php_phongo_bson_to_zval(bson_get_data(&reply), reply.len, &zv);

#if PHP_VERSION_ID >= 70000
phongo_add_exception_prop(ZEND_STRL("resultDocument"), &zv);
#else
phongo_add_exception_prop(ZEND_STRL("resultDocument"), zv TSRMLS_CC);
#endif
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Testing this locally, I found that it was still leaking. A debug build of PHP typically reports simple leaks on its own, which is how I caught it. Running the tests with Valgrind confirmed the same.

I don't expect that Homebrew uses debug builds of PHP, so no worries there.

Ultimately, I believe this is due to PHP adding a reference when it updates the property of an object. When phongo_add_exception_prop() uses zend_update_property(). Unless the object is some internal class that is also using a custom handler, this will call zend_std_write_property(). In that function, you might notice that the zval gets a reference added to it.

I came up with kvwalker#1 while debugging this, although in hingsight it is possible that a simple zval_ptr_dtor(&zv) before the end of this scope might have fixed the leak. If you determine it does, feel free to do that and ignore my PR.

As for why this differs from phongo_execute_bulk_write() and it's assignment of BulkWriteException::$writeResult, I believe there are a few reasons:

  • There, we always initialize return_value as a WriteResult object, since it's either going to be used as the actual return value or stuffed into an exception. The only time we don't initialize it is if we know there isn't an error and the user is ignoring the return value.
  • When phongo_add_exception_prop() assigns return_value to the exception property, it gets a reference added. At that point, I expect its refcount is 2; however, PHP likely decrements the refcount on return_value when returning from executeBulkWrite() back to userland when an exception is thrown since it knows that userland has no way to access the function's return value.
  • In the event of an exception, we're left with a return_value with refcount=1 assigned to the exception object. It is ultimately freed along with the exception itself.

zval_ptr_dtor(&zv);
} else {
phongo_throw_exception_from_bson_error_t(&error TSRMLS_CC);
}
bson_destroy(&reply);
bson_destroy(&opts);
return false;
Expand Down
64 changes: 64 additions & 0 deletions tests/manager/manager-executeWriteCommand_error-002.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
--TEST--
MongoDB\Driver\Manager::executeWriteCommand() throws CommandException for invalid writeConcern
--SKIPIF--
<?php require __DIR__ . "/../utils/basic-skipif.inc"; ?>
<?php NEEDS('REPLICASET'); CLEANUP(REPLICASET); ?>
--FILE--
<?php
require_once __DIR__ . "/../utils/basic.inc";

$manager = new MongoDB\Driver\Manager(REPLICASET);

$command = new MongoDB\Driver\Command([
'findAndModify' => COLLECTION_NAME,
'query' => ['_id' => 'foo'],
'update' => ['foo' => ['bar']],
'upsert' => true,
'new' => true,
]);

try {
$manager->executeWriteCommand(DATABASE_NAME, $command, ['writeConcern' => new MongoDB\Driver\WriteConcern("undefined")]);
} catch (MongoDB\Driver\Exception\CommandException $e) {
printf("%s(%d): %s\n", get_class($e), $e->getCode(), $e->getMessage());
var_dump($e->getResultDocument());
}

?>
===DONE===
<?php exit(0); ?>
--EXPECTF--
MongoDB\Driver\Exception\CommandException(79): Write Concern error: No write concern mode named 'undefined' found in replica set configuration
object(stdClass)#%d (%d) {
["lastErrorObject"]=>
object(stdClass)#%d (3) {
["n"]=>
int(1)
["updatedExisting"]=>
bool(false)
["upserted"]=>
string(3) "foo"
}
["value"]=>
object(stdClass)#%d (2) {
["_id"]=>
string(3) "foo"
["foo"]=>
array(1) {
[0]=>
string(3) "bar"
}
}
["writeConcernError"]=>
object(stdClass)#%d (3) {
["code"]=>
int(79)
["codeName"]=>
string(23) "UnknownReplWriteConcern"
["errmsg"]=>
string(74) "No write concern mode named 'undefined' found in replica set configuration"
}
["ok"]=>
float(1)%A
}
===DONE===
41 changes: 41 additions & 0 deletions tests/manager/manager-executeWriteCommand_error-003.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
--TEST--
MongoDB\Driver\Manager::executeWriteCommand() throws CommandException for unsupported update operator
--SKIPIF--
<?php require __DIR__ . "/../utils/basic-skipif.inc"; ?>
<?php NEEDS('STANDALONE'); ?>
--FILE--
<?php
require_once __DIR__ . "/../utils/basic.inc";

$manager = new MongoDB\Driver\Manager(STANDALONE);

$command = new MongoDB\Driver\Command([
'findAndModify' => COLLECTION_NAME,
'query' => ['_id' => 'foo'],
'upsert' => true,
'new' => true,
]);

try {
$manager->executeWriteCommand(DATABASE_NAME, $command);
} catch (MongoDB\Driver\Exception\CommandException $e) {
printf("%s(%d): %s\n", get_class($e), $e->getCode(), $e->getMessage());
var_dump($e->getResultDocument());
}

?>
===DONE===
<?php exit(0); ?>
--EXPECT--
MongoDB\Driver\Exception\CommandException(9): Either an update or remove=true must be specified
object(stdClass)#4 (4) {
["ok"]=>
float(0)
["errmsg"]=>
string(49) "Either an update or remove=true must be specified"
["code"]=>
int(9)
["codeName"]=>
string(13) "FailedToParse"
}
===DONE===