Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
synapse
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Container registry
Model registry
Monitor
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Maunium
synapse
Commits
18279631
Unverified
Commit
18279631
authored
1 year ago
by
Erik Johnston
Committed by
GitHub
1 year ago
Browse files
Options
Downloads
Patches
Plain Diff
Fix rare deadlock when using read/write locks (#16169)
parent
85118420
Branches
Branches containing commit
Tags
Tags containing commit
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
changelog.d/16169.bugfix
+1
-0
1 addition, 0 deletions
changelog.d/16169.bugfix
synapse/storage/schema/main/delta/80/04_read_write_locks_deadlock.sql.postgres
+71
-0
71 additions, 0 deletions
...a/main/delta/80/04_read_write_locks_deadlock.sql.postgres
with
72 additions
and
0 deletions
changelog.d/16169.bugfix
0 → 100644
+
1
−
0
View file @
18279631
Fix a rare race that could block new events from being sent for up to two minutes. Introduced in v1.90.0.
This diff is collapsed.
Click to expand it.
synapse/storage/schema/main/delta/80/0
2
_read_write_locks_deadlock.sql.postgres
→
synapse/storage/schema/main/delta/80/0
4
_read_write_locks_deadlock.sql.postgres
+
71
−
0
View file @
18279631
...
...
@@ -13,25 +13,59 @@
* limitations under the License.
*/
-- To avoid the possibility of a deadlock, lock the
-- `worker_read_write_locks_mode` table so that we serialize inserts/deletes
-- for a specific lock name/key.
CREATE OR REPLACE FUNCTION delete_read_write_lock_parent_before() RETURNS trigger AS $$
-- Remove a previous attempt to avoid deadlocks
DROP TRIGGER IF EXISTS delete_read_write_lock_parent_before_trigger ON worker_read_write_locks;
DROP FUNCTION IF EXISTS delete_read_write_lock_parent_before;
-- Ensure that we keep `worker_read_write_locks_mode` up to date whenever a lock
-- is released (i.e. a row deleted from `worker_read_write_locks`). Either we
-- update the `worker_read_write_locks_mode.token` to match another instance
-- that has currently acquired the lock, or we delete the row if nobody has
-- currently acquired a lock.
CREATE OR REPLACE FUNCTION delete_read_write_lock_parent() RETURNS trigger AS $$
DECLARE
new_token TEXT;
mode_row_token TEXT;
BEGIN
-- `PERFORM` is a `SELECT` which discards the rows.
PERFORM * FROM worker_read_write_locks_mode
-- Only update the token in `_mode` if its our token. This prevents
-- deadlocks.
--
-- We shove the token into `mode_row_token`, as otherwise postgres complains
-- we're not using the returned data.
SELECT token INTO mode_row_token FROM worker_read_write_locks_mode
WHERE
lock_name = OLD.lock_name
AND lock_key = OLD.lock_key
AND token = OLD.token
FOR UPDATE;
RETURN OLD;
IF NOT FOUND THEN
RETURN NEW;
END IF;
SELECT token INTO new_token FROM worker_read_write_locks
WHERE
lock_name = OLD.lock_name
AND lock_key = OLD.lock_key
LIMIT 1 FOR UPDATE SKIP LOCKED;
IF NOT FOUND THEN
DELETE FROM worker_read_write_locks_mode
WHERE lock_name = OLD.lock_name AND lock_key = OLD.lock_key AND token = OLD.token;
ELSE
UPDATE worker_read_write_locks_mode
SET token = new_token
WHERE lock_name = OLD.lock_name AND lock_key = OLD.lock_key;
END IF;
RETURN NEW;
END
$$
LANGUAGE plpgsql;
DROP TRIGGER IF EXISTS delete_read_write_lock_parent_
before_
trigger ON worker_read_write_locks;
CREATE TRIGGER delete_read_write_lock_parent_
before_
trigger
BEFORE
DELETE ON worker_read_write_locks
DROP TRIGGER IF EXISTS delete_read_write_lock_parent_trigger ON worker_read_write_locks;
CREATE TRIGGER delete_read_write_lock_parent_trigger
AFTER
DELETE ON worker_read_write_locks
FOR EACH ROW
EXECUTE PROCEDURE delete_read_write_lock_parent
_before
();
EXECUTE PROCEDURE delete_read_write_lock_parent();
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment