Skip to content

Commit cca7d04

Browse files
committed
PHPC-553: Suppress warnings during stream creation
This allows PHP's warnings during connection initialization (e.g. unresolvable host names) to be captured into the bson_error_t, which is later used to throw an exception.
1 parent 0a08b03 commit cca7d04

5 files changed

+136
-2
lines changed

php_phongo.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1086,6 +1086,7 @@ bool php_phongo_ssl_verify(php_stream *stream, const char *hostname, bson_error_
10861086

10871087
mongoc_stream_t* phongo_stream_initiator(const mongoc_uri_t *uri, const mongoc_host_list_t *host, void *user_data, bson_error_t *error) /* {{{ */
10881088
{
1089+
zend_error_handling error_handling;
10891090
php_phongo_stream_socket *base_stream = NULL;
10901091
php_stream *stream = NULL;
10911092
const bson_t *options;
@@ -1138,7 +1139,9 @@ mongoc_stream_t* phongo_stream_initiator(const mongoc_uri_t *uri, const mongoc_h
11381139
spprintf(&uniqid, 0, "%s:%d[%s]", host->host, host->port, mongoc_uri_get_string(uri));
11391140

11401141
MONGOC_DEBUG("Connecting to '%s'", uniqid);
1142+
zend_replace_error_handling(EH_SUPPRESS, NULL, &error_handling TSRMLS_CC);
11411143
stream = php_stream_xport_create(dsn, dsn_len, 0, STREAM_XPORT_CLIENT | STREAM_XPORT_CONNECT, uniqid, timeoutp, (php_stream_context *)user_data, &errmsg, &errcode);
1144+
zend_restore_error_handling(&error_handling TSRMLS_CC);
11421145

11431146
if (!stream) {
11441147
bson_set_error (error, MONGOC_ERROR_STREAM, MONGOC_ERROR_STREAM_CONNECT, "Failed connecting to '%s:%d': %s", host->host, host->port, phongo_str(errmsg));
@@ -1155,8 +1158,6 @@ mongoc_stream_t* phongo_stream_initiator(const mongoc_uri_t *uri, const mongoc_h
11551158
efree(uniqid);
11561159

11571160
if (mongoc_uri_get_ssl(uri)) {
1158-
zend_error_handling error_handling;
1159-
11601161
zend_replace_error_handling(EH_THROW, php_phongo_sslconnectionexception_ce, &error_handling TSRMLS_CC);
11611162

11621163
MONGOC_DEBUG("Enabling SSL");
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
--TEST--
2+
MongoDB\Driver\Manager::executeBulkWrite() should not issue warning before exception
3+
--SKIPIF--
4+
<?php require __DIR__ . "/../utils/basic-skipif.inc"; ?>
5+
--FILE--
6+
<?php
7+
require_once __DIR__ . "/../utils/basic.inc";
8+
9+
$bulk = new MongoDB\Driver\BulkWrite;
10+
$bulk->insert(['x' => 1]);
11+
12+
// Invalid host cannot be resolved
13+
$manager = new MongoDB\Driver\Manager('mongodb://invalid.host:27017', ['serverSelectionTimeoutMS' => 1]);
14+
15+
echo throws(function() use ($manager, $bulk) {
16+
$manager->executeBulkWrite(NS, $bulk);
17+
}, 'MongoDB\Driver\Exception\RuntimeException'), "\n";
18+
19+
// Valid host refuses connection
20+
$manager = new MongoDB\Driver\Manager('mongodb://localhost:54321', ['serverSelectionTimeoutMS' => 1]);
21+
22+
echo throws(function() use ($manager, $bulk) {
23+
$manager->executeBulkWrite(NS, $bulk);
24+
}, 'MongoDB\Driver\Exception\RuntimeException'), "\n";
25+
26+
?>
27+
===DONE===
28+
<?php exit(0); ?>
29+
--EXPECT--
30+
OK: Got MongoDB\Driver\Exception\RuntimeException
31+
No suitable servers found (`serverselectiontryonce` set): [Failed connecting to 'invalid.host:27017': php_network_getaddresses: getaddrinfo failed: Name or service not known]
32+
OK: Got MongoDB\Driver\Exception\RuntimeException
33+
No suitable servers found (`serverselectiontryonce` set): [Failed connecting to 'localhost:54321': Connection refused]
34+
===DONE===
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
--TEST--
2+
MongoDB\Driver\Manager::executeCommand() should not issue warning before exception
3+
--SKIPIF--
4+
<?php require __DIR__ . "/../utils/basic-skipif.inc"; ?>
5+
--FILE--
6+
<?php
7+
require_once __DIR__ . "/../utils/basic.inc";
8+
9+
$command = new MongoDB\Driver\Command(['ping' => 1]);
10+
11+
// Invalid host cannot be resolved
12+
$manager = new MongoDB\Driver\Manager('mongodb://invalid.host:27017', ['serverSelectionTimeoutMS' => 1]);
13+
14+
echo throws(function() use ($manager, $command) {
15+
$manager->executeCommand(DATABASE_NAME, $command);
16+
}, 'MongoDB\Driver\Exception\RuntimeException'), "\n";
17+
18+
// Valid host refuses connection
19+
$manager = new MongoDB\Driver\Manager('mongodb://localhost:54321', ['serverSelectionTimeoutMS' => 1]);
20+
21+
echo throws(function() use ($manager, $command) {
22+
$manager->executeCommand(DATABASE_NAME, $command);
23+
}, 'MongoDB\Driver\Exception\RuntimeException'), "\n";
24+
25+
?>
26+
===DONE===
27+
<?php exit(0); ?>
28+
--EXPECT--
29+
OK: Got MongoDB\Driver\Exception\RuntimeException
30+
No suitable servers found (`serverselectiontryonce` set): [Failed connecting to 'invalid.host:27017': php_network_getaddresses: getaddrinfo failed: Name or service not known]
31+
OK: Got MongoDB\Driver\Exception\RuntimeException
32+
No suitable servers found (`serverselectiontryonce` set): [Failed connecting to 'localhost:54321': Connection refused]
33+
===DONE===
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
--TEST--
2+
MongoDB\Driver\Manager::executeQuery() should not issue warning before exception
3+
--SKIPIF--
4+
<?php require __DIR__ . "/../utils/basic-skipif.inc"; ?>
5+
--FILE--
6+
<?php
7+
require_once __DIR__ . "/../utils/basic.inc";
8+
9+
$query = new MongoDB\Driver\Query([]);
10+
11+
// Invalid host cannot be resolved
12+
$manager = new MongoDB\Driver\Manager('mongodb://invalid.host:27017', ['serverSelectionTimeoutMS' => 1]);
13+
14+
echo throws(function() use ($manager, $query) {
15+
$manager->executeQuery(NS, $query);
16+
}, 'MongoDB\Driver\Exception\RuntimeException'), "\n";
17+
18+
// Valid host refuses connection
19+
$manager = new MongoDB\Driver\Manager('mongodb://localhost:54321', ['serverSelectionTimeoutMS' => 1]);
20+
21+
echo throws(function() use ($manager, $query) {
22+
$manager->executeQuery(NS, $query);
23+
}, 'MongoDB\Driver\Exception\RuntimeException'), "\n";
24+
25+
?>
26+
===DONE===
27+
<?php exit(0); ?>
28+
--EXPECT--
29+
OK: Got MongoDB\Driver\Exception\RuntimeException
30+
No suitable servers found (`serverselectiontryonce` set): [Failed connecting to 'invalid.host:27017': php_network_getaddresses: getaddrinfo failed: Name or service not known]
31+
OK: Got MongoDB\Driver\Exception\RuntimeException
32+
No suitable servers found (`serverselectiontryonce` set): [Failed connecting to 'localhost:54321': Connection refused]
33+
===DONE===
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
--TEST--
2+
MongoDB\Driver\Manager::selectServer() should not issue warning before exception
3+
--SKIPIF--
4+
<?php require __DIR__ . "/../utils/basic-skipif.inc"; ?>
5+
--FILE--
6+
<?php
7+
require_once __DIR__ . "/../utils/basic.inc";
8+
9+
$rp = new MongoDB\Driver\ReadPreference(MongoDB\Driver\ReadPreference::RP_PRIMARY);
10+
11+
// Invalid host cannot be resolved
12+
$manager = new MongoDB\Driver\Manager('mongodb://invalid.host:27017', ['serverSelectionTimeoutMS' => 1]);
13+
14+
echo throws(function() use ($manager, $rp) {
15+
$manager->selectServer($rp);
16+
}, 'MongoDB\Driver\Exception\RuntimeException'), "\n";
17+
18+
// Valid host refuses connection
19+
$manager = new MongoDB\Driver\Manager('mongodb://localhost:54321', ['serverSelectionTimeoutMS' => 1]);
20+
21+
echo throws(function() use ($manager, $rp) {
22+
$manager->selectServer($rp);
23+
}, 'MongoDB\Driver\Exception\RuntimeException'), "\n";
24+
25+
?>
26+
===DONE===
27+
<?php exit(0); ?>
28+
--EXPECT--
29+
OK: Got MongoDB\Driver\Exception\RuntimeException
30+
No suitable servers found (`serverselectiontryonce` set): [Failed connecting to 'invalid.host:27017': php_network_getaddresses: getaddrinfo failed: Name or service not known]
31+
OK: Got MongoDB\Driver\Exception\RuntimeException
32+
No suitable servers found (`serverselectiontryonce` set): [Failed connecting to 'localhost:54321': Connection refused]
33+
===DONE===

0 commit comments

Comments
 (0)