Skip to content

Guard discovery skip table against shutdown race - #20

Open
ream88 wants to merge 1 commit into
phoenixframework:mainfrom
ream88:guard-discovery-skip-table
Open

Guard discovery skip table against shutdown race#20
ream88 wants to merge 1 commit into
phoenixframework:mainfrom
ream88:guard-discovery-skip-table

Conversation

@ream88

@ream88 ream88 commented Aug 26, 2026

Copy link
Copy Markdown

The discovery skip table is a named ETS table created in LifecycleManager.init/1, so it is owned by the manager process and is deleted when that process stops. Discovery itself runs in a Task.Supervisor.async_nolink/2 task that is not linked to the manager, and the Task.Supervisor is a sibling started before the manager in a :one_for_all tree, so it is still alive while the manager terminates. terminate/2 does not cancel the in-flight task either.

When the manager stops mid-round the orphaned discovery workers keep running and raise on the next skip-table write:

** (ArgumentError) errors were found at the given arguments:
  * 1st argument: the table identifier does not refer to an existing ETS table (stdlib 8.0) :ets.insert(:"durable_server_discovery_skip_...", {...}) (durable_server 0.1.4) lib/durable_server/lifecycle_manager.ex:2079: anonymous fn/3 in DurableServer.LifecycleManager.discover_and_restart_servers/1 (elixir 1.20.0) lib/task/supervised.ex:105: Task.Supervised.invoke_mfa/2 (elixir 1.20.0) lib/task/supervised.ex:40: Task.Supervised.reply/4

Guard the two writes the same way discovery_diag_snapshot/1 already guards the diagnostics table, and add a regression test that blocks a discovery worker inside get_object/3, kills the manager, then releases the worker.

The discovery skip table is a named ETS table created in
LifecycleManager.init/1, so it is owned by the manager process and is
deleted when that process stops. Discovery itself runs in a
Task.Supervisor.async_nolink/2 task that is not linked to the manager,
and the Task.Supervisor is a sibling started *before* the manager in a
:one_for_all tree, so it is still alive while the manager terminates.
terminate/2 does not cancel the in-flight task either.

When the manager stops mid-round the orphaned discovery workers keep
running and raise on the next skip-table write:

    ** (ArgumentError) errors were found at the given arguments:
      * 1st argument: the table identifier does not refer to an existing ETS table
        (stdlib 8.0) :ets.insert(:"durable_server_discovery_skip_...", {...})
        (durable_server 0.1.4) lib/durable_server/lifecycle_manager.ex:2079: anonymous fn/3 in DurableServer.LifecycleManager.discover_and_restart_servers/1
        (elixir 1.20.0) lib/task/supervised.ex:105: Task.Supervised.invoke_mfa/2
        (elixir 1.20.0) lib/task/supervised.ex:40: Task.Supervised.reply/4

Guard the two writes the same way discovery_diag_snapshot/1 already
guards the diagnostics table, and add a regression test that blocks a
discovery worker inside get_object/3, kills the manager, then releases
the worker.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01JfS2xUh9gRc4ymCVesKGkW
Copilot AI lite review requested due to automatic review settings August 26, 2026 09:32

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR fixes a shutdown race where an orphaned discovery task can outlive DurableServer.LifecycleManager and attempt to write to the manager-owned, named ETS “discovery skip” table after it has been deleted, causing :ets.insert/2 to raise.

Changes:

  • Wrap discovery skip-table writes behind a guard (skip_cache_put/2) that checks table existence (and safely ignores ArgumentError) before inserting.
  • Add a regression test that blocks a discovery worker inside get_object/3, kills the manager mid-round, then unblocks the worker to ensure no crash occurs on skip-table writes.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.

File Description
lib/durable_server/lifecycle_manager.ex Replaces direct :ets.insert/2 calls with skip_cache_put/2 to avoid crashes when the skip table is deleted during shutdown.
test/durable_server/lifecycle_test.exs Adds a PauseOnFetchBackend and a regression test reproducing the shutdown race to validate the guard.

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants