From 0effcd2cfed36baec1d960b64c901da7e56710a8 Mon Sep 17 00:00:00 2001
From: Mark Felder <feld@feld.me>
Date: Mon, 19 Apr 2021 15:43:17 -0500
Subject: [PATCH 1/6] Set Repo.transaction/2 timeout to infinity. Fixes
 pleroma.user delete_activities mix task.

---
 lib/pleroma/web/activity_pub/pipeline.ex | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/lib/pleroma/web/activity_pub/pipeline.ex b/lib/pleroma/web/activity_pub/pipeline.ex
index 377eccb92..400823094 100644
--- a/lib/pleroma/web/activity_pub/pipeline.ex
+++ b/lib/pleroma/web/activity_pub/pipeline.ex
@@ -24,7 +24,7 @@ defmodule Pleroma.Web.ActivityPub.Pipeline do
   @spec common_pipeline(map(), keyword()) ::
           {:ok, Activity.t() | Object.t(), keyword()} | {:error, any()}
   def common_pipeline(object, meta) do
-    case Repo.transaction(fn -> do_common_pipeline(object, meta) end) do
+    case Repo.transaction(fn -> do_common_pipeline(object, meta) end, timeout: :infinity) do
       {:ok, {:ok, activity, meta}} ->
         @side_effects.handle_after_transaction(meta)
         {:ok, activity, meta}

From 9bc69196d5dfbd3fb37c0e62da19ce08fb9bf28d Mon Sep 17 00:00:00 2001
From: Mark Felder <feld@feld.me>
Date: Tue, 20 Apr 2021 11:10:39 -0500
Subject: [PATCH 2/6] Add utility function to return infinite timeout for SQL
 transactions if we detect it was called from a Mix Task

---
 lib/pleroma/utils.ex | 9 +++++++++
 1 file changed, 9 insertions(+)

diff --git a/lib/pleroma/utils.ex b/lib/pleroma/utils.ex
index bc0c95332..5e2fa8bf7 100644
--- a/lib/pleroma/utils.ex
+++ b/lib/pleroma/utils.ex
@@ -63,4 +63,13 @@ defmodule Pleroma.Utils do
   end
 
   def posix_error_message(_), do: ""
+
+  def query_timeout do
+    {parent, _, _, _} = Process.info(self(), :current_stacktrace) |> elem(1) |> Enum.fetch!(2)
+
+    cond do
+      parent |> to_string |> String.starts_with?("Elixir.Mix.Task") -> [timeout: :infinity]
+      true -> [timeout: 15_000]
+    end
+  end
 end

From 9f711ddcf84bdb5a5680e1b55afa83768014906d Mon Sep 17 00:00:00 2001
From: Mark Felder <feld@feld.me>
Date: Tue, 20 Apr 2021 11:16:24 -0500
Subject: [PATCH 3/6] Try to set query timeout intelligently

---
 lib/pleroma/web/activity_pub/pipeline.ex | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/lib/pleroma/web/activity_pub/pipeline.ex b/lib/pleroma/web/activity_pub/pipeline.ex
index 400823094..a0f2e0312 100644
--- a/lib/pleroma/web/activity_pub/pipeline.ex
+++ b/lib/pleroma/web/activity_pub/pipeline.ex
@@ -7,6 +7,7 @@ defmodule Pleroma.Web.ActivityPub.Pipeline do
   alias Pleroma.Config
   alias Pleroma.Object
   alias Pleroma.Repo
+  alias Pleroma.Utils
   alias Pleroma.Web.ActivityPub.ActivityPub
   alias Pleroma.Web.ActivityPub.MRF
   alias Pleroma.Web.ActivityPub.ObjectValidator
@@ -24,7 +25,7 @@ defmodule Pleroma.Web.ActivityPub.Pipeline do
   @spec common_pipeline(map(), keyword()) ::
           {:ok, Activity.t() | Object.t(), keyword()} | {:error, any()}
   def common_pipeline(object, meta) do
