Skip to content

Commit d31116a

Browse files
committed
change supervisor strategy
1 parent df9e53b commit d31116a

File tree

3 files changed

+17
-4
lines changed

3 files changed

+17
-4
lines changed

packages/sync-service/lib/electric/shape_cache/shape_status/shape_db/supervisor.ex

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,12 +65,16 @@ defmodule Electric.ShapeCache.ShapeStatus.ShapeDb.Supervisor do
6565
lazy: not exclusive_mode},
6666
id: {:pool, :write}
6767
),
68-
# write buffer for batching SQLite writes to avoid timeout cascades
68+
# Write buffer for batching SQLite writes to avoid timeout cascades.
6969
{ShapeDb.WriteBuffer, opts},
7070
{Task, fn -> ShapeDb.Statistics.initialize(stack_id) end}
7171
]
7272
])
7373

74-
Supervisor.init(children, strategy: :one_for_one)
74+
# Because the full state of the system is split between the actual db, the
75+
# writeBuffer and the ShapeStatus ets caches, we are not safe to adopt a
76+
# one_for_one strategy and need to propagate an exit in the children of
77+
# this supervisor to the parent
78+
Supervisor.init(children, strategy: :one_for_all, max_restarts: 0)
7579
end
7680
end

packages/sync-service/lib/electric/shape_cache/shape_status/shape_db/write_buffer.ex

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -274,8 +274,6 @@ defmodule Electric.ShapeCache.ShapeStatus.ShapeDb.WriteBuffer do
274274

275275
@impl GenServer
276276
def init(opts) do
277-
Process.flag(:trap_exit, true)
278-
279277
stack_id = Keyword.fetch!(opts, :stack_id)
280278
manual_flush_only = Keyword.get(opts, :manual_flush_only, false)
281279

packages/sync-service/test/electric/shape_cache/shape_status/shape_db_test.exs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -657,6 +657,17 @@ defmodule Electric.ShapeCache.ShapeStatus.ShapeDbTest do
657657
end)
658658
end
659659
end
660+
661+
test "crashing WriteBuffer restarts entire supervision tree", ctx do
662+
assert supervisor_pid = GenServer.whereis(ShapeDb.Supervisor.name(ctx.stack_id))
663+
super_ref = Process.monitor(supervisor_pid)
664+
assert write_buffer_pid = GenServer.whereis(ShapeDb.WriteBuffer.name(ctx.stack_id))
665+
666+
buffer_ref = Process.monitor(write_buffer_pid)
667+
Process.exit(write_buffer_pid, :some_reason)
668+
assert_receive {:DOWN, ^buffer_ref, :process, ^write_buffer_pid, :some_reason}
669+
assert_receive {:DOWN, ^super_ref, :process, ^supervisor_pid, _}
670+
end
660671
end
661672

662673
describe "pool scaling" do

0 commit comments

Comments
 (0)