From 83741b3ab44b4f089ca6e6d49b91aaed1d3c024b Mon Sep 17 00:00:00 2001
From: Nathaniel Catchpole <catch@35733.no-reply.drupal.org>
Date: Tue, 9 May 2017 10:48:39 +0100
Subject: [PATCH] Issue #2859704 by alexpott, tacituseu, xjm, vaplas,
 mpdonadio, catch, Mixologic: Intermittent segfaults on DrupalCI (some did

---
 core/lib/Drupal/Core/DrupalKernel.php | 15 +++++++++++++++
 1 file changed, 15 insertions(+)

diff --git a/core/lib/Drupal/Core/DrupalKernel.php b/core/lib/Drupal/Core/DrupalKernel.php
index 3f53d04a3b84..02c97f5edbe4 100644
--- a/core/lib/Drupal/Core/DrupalKernel.php
+++ b/core/lib/Drupal/Core/DrupalKernel.php
@@ -870,6 +870,16 @@ protected function initializeContainer() {
     // If there is no container and no cached container definition, build a new
     // one from scratch.
     if (!isset($container) && !isset($container_definition)) {
+      // Building the container creates 1000s of objects. Garbage collection of
+      // these objects is expensive. This appears to be causing random
+      // segmentation faults in PHP 5.6 due to
+      // https://bugs.php.net/bug.php?id=72286. Once the container is rebuilt,
+      // garbage collection is re-enabled.
+      $disable_gc = version_compare(PHP_VERSION, '7', '<') && gc_enabled();
+      if ($disable_gc) {
+        gc_collect_cycles();
+        gc_disable();
+      }
       $container = $this->compileContainer();
 
       // Only dump the container if dumping is allowed. This is useful for
@@ -879,6 +889,11 @@ protected function initializeContainer() {
         $dumper = new $this->phpArrayDumperClass($container);
         $container_definition = $dumper->getArray();
       }
+      // If garbage collection was disabled prior to rebuilding container,
+      // re-enable it.
+      if ($disable_gc) {
+        gc_enable();
+      }
     }
 
     // The container was rebuilt successfully.
-- 
GitLab