-    case Repo.transaction(fn -> do_common_pipeline(object, meta) end, timeout: :infinity) do
+    case Repo.transaction(fn -> do_common_pipeline(object, meta) end, Utils.query_timeout()) do
       {:ok, {:ok, activity, meta}} ->
         @side_effects.handle_after_transaction(meta)
         {:ok, activity, meta}

From 99fd9c5e38ad08973f435f1a67d6af60d004c578 Mon Sep 17 00:00:00 2001
From: Mark Felder <feld@feld.me>
Date: Tue, 20 Apr 2021 12:00:02 -0500
Subject: [PATCH 4/6] OTP releases executing commands via pleroma_ctl show the
 parent of the process is :erl_eval

---
 lib/pleroma/utils.ex | 1 +
 1 file changed, 1 insertion(+)

diff --git a/lib/pleroma/utils.ex b/lib/pleroma/utils.ex
index 5e2fa8bf7..55aecc509 100644
--- a/lib/pleroma/utils.ex
+++ b/lib/pleroma/utils.ex
@@ -69,6 +69,7 @@ defmodule Pleroma.Utils do
 
     cond do
       parent |> to_string |> String.starts_with?("Elixir.Mix.Task") -> [timeout: :infinity]
+      parent == :erl_eval -> [timeout: :infinity]
       true -> [timeout: 15_000]
     end
   end

From 959dc6e6fc95b33700fb7e08689afb701b2877f2 Mon Sep 17 00:00:00 2001
From: Mark Felder <feld@feld.me>
Date: Thu, 22 Apr 2021 10:11:08 -0500
Subject: [PATCH 5/6] Cleanup and ensure we obey custom Repo timeout

---
 lib/pleroma/utils.ex | 11 ++++++++++-
 1 file changed, 10 insertions(+), 1 deletion(-)

diff --git a/lib/pleroma/utils.ex b/lib/pleroma/utils.ex
index 55aecc509..a446d3ae6 100644
--- a/lib/pleroma/utils.ex
+++ b/lib/pleroma/utils.ex
@@ -11,6 +11,8 @@ defmodule Pleroma.Utils do
     eperm epipe erange erofs espipe esrch estale etxtbsy exdev
   )a
 
+  @repo_timeout Pleroma.Config.get([Pleroma.Repo, :timeout], 15_000)
+
   def compile_dir(dir) when is_binary(dir) do
     dir
     |> File.ls!()
@@ -64,13 +66,20 @@ defmodule Pleroma.Utils do
 
   def posix_error_message(_), do: ""
 
+  @doc """
+  Returns [timeout: integer] suitable for passing as an option to Repo functions.
+
+  This function detects if the execution was triggered from IEx shell, Mix task, or
+  ./bin/pleroma_ctl and sets the timeout to :infinity, else returns the default timeout value.
+  """
+  @spec query_timeout() :: [timeout: integer]
   def query_timeout do
     {parent, _, _, _} = Process.info(self(), :current_stacktrace) |> elem(1) |> Enum.fetch!(2)
 
     cond do
       parent |> to_string |> String.starts_with?("Elixir.Mix.Task") -> [timeout: :infinity]
       parent == :erl_eval -> [timeout: :infinity]
-      true -> [timeout: 15_000]
+      true -> [timeout: @repo_timeout]
     end
   end
 end

From d7a71a275abea6286ee116d092ddc9440a9419a5 Mon Sep 17 00:00:00 2001
From: Mark Felder <feld@feld.me>
Date: Thu, 22 Apr 2021 10:15:05 -0500
Subject: [PATCH 6/6] Fixed pleroma.user delete_activities mix task.

---
 CHANGELOG.md | 1 +
 1 file changed, 1 insertion(+)

diff --git a/CHANGELOG.md b/CHANGELOG.md
index bfa76a89a..a1173414d 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -23,6 +23,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
 - Uploading custom instance thumbnail via AdminAPI/AdminFE generated invalid URL to the image
 - Applying ConcurrentLimiter settings via AdminAPI
 - User login failures if their `notification_settings` were in a NULL state.
+- Mix task `pleroma.user delete_activities` query transaction timeout is now :infinity
 
 ## [2.3.0] - 2020-03-01