Skip to content

Commit 0465d2d

Browse files
committed
add shutdown functions
1 parent efd6d91 commit 0465d2d

File tree

5 files changed

+63
-0
lines changed

5 files changed

+63
-0
lines changed

agent/php_mysqli.c

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -688,3 +688,28 @@ const char* nr_php_mysqli_strip_persistent_prefix(const char* host) {
688688

689689
return host;
690690
}
691+
692+
void nr_mysqli_rshutdown() {
693+
/*
694+
* This frees mysqli metadata stored in the transaction.
695+
*
696+
* `mysqli_queries` contains duplicates of zvals. If
697+
* `nr_php_txn_end` is called from the post-deactivate callback, request
698+
* shutdown functions have already been called; and the Zend VM has already
699+
* forcefully freed all dangling zvals that are not referenced by the global
700+
* scope (regardless of their reference count), thus leaving the zvals stored
701+
* in the mysqli_queries metadata in an "undefined" state. Consequently,
702+
* freeing the zvals in `nr_php_txn_end` at this stage can result in undefined
703+
* behavior.
704+
*
705+
* Calling this function during the RSHUTDOWN phase ensures that the zvals in
706+
* `mysqli_queries` are cleaned up before Zend winds down the VM and
707+
* forcefully frees zvals.
708+
*
709+
* If `nr_php_txn_end` is called outside the post-deactivate callback,
710+
* it frees `mysqli_queries` by itself.
711+
*/
712+
if (nrlikely(NRPRG(txn))) {
713+
nr_hashmap_destroy(&NRTXNGLOBAL(mysqli_queries));
714+
}
715+
}

agent/php_mysqli.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,4 +163,10 @@ extern nr_datastore_instance_t* nr_php_mysqli_retrieve_datastore_instance(
163163
extern void nr_php_mysqli_remove_datastore_instance(
164164
const zval* mysqli_obj TSRMLS_DC);
165165

166+
/*
167+
* Purpose : Frees reference incremented, transaction global zvals
168+
* that must be cleaned up prior to postdeactivate
169+
*/
170+
extern void nr_mysqli_rshutdown();
171+
166172
#endif /* PHP_MYSQLI_HDR */

agent/php_pdo.c

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -642,3 +642,28 @@ zval* nr_php_pdo_disable_persistence(const zval* options TSRMLS_DC) {
642642
nr_php_zval_free(&persistent);
643643
return result;
644644
}
645+
646+
void nr_pdo_rshutdown() {
647+
/*
648+
* This frees pdo metadata stored in the transaction.
649+
*
650+
* `pdo_link_options` contains duplicates of zvals. If
651+
* `nr_php_txn_end` is called from the post-deactivate callback, request
652+
* shutdown functions have already been called; and the Zend VM has already
653+
* forcefully freed all dangling zvals that are not referenced by the global
654+
* scope (regardless of their reference count), thus leaving the zvals stored
655+
* in the pdo_link_options metadata in an "undefined" state. Consequently,
656+
* freeing the zvals in `nr_php_txn_end` at this stage can result in undefined
657+
* behavior.
658+
*
659+
* Calling this function during the RSHUTDOWN phase ensures that the zvals in
660+
* `pdo_link_options` are cleaned up before Zend winds down the VM and
661+
* forcefully frees zvals.
662+
*
663+
* If `nr_php_txn_end` is called outside the post-deactivate callback,
664+
* it frees `pdo_link_options` by itself.
665+
*/
666+
if (nrlikely(NRPRG(txn))) {
667+
nr_hashmap_destroy(&NRTXNGLOBAL(pdo_link_options));
668+
}
669+
}

agent/php_pdo.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,4 +161,9 @@ extern nr_status_t nr_php_pdo_parse_data_source(
161161
extern void nr_php_pdo_free_data_sources(struct pdo_data_src_parser* parsed,
162162
size_t nparams);
163163

164+
/*
165+
* Purpose : Frees reference incremented, transaction global zvals
166+
* that must be cleaned up prior to postdeactivate
167+
*/
168+
extern void nr_pdo_rshutdown();
164169
#endif

agent/php_rshutdown.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@ PHP_RSHUTDOWN_FUNCTION(newrelic) {
4949

5050
nr_guzzle4_rshutdown(TSRMLS_C);
5151
nr_curl_rshutdown(TSRMLS_C);
52+
nr_pdo_rshutdown();
53+
nr_mysqli_rshutdown();
5254

5355
nrl_verbosedebug(NRL_INIT, "RSHUTDOWN processing done");
5456

0 commit comments

Comments
 (0)