Skip to content

Commit cf188e9

Browse files
authored
PCBC-899: ensure the connection will be closed on error (#70)
1 parent a60931c commit cf188e9

File tree

4 files changed

+27
-15
lines changed

4 files changed

+27
-15
lines changed

bin/build

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ end
7171
CB_PHP_PREFIX = ENV.fetch("CB_PHP_PREFIX", DEFAULT_PHP_PREFIX)
7272
CB_CC = ENV.fetch("CB_CC", default_cc)
7373
CB_CXX = ENV.fetch("CB_CXX", default_cxx)
74+
CB_CMAKE_BUILD_TYPE = ENV.fetch("CMAKE_BUILD_TYPE", "Debug")
7475

7576
run("#{CB_PHP_PREFIX}/bin/php --version || true")
7677
run("#{CB_PHP_PREFIX}/bin/php --ini || true")
@@ -85,7 +86,7 @@ end
8586
Dir.chdir(PROJECT_ROOT) do
8687
run("#{CB_PHP_PREFIX}/bin/phpize")
8788
run("rm -rf cmake-build") unless ENV.fetch("CB_DO_NOT_CLEAN", false)
88-
run("./configure --with-php-config=#{CB_PHP_PREFIX}/bin/php-config CC=#{CB_CC} CXX=#{CB_CXX}")
89+
run("./configure --with-php-config=#{CB_PHP_PREFIX}/bin/php-config CC=#{CB_CC} CXX=#{CB_CXX} COUCHBASE_CMAKE_BUILD_TYPE=#{CB_CMAKE_BUILD_TYPE}")
8990
exit if ENV.fetch("CB_CONFIGURE_ONLY", false)
9091
run("make clean") unless ENV.fetch("CB_DO_NOT_CLEAN", false)
9192
ENV["CMAKE_BUILD_PARALLEL_LEVEL"] = ENV.fetch("CB_NUMBER_OF_JOBS", "4")

config.m4

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ PHP_ADD_MAKEFILE_FRAGMENT
3939
AC_CONFIG_COMMANDS_POST([
4040
echo "
4141
CMAKE : ${CMAKE}
42+
CMAKE_BUILD_TYPE : ${COUCHBASE_CMAKE_BUILD_TYPE:-RelWithDebInfo}
4243
CMAKE_SOURCE_DIRECTORY : ${COUCHBASE_CMAKE_SOURCE_DIRECTORY}
4344
CMAKE_BUILD_DIRECTORY : ${COUCHBASE_CMAKE_BUILD_DIRECTORY}
4445
CMAKE_C_COMPILER : ${CC}
@@ -51,7 +52,7 @@ COUCHBASE_PHP_LIBDIR : ${phplibdir}
5152
COUCHBASE_CMAKE_EXTRA : ${COUCHBASE_CMAKE_EXTRA}
5253
"
5354
${CMAKE} -S ${COUCHBASE_CMAKE_SOURCE_DIRECTORY} -B${COUCHBASE_CMAKE_BUILD_DIRECTORY} \
54-
-DCMAKE_BUILD_TYPE=RelWithDebInfo \
55+
-DCMAKE_BUILD_TYPE=${COUCHBASE_CMAKE_BUILD_TYPE:-RelWithDebInfo} \
5556
-DCMAKE_C_COMPILER="${CC}" \
5657
-DCMAKE_CXX_COMPILER="${CXX}" \
5758
-DCMAKE_C_FLAGS="${CFLAGS}" \

src/wrapper/connection_handle.cxx

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -390,16 +390,7 @@ class connection_handle::impl : public std::enable_shared_from_this<connection_h
390390

391391
~impl()
392392
{
393-
if (cluster_) {
394-
auto barrier = std::make_shared<std::promise<void>>();
395-
auto f = barrier->get_future();
396-
cluster_->close([barrier]() { barrier->set_value(); });
397-
f.wait();
398-
if (worker.joinable()) {
399-
worker.join();
400-
}
401-
cluster_.reset();
402-
}
393+
stop();
403394
}
404395

405396
[[nodiscard]] std::shared_ptr<couchbase::core::cluster> cluster() const
@@ -412,6 +403,20 @@ class connection_handle::impl : public std::enable_shared_from_this<connection_h
412403
worker = std::thread([self = shared_from_this()]() { self->ctx_.run(); });
413404
}
414405

406+
void stop()
407+
{
408+
if (cluster_) {
409+
auto barrier = std::make_shared<std::promise<void>>();
410+
auto f = barrier->get_future();
411+
cluster_->close([barrier]() { barrier->set_value(); });
412+
f.wait();
413+
cluster_.reset();
414+
if (worker.joinable()) {
415+
worker.join();
416+
}
417+
}
418+
}
419+
415420
std::string cluster_version(const std::string& bucket_name = "")
416421
{
417422
auto barrier = std::make_shared<std::promise<couchbase::core::operations::management::cluster_describe_response>>();
@@ -452,6 +457,7 @@ class connection_handle::impl : public std::enable_shared_from_this<connection_h
452457
auto f = barrier->get_future();
453458
cluster_->open(origin_, [barrier](std::error_code ec) { barrier->set_value(ec); });
454459
if (auto ec = f.get()) {
460+
stop();
455461
return { ec, { __LINE__, __FILE__, __func__ } };
456462
}
457463
return {};
@@ -3431,7 +3437,7 @@ connection_handle::change_password(zval* return_value, const zend_string* new_pa
34313437
{
34323438
couchbase::core::operations::management::change_password_request request{ cb_string_new(new_password) };
34333439

3434-
if(auto e = cb_assign_timeout(request, options); e.ec) {
3440+
if (auto e = cb_assign_timeout(request, options); e.ec) {
34353441
return e;
34363442
}
34373443

tests/UserManagerTest.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -218,8 +218,12 @@ function () use ($username) {
218218

219219
$cluster = new Cluster(self::env()->connectionString(), $options);
220220

221-
$this->expectException(AuthenticationFailureException::class);
222-
$newCluster = new Cluster(self::env()->connectionString(), $newOptions);
221+
$this->wrapException(
222+
function () use ($newOptions) {
223+
new Cluster(self::env()->connectionString(), $newOptions);
224+
},
225+
AuthenticationFailureException::class
226+
);
223227

224228
$manager = $cluster->users();
225229
$manager->changePassword("newPassword");

0 commit comments

Comments
 (0)