diff --git a/core/modules/image/src/Controller/ImageStyleDownloadController.php b/core/modules/image/src/Controller/ImageStyleDownloadController.php
index 5da6bc89d548ba04a86287481c9b655c945c946b..8859bdd16a90482ccee07220bbc742367182555e 100644
--- a/core/modules/image/src/Controller/ImageStyleDownloadController.php
+++ b/core/modules/image/src/Controller/ImageStyleDownloadController.php
@@ -139,8 +139,8 @@ public function deliver(Request $request, $scheme, ImageStyleInterface $image_st
 
     // Don't start generating the image if the derivative already exists or if
     // generation is in progress in another thread.
-    $lock_name = 'image_style_deliver:' . $image_style->id() . ':' . Crypt::hashBase64($image_uri);
     if (!file_exists($derivative_uri)) {
+      $lock_name = 'image_style_deliver:' . $image_style->id() . ':' . Crypt::hashBase64($image_uri);
       $lock_acquired = $this->lock->acquire($lock_name);
       if (!$lock_acquired) {
         // Tell client to retry again in 3 seconds. Currently no browsers are