Skip to content

Commit d4e439e

Browse files
committed
PCBC-518: Detect and cleanup connections left in bad state
When `max_execution_time` setting set, the connection might be left in bad state, when the event loop is not properly closed. This patch allows to detect such connections in persistent cache and close them properly. Change-Id: I072afe724f33fbeebff0ae73496147d15e3e1a92 Reviewed-on: http://review.couchbase.org/87388 Tested-by: Build Bot <[email protected]> Reviewed-by: Sergey Avseyev <[email protected]>
1 parent ce7a522 commit d4e439e

File tree

1 file changed

+31
-19
lines changed

1 file changed

+31
-19
lines changed

src/couchbase/pool.c

Lines changed: 31 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -242,12 +242,12 @@ void pcbc_connection_delref(pcbc_connection_t *conn TSRMLS_DC)
242242
}
243243

244244
#if PHP_VERSION_ID >= 70000
245-
static pcbc_connection_t *pcbc_connection_lookup(smart_str *plist_key TSRMLS_DC)
245+
static zend_resource *pcbc_connection_lookup(smart_str *plist_key TSRMLS_DC)
246246
{
247247
zend_resource *res;
248248
res = zend_hash_find_ptr(&EG(persistent_list), plist_key->s);
249-
if (res != NULL && res->type == pcbc_res_couchbase && res->ptr) {
250-
return res->ptr;
249+
if (res != NULL && res->type == pcbc_res_couchbase) {
250+
return res;
251251
}
252252
return NULL;
253253
}
@@ -270,16 +270,14 @@ static lcb_error_t pcbc_connection_cache(smart_str *plist_key, pcbc_connection_t
270270
return LCB_SUCCESS;
271271
}
272272
#else
273-
static pcbc_connection_t *pcbc_connection_lookup(smart_str *plist_key TSRMLS_DC)
273+
static zend_rsrc_list_entry *pcbc_connection_lookup(smart_str *plist_key TSRMLS_DC)
274274
{
275275
zend_rsrc_list_entry *res = NULL;
276276
int rv;
277277

278278
rv = zend_hash_find(&EG(persistent_list), plist_key->c, plist_key->len, (void *)&res);
279-
if (rv == SUCCESS) {
280-
if (res->type == pcbc_res_couchbase) {
281-
return res->ptr;
282-
}
279+
if (rv == SUCCESS && res->type == pcbc_res_couchbase) {
280+
return res;
283281
}
284282
return NULL;
285283
}
@@ -337,6 +335,11 @@ lcb_error_t pcbc_connection_get(pcbc_connection_t **result, lcb_type_t type, con
337335
pcbc_connection_t *conn = NULL;
338336
smart_str plist_key = {0};
339337
zend_bool is_persistent = 1; // always persistent connections
338+
#if PHP_VERSION_ID >= 70000
339+
zend_resource *res = NULL;
340+
#else
341+
zend_rsrc_list_entry *res = NULL;
342+
#endif
340343

341344
rv = pcbc_normalize_connstr(type, (char *)connstr, bucketname, &cstr TSRMLS_CC);
342345
if (rv != LCB_SUCCESS) {
@@ -349,17 +352,26 @@ lcb_error_t pcbc_connection_get(pcbc_connection_t **result, lcb_type_t type, con
349352
smart_str_appends(&plist_key, cstr);
350353
smart_str_appendc(&plist_key, '|');
351354
smart_str_appends(&plist_key, auth_hash);
352-
353-
conn = pcbc_connection_lookup(&plist_key TSRMLS_CC);
354-
if (conn) {
355-
efree(cstr);
356-
smart_str_free(&plist_key);
357-
pcbc_connection_addref(conn TSRMLS_CC);
358-
pcbc_log(LOGARGS(conn->lcb, DEBUG),
359-
"cachehit: type=%d, connstr=%s, bucketname=%s, auth_hash=%s, lcb=%p, refs=%d", conn->type,
360-
conn->connstr, conn->bucketname, conn->auth_hash, conn->lcb, conn->refs);
361-
*result = conn;
362-
return LCB_SUCCESS;
355+
res = pcbc_connection_lookup(&plist_key TSRMLS_CC);
356+
if (res) {
357+
conn = res->ptr;
358+
if (conn) {
359+
if (lcb_is_waiting(conn->lcb)) {
360+
pcbc_log(LOGARGS(conn->lcb, DEBUG),
361+
"cachestale: type=%d, connstr=%s, bucketname=%s, auth_hash=%s, lcb=%p, refs=%d", conn->type,
362+
conn->connstr, conn->bucketname, conn->auth_hash, conn->lcb, conn->refs);
363+
pcbc_destroy_connection_resource(res);
364+
} else {
365+
efree(cstr);
366+
smart_str_free(&plist_key);
367+
pcbc_connection_addref(conn TSRMLS_CC);
368+
pcbc_log(LOGARGS(conn->lcb, DEBUG),
369+
"cachehit: type=%d, connstr=%s, bucketname=%s, auth_hash=%s, lcb=%p, refs=%d", conn->type,
370+
conn->connstr, conn->bucketname, conn->auth_hash, conn->lcb, conn->refs);
371+
*result = conn;
372+
return LCB_SUCCESS;
373+
}
374+
}
363375
}
364376

365377
rv = pcbc_establish_connection(type, &lcb, cstr, auth, auth_hash TSRMLS_CC);

0 commit comments

Comments
 (0)