Commit 367468ca authored by catch's avatar catch
Browse files

fix: #3641672 WorkspaceManager::executeInWorkspace() and...

fix: #3641672 WorkspaceManager::executeInWorkspace() and executeOutsideWorkspace() do not restore the active workspace properly

By: amateescu
(cherry picked from commit c706424d)
parent a6a23779
Loading
Loading
Loading
Loading
Loading
+12 −8
Changes for core/modules/workspaces/src/WorkspaceManager.php: 12 added lines, 8 removed lines.
Original line number Diff line number Diff line
@@ -153,14 +153,16 @@ public function executeInWorkspace($workspace_id, callable $function) {
    if ($should_switch_workspace) {
      $this->doSwitchWorkspace($workspace, TRUE);
    }
    $result = $function();

    // Switch back if needed.
    try {
      return $function();
    }
    finally {
      // Switch back if needed, even when the callback throws an exception.
      if ($should_switch_workspace) {
        $this->doSwitchWorkspace($previous_active_workspace, TRUE);
      }

    return $result;
    }
  }

  /**
@@ -173,14 +175,16 @@ public function executeOutsideWorkspace(callable $function) {
    if ($previous_active_workspace) {
      $this->doSwitchWorkspace(NULL, TRUE);
    }
    $result = $function();

    // Switch back if needed.
    try {
      return $function();
    }
    finally {
      // Switch back if needed, even when the callback throws an exception.
      if ($previous_active_workspace) {
        $this->doSwitchWorkspace($previous_active_workspace, TRUE);
      }

    return $result;
    }
  }

}
+29
Changes for core/modules/workspaces/tests/src/Kernel/WorkspaceIntegrationTest.php: 29 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -891,6 +891,35 @@ function (WorkspaceSwitchEvent $event) use (&$switch_events): void {
    $switch_events = [];
    $this->workspaceManager->executeOutsideWorkspace(function (): void {});
    $this->assertSame([TRUE, TRUE], $switch_events);

    // A callback that throws an exception should not change the active
    // workspace, and the switch back should still be temporary.
    $switch_events = [];
    try {
      $this->workspaceManager->executeOutsideWorkspace(function (): void {
        throw new \RuntimeException('Failed outside the workspace.');
      });
      $this->fail('The exception thrown by the callback was not propagated.');
    }
    catch (\RuntimeException $e) {
      $this->assertSame('Failed outside the workspace.', $e->getMessage());
    }
    $this->assertSame('stage', $this->workspaceManager->getActiveWorkspace()?->id());
    $this->assertSame([TRUE, TRUE], $switch_events);

    $this->workspaceManager->switchToLive();
    $switch_events = [];
    try {
      $this->workspaceManager->executeInWorkspace('stage', function (): void {
        throw new \RuntimeException('Failed in the workspace.');
      });
      $this->fail('The exception thrown by the callback was not propagated.');
    }
    catch (\RuntimeException $e) {
      $this->assertSame('Failed in the workspace.', $e->getMessage());
    }
    $this->assertFalse($this->workspaceManager->hasActiveWorkspace());
    $this->assertSame([TRUE, TRUE], $switch_events);
  }

  /**