Repository: elastic/apm-agent-php (php)

Issue:
Memory problem since 1.11.0
Hi,

I'm using a Dockerfile to run a symfony application

I'm installing APM with:

```Dockerfile
ARG APM_VERSION="v1.10.0/apm-agent-php_1.10.0_all.deb"
RUN set -e; \
    curl -s -L -o /tmp/apm-agent.deb https://github.com/elastic/apm-agent-php/releases/download/${APM_VERSION}; \
    apt install /tmp/apm-agent.deb; \
    rm -f /tmp/apm-agent.deb
```

All works OK, but when I upgrade to 1.11.0 I have memory problems

I can't reproduce the problem outside my project I can't share it (it's not my project, the owner is a company).

Let me share the output depending on the APM version installed and some APM config:

**Installing v1.10.0/apm-agent-php_1.10.0_all.deb**

CAPTURE_ERRORS OFF:
```shell
$ rm -Rf var/cache/* && ELASTIC_APM_CAPTURE_ERRORS="false" bin/console cache:warmup -n

 // Warming up the cache for the dev environment with debug false                                                       
                                                                                                                        
 [OK] Cache for the "dev" environment (debug=false) was successfully warmed.                                            
                                                                                                                        
peak memory usage (MB): 168
```

CAPTURE_ERRORS ON:
```shell
$ rm -Rf var/cache/* && ELASTIC_APM_CAPTURE_ERRORS="true" bin/console cache:warmup -n

 // Warming up the cache for the dev environment with debug false                                                       
                                                                                                                        
 [OK] Cache for the "dev" environment (debug=false) was successfully warmed.                                            
                                                                                                                        
peak memory usage (MB): 166
```

**Installing v1.11.0/apm-agent-php_1.11.0_amd64.deb**

CAPTURE_ERRORS OFF:
```shell
$ rm -Rf var/cache/* && ELASTIC_APM_CAPTURE_ERRORS="false" bin/console cache:warmup -n

 // Warming up the cache for the dev environment with debug false                                                       
                                                                                                                        
 [OK] Cache for the "dev" environment (debug=false) was successfully warmed.                                            
                                                                                                                        
peak memory usage (MB): 168
```

CAPTURE_ERRORS ON:
```shell
$ rm -Rf var/cache/* && ELASTIC_APM_CAPTURE_ERRORS="true" bin/console cache:warmup -n
PHP Fatal error:  Allowed memory size of 1073741824 bytes exhausted (tried to allocate 20480 bytes) in /app/vendor/symfony/dependency-injection/Compiler/AutowireRequiredMethodsPass.php on line 76
```

Same problem with 1.13.0

Do you know what is happening?

Some info:
* ubuntu 22.04
* php 8.2

php mods enabled:

```
app@fd77ede50f3f:/etc/php/8.2/cli/conf.d$ ls
10-mysqlnd.ini  20-apcu.ini      20-dom.ini       20-gettext.ini   20-intl.ini       20-phar.ini      20-simplexml.ini  20-tokenizer.ini  20-yaml.ini         99-elastic-apm-custom.ini
10-opcache.ini  20-bcmath.ini    20-exif.ini      20-gmp.ini       20-mbstring.ini   20-posix.ini     20-sockets.ini    20-uuid.ini       20-zip.ini
10-pdo.ini      20-calendar.ini  20-ffi.ini       20-iconv.ini     20-mongodb.ini    20-readline.ini  20-sysvmsg.ini    20-xmlreader.ini  30-ds.ini
15-xml.ini      20-ctype.ini     20-fileinfo.ini  20-igbinary.ini  20-mysqli.ini     20-redis.ini     20-sysvsem.ini    20-xmlwriter.ini  90-company.ini
20-amqp.ini     20-curl.ini      20-ftp.ini       20-imagick.ini   20-pdo_mysql.ini  20-shmop.ini     20-sysvshm.ini    20-xsl.ini        98-elastic-apm.ini
```

Pull request description:
Fixed memory leak in exception handling (#1159)
Fixes #1159 #1159

Patch:
diff --git a/.ci/updatecli/updatecli.d/update-gherkin-specs.yml b/.ci/updatecli/updatecli.d/update-gherkin-specs.yml
index 13f0617a..0296437b 100644
--- a/.ci/updatecli/updatecli.d/update-gherkin-specs.yml
+++ b/.ci/updatecli/updatecli.d/update-gherkin-specs.yml
@@ -6,27 +6,27 @@ scms:
     kind: github
     spec:
       user: '{{ requiredEnv "GITHUB_ACTOR" }}'
-      owner: "{{ .github.owner }}"
-      repository: "{{ .github.repository }}"
+      owner: "{{ .scm.owner }}"
+      repository: "{{ .scm.repository }}"
       token: '{{ requiredEnv "GITHUB_TOKEN" }}'
       username: '{{ requiredEnv "GITHUB_ACTOR" }}'
-      branch: "{{ .github.branch }}"
+      branch: "{{ .scm.branch }}"
       commitusingapi: true
   apm:
     kind: github
     spec:
       user: '{{ requiredEnv "GITHUB_ACTOR" }}'
-      owner: "{{ .github.owner }}"
-      repository: "{{ .github.apm_repository }}"
+      owner: "{{ .scm.owner }}"
+      repository: "{{ .scm.apm_repository }}"
       token: '{{ requiredEnv "GITHUB_TOKEN" }}'
       username: '{{ requiredEnv "GITHUB_ACTOR" }}'
-      branch: "{{ .github.branch }}"
+      branch: "{{ .scm.branch }}"
 
 sources:
   sha:
     kind: file
     spec:
-      file: 'https://github.com/{{ .github.owner }}/{{ .github.apm_repository }}/commit/{{ .github.branch }}.patch'
+      file: 'https://github.com/{{ .scm.owner }}/{{ .scm.apm_repository }}/commit/{{ .scm.branch }}.patch'
       matchpattern: "^From\\s([0-9a-f]{40})\\s"
     transformers:
       - findsubmatch:
@@ -36,7 +36,7 @@ sources:
     dependson:
       - sha
     spec:
-      command: gh api /repos/{{ .github.owner }}/{{ .github.apm_repository }}/commits/{{ source "sha" }}/pulls --jq '.[].html_url'
+      command: gh api /repos/{{ .scm.owner }}/{{ .scm.apm_repository }}/commits/{{ source "sha" }}/pulls --jq '.[].html_url'
       environments:
         - name: GITHUB_TOKEN
         - name: PATH
diff --git a/.ci/updatecli/updatecli.d/update-json-specs.yml b/.ci/updatecli/updatecli.d/update-json-specs.yml
index ae981677..a92587e4 100644
--- a/.ci/updatecli/updatecli.d/update-json-specs.yml
+++ b/.ci/updatecli/updatecli.d/update-json-specs.yml
@@ -6,27 +6,27 @@ scms:
     kind: github
     spec:
       user: '{{ requiredEnv "GITHUB_ACTOR" }}'
-      owner: "{{ .github.owner }}"
-      repository: "{{ .github.repository }}"
+      owner: "{{ .scm.owner }}"
+      repository: "{{ .scm.repository }}"
       token: '{{ requiredEnv "GITHUB_TOKEN" }}'
       username: '{{ requiredEnv "GITHUB_ACTOR" }}'
-      branch: "{{ .github.branch }}"
+      branch: "{{ .scm.branch }}"
       commitusingapi: true
   apm:
     kind: github
     spec:
       user: '{{ requiredEnv "GITHUB_ACTOR" }}'
-      owner: "{{ .github.owner }}"
-      repository: "{{ .github.apm_repository }}"
+      owner: "{{ .scm.owner }}"
+      repository: "{{ .scm.apm_repository }}"
       token: '{{ requiredEnv "GITHUB_TOKEN" }}'
       username: '{{ requiredEnv "GITHUB_ACTOR" }}'
-      branch: "{{ .github.branch }}"
+      branch: "{{ .scm.branch }}"
 
 sources:
   sha:
     kind: file
     spec:
-      file: 'https://github.com/{{ .github.owner }}/{{ .github.apm_repository }}/commit/{{ .github.branch }}.patch'
+      file: 'https://github.com/{{ .scm.owner }}/{{ .scm.apm_repository }}/commit/{{ .scm.branch }}.patch'
       matchpattern: "^From\\s([0-9a-f]{40})\\s"
     transformers:
       - findsubmatch:
@@ -36,7 +36,7 @@ sources:
     dependson:
       - sha
     spec:
-      command: gh api /repos/{{ .github.owner }}/{{ .github.apm_repository }}/commits/{{ source "sha" }}/pulls --jq '.[].html_url'
+      command: gh api /repos/{{ .scm.owner }}/{{ .scm.apm_repository }}/commits/{{ source "sha" }}/pulls --jq '.[].html_url'
       environments:
         - name: GITHUB_TOKEN
         - name: PATH
@@ -67,7 +67,7 @@ actions:
         ### Why
         *Changeset*
         * {{ source "pull_request" }}
-        * https://github.com/{{ .github.owner }}/{{ .github.apm_repository }}/commit/{{ source "sha" }}
+        * https://github.com/{{ .scm.owner }}/{{ .scm.apm_repository }}/commit/{{ source "sha" }}
       title: '[Automation] Update JSON specs'
 
 targets:
diff --git a/.ci/updatecli/updatecli.d/update-specs.yml b/.ci/updatecli/updatecli.d/update-specs.yml
deleted file mode 100644
index e9bbaf94..00000000
--- a/.ci/updatecli/updatecli.d/update-specs.yml
+++ /dev/null
@@ -1,85 +0,0 @@
-name: update-specs
-pipelineid: update-schema-specs
-
-scms:
-  default:
-    kind: github
-    spec:
-      user: '{{ requiredEnv "GITHUB_ACTOR" }}'
-      owner: "{{ .github.owner }}"
-      repository: "{{ .github.repository }}"
-      token: '{{ requiredEnv "GITHUB_TOKEN" }}'
-      username: '{{ requiredEnv "GITHUB_ACTOR" }}'
-      branch: "{{ .github.branch }}"
-      commitusingapi: true
-
-  apm-data:
-    kind: github
-    spec:
-      user: '{{ requiredEnv "GITHUB_ACTOR" }}'
-      owner: "{{ .github.owner }}"
-      repository: "{{ .github.apm_data_repository }}"
-      token: '{{ requiredEnv "GITHUB_TOKEN" }}'
-      username: '{{ requiredEnv "GITHUB_ACTOR" }}'
-      branch: "{{ .github.branch }}"
-
-sources:
-  sha:
-    kind: file
-    spec:
-      file: 'https://github.com/{{ .github.owner }}/{{ .github.apm_data_repository }}/commit/{{ .github.branch }}.patch'
-      matchpattern: "^From\\s([0-9a-f]{40})\\s"
-    transformers:
-      - findsubmatch:
-          pattern: "[0-9a-f]{40}"
-  pull_request:
-    kind: shell
-    dependson:
-      - sha
-    spec:
-      command: gh api /repos/{{ .github.owner }}/{{ .github.apm_data_repository }}/commits/{{ source "sha" }}/pulls --jq '.[].html_url'
-      environments:
-        - name: GITHUB_TOKEN
-        - name: PATH
-  agent-specs-tarball:
-    kind: shell
-    scmid: apm-data
-    dependson:
-      - sha
-    spec:
-      command: tar cvzf {{ requiredEnv "GITHUB_WORKSPACE" }}/json-schema.tgz .
-      environments:
-        - name: PATH
-      workdir: "{{ .specs.apm_data_path }}"
-
-actions:
-  pr:
-    kind: "github/pullrequest"
-    scmid: default
-    sourceid: sha
-    spec:
-      automerge: false
-      draft: false
-      labels:
-        - "automation"
-      description: |-
-        ### What
-        APM agent json server schema automatic sync
-
-        ### Why
-        *Changeset*
-        * {{ source "pull_request" }}
-        * https://github.com/{{ .github.owner }}/{{ .github.apm_data_repository }}/commit/{{ source "sha" }}
-      title: '[Automation] Update JSON server schema specs'
-
-targets:
-  agent-json-schema:
-    name: APM agent json server schema {{ source "sha" }}
-    scmid: default
-    disablesourceinput: true
-    kind: shell
-    spec:
-      # git diff helps to print what it changed, If it is empty, then updatecli report a success with no changes applied.
-      # See https://www.updatecli.io/docs/plugins/resource/shell/#_shell_target
-      command: 'tar -xzf {{ requiredEnv "GITHUB_WORKSPACE" }}/json-schema.tgz && git --no-pager diff'
-      workdir: "{{ .apm_agent.server_schema_specs_path  }}"
diff --git a/.ci/updatecli/values.d/apm-data-spec.yml b/.ci/updatecli/values.d/apm-data-spec.yml
new file mode 100644
index 00000000..282d7f78
--- /dev/null
+++ b/.ci/updatecli/values.d/apm-data-spec.yml
@@ -0,0 +1,2 @@
+apm_schema_specs_path: tests/APM_Server_intake_API_schema/latest_used
+signedcommit: true
\ No newline at end of file
diff --git a/.ci/updatecli/values.d/scm.yml b/.ci/updatecli/values.d/scm.yml
new file mode 100644
index 00000000..b51fb2b1
--- /dev/null
+++ b/.ci/updatecli/values.d/scm.yml
@@ -0,0 +1,6 @@
+scm:
+  enabled: true
+  owner: elastic
+  repository: apm-agent-php
+  apm_repository: apm
+  branch: main
\ No newline at end of file
diff --git a/.ci/updatecli/values.d/specs.yml b/.ci/updatecli/values.d/specs.yml
new file mode 100644
index 00000000..9e4f8548
--- /dev/null
+++ b/.ci/updatecli/values.d/specs.yml
@@ -0,0 +1,6 @@
+specs:
+  apm_json_path: "tests/agents/json-specs"
+  apm_gherkin_path: "tests/agents/gherkin-specs"
+apm_agent:
+  gherkin_specs_path: "tests/APM_Agents_shared/gherkin-specs"
+  json_specs_path: "tests/APM_Agents_shared/json-specs"
\ No newline at end of file
diff --git a/.ci/updatecli/values.yml b/.ci/updatecli/values.yml
deleted file mode 100644
index e4e37b1a..00000000
--- a/.ci/updatecli/values.yml
+++ /dev/null
@@ -1,14 +0,0 @@
-github:
-  owner: "elastic"
-  repository: "apm-agent-php"
-  apm_repository: "apm"
-  apm_data_repository: "apm-data"
-  branch: "main"
-specs:
-  apm_data_path: "input/elasticapm/docs/spec/v2"
-  apm_json_path: "tests/agents/json-specs"
-  apm_gherkin_path: "tests/agents/gherkin-specs"
-apm_agent:
-  gherkin_specs_path: "tests/APM_Agents_shared/gherkin-specs"
-  json_specs_path: "tests/APM_Agents_shared/json-specs"
-  server_schema_specs_path: "tests/APM_Server_intake_API_schema/latest_used"
\ No newline at end of file
diff --git a/.github/workflows/updatecli.yml b/.github/workflows/updatecli.yml
index a9e2f509..1906bd61 100644
--- a/.github/workflows/updatecli.yml
+++ b/.github/workflows/updatecli.yml
@@ -16,7 +16,40 @@ jobs:
 
       - uses: elastic/oblt-actions/updatecli/run@v1
         with:
-          command: "--experimental apply --config .ci/updatecli/updatecli.d --values .ci/updatecli/values.yml"
+          command: "--experimental apply --config .ci/updatecli/updatecli.d --values .ci/updatecli/values.d/scm.yml --values .ci/updatecli/values.d/specs.yml"
+        env:
+          GITHUB_TOKEN: ${{ secrets.UPDATECLI_GH_TOKEN }}
+
+      - if: failure()
+        uses: elastic/oblt-actions/slack/send@v1
+        with:
+          bot-token: ${{ secrets.SLACK_BOT_TOKEN }}
+          channel-id: "#apm-agent-php"
+          message: ":traffic_cone: updatecli failed for `${{ github.repository }}@${{ github.ref_name }}`, @robots-ci please look what's going on <https://github.com/${{ github.repository }}/actions/runs/${{ github.run_id }}|here>"
+
+  compose:
+    runs-on: ubuntu-latest
+    permissions:
+      contents: read
+      packages: read
+    steps:
+      - uses: actions/checkout@v4
+
+      - uses: docker/login-action@0d4c9c5ea7693da7b068278f7b52bda2a190a446 # v3.2.0
+        with:
+          registry: ghcr.io
+          username: ${{ github.actor }}
+          password: ${{ secrets.GITHUB_TOKEN }}
+
+      - uses: elastic/oblt-actions/updatecli/run@v1
+        with:
+          command: --experimental compose diff
+        env:
+          GITHUB_TOKEN: ${{ secrets.UPDATECLI_GH_TOKEN }}
+
+      - uses: elastic/oblt-actions/updatecli/run@v1
+        with:
+          command: --experimental compose apply
         env:
           GITHUB_TOKEN: ${{ secrets.UPDATECLI_GH_TOKEN }}
 
diff --git a/agent/native/ext/lifecycle.cpp b/agent/native/ext/lifecycle.cpp
index 0291c2f1..0e55f83b 100644
--- a/agent/native/ext/lifecycle.cpp
+++ b/agent/native/ext/lifecycle.cpp
@@ -162,11 +162,10 @@ void elasticApmZendThrowExceptionHookImpl(
     resetLastThrown();
 
 #if PHP_MAJOR_VERSION >= 8 /* if PHP version is 8.* and later */
-    zval thrownAsZval;
-    zval* thrownAsPzval = &thrownAsZval;
-    ZVAL_OBJ_COPY( /* dst: */ thrownAsPzval, /* src: */ thrownAsPzobj );
+    ZVAL_OBJ_COPY(&ELASTICAPM_G( lastException ), thrownAsPzobj );
+#else
+    ZVAL_COPY(&ELASTICAPM_G(lastException), thrownAsPzval );
 #endif
-    ZVAL_COPY( /* pZvalDst: */ &ELASTICAPM_G(lastException), /* pZvalSrc: */ thrownAsPzval );
 
     ELASTIC_APM_LOG_DEBUG_FUNCTION_EXIT();
 }
@@ -577,7 +576,7 @@ void elasticApmRequestShutdown()
         ELASTIC_APM_LOG_DEBUG( "opcache.preload request detected on shutdown" );
         return;
     }
-	
+
     if (ELASTICAPM_G(globals)->periodicTaskExecutor_) {
         ELASTIC_APM_LOG_DEBUG("pausing inferred spans thread");
         ELASTICAPM_G(globals)->periodicTaskExecutor_->suspendPeriodicTasks();
@@ -681,4 +680,4 @@ ResultCode elasticApmEnterAgentCode( String dbgCalledFromFile, int dbgCalledFrom
 
     failure:
     goto finally;
-}
\ No newline at end of file
+}
diff --git a/update-compose.yaml b/update-compose.yaml
new file mode 100644
index 00000000..3a226f18
--- /dev/null
+++ b/update-compose.yaml
@@ -0,0 +1,6 @@
+policies:
+  - name: Handle apm-data server specs
+    policy: ghcr.io/elastic/oblt-updatecli-policies/apm/apm-data-spec:0.0.1@sha256:1b8a9876bb8ff9d4b61a0b655b957f10e9c65de0c8a333a88b7424d10b498adc
+    values:
+      - .ci/updatecli/values.d/scm.yml
+      - .ci/updatecli/values.d/apm-data-spec.yml


Test patch:
diff --git a/tests/ElasticApmTests/ComponentTests/Util/BuiltinHttpServerAppCodeHostStarter.php b/tests/ElasticApmTests/ComponentTests/Util/BuiltinHttpServerAppCodeHostStarter.php
index e327f9fd..4b04ef3d 100644
--- a/tests/ElasticApmTests/ComponentTests/Util/BuiltinHttpServerAppCodeHostStarter.php
+++ b/tests/ElasticApmTests/ComponentTests/Util/BuiltinHttpServerAppCodeHostStarter.php
@@ -74,7 +74,7 @@ final class BuiltinHttpServerAppCodeHostStarter extends HttpServerStarter
     {
         Assert::assertCount(1, $ports);
         return InfraUtilForTests::buildAppCodePhpCmd($this->agentConfigSourceBuilder->getPhpIniFile())
-               . " -S localhost:" . $ports[0]
+               . " -S 0.0.0.0:" . $ports[0]
                . ' "' . FileUtilForTests::listToPath([__DIR__, self::APP_CODE_HOST_ROUTER_SCRIPT]) . '"';
     }