Guard discovery skip table against shutdown race - #20
Open
ream88 wants to merge 1 commit into
Open
Conversation
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
There was a problem hiding this comment.
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 ignoresArgumentError) 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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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:
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.