-
Notifications
You must be signed in to change notification settings - Fork 209
PHPC-578: Expose result document for failed commands #781
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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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=== |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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=== |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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()
useszend_update_property()
. Unless the object is some internal class that is also using a custom handler, this will callzend_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 ofBulkWriteException::$writeResult
, I believe there are a few reasons: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.phongo_add_exception_prop()
assignsreturn_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 onreturn_value
when returning fromexecuteBulkWrite()
back to userland when an exception is thrown since it knows that userland has no way to access the function's return value.return_value
with refcount=1 assigned to the exception object. It is ultimately freed along with the exception itself.