diff --git a/core/includes/bootstrap.inc b/core/includes/bootstrap.inc
index 207fdda..0b2ce65 100644
--- a/core/includes/bootstrap.inc
+++ b/core/includes/bootstrap.inc
@@ -703,12 +703,14 @@ function drupal_set_message($message = NULL, $type = 'status', $repeat = FALSE)
       $_SESSION['messages'][$type] = array();
     }
 
-    if ($repeat || !in_array($message, $_SESSION['messages'][$type])) {
-      $_SESSION['messages'][$type][] = array(
-        'safe' => SafeMarkup::isSafe($message),
-        'message' => $message,
-      );
+    $new = array(
+      'safe' => SafeMarkup::isSafe($message),
+      'message' => $message,
+    );
+    if ($repeat || !in_array($new, $_SESSION['messages'][$type])) {
+      $_SESSION['messages'][$type][] = $new;
     }
+    unset($new);
 
     // Mark this page as being uncacheable.
     \Drupal::service('page_cache_kill_switch')->trigger();
diff --git a/core/modules/system/src/Tests/Bootstrap/DrupalSetMessageTest.php b/core/modules/system/src/Tests/Bootstrap/DrupalSetMessageTest.php
index c34f8dd..37a1663 100644
--- a/core/modules/system/src/Tests/Bootstrap/DrupalSetMessageTest.php
+++ b/core/modules/system/src/Tests/Bootstrap/DrupalSetMessageTest.php
@@ -34,4 +34,13 @@ function testSetRemoveMessages() {
     $this->assertText('Second message (not removed).');
   }
 
+  /**
+   * Tests setting duplicated messages.
+   */
+  function testDuplicatedMessages() {
+    $this->drupalGet('system-test/drupal-set-message');
+    $this->assertUniqueText('Non Duplicated message');
+    $this->assertNoUniqueText('Duplicated message');
+  }
+
 }
diff --git a/core/modules/system/tests/modules/system_test/src/Controller/SystemTestController.php b/core/modules/system/tests/modules/system_test/src/Controller/SystemTestController.php
index d79e3f6..492f425 100644
--- a/core/modules/system/tests/modules/system_test/src/Controller/SystemTestController.php
+++ b/core/modules/system/tests/modules/system_test/src/Controller/SystemTestController.php
@@ -65,6 +65,13 @@ public function drupalSetMessageTest() {
 
     // Remove the first.
     unset($_SESSION['messages']['status'][0]);
+
+    // Duplicate message check.
+    drupal_set_message('Non Duplicated message', 'status', FALSE);
+    drupal_set_message('Non Duplicated message', 'status', FALSE);
+
+    drupal_set_message('Duplicated message', 'status', TRUE);
+    drupal_set_message('Duplicated message', 'status', TRUE);
     return [];
   }
 
