Skip to content
This repository was archived by the owner on Apr 26, 2024. It is now read-only.

Commit 4adaba9

Browse files
authored
Fix rare deadlock when using read/write locks (#16133)
1 parent 7cd79ce commit 4adaba9

File tree

2 files changed

+38
-0
lines changed

2 files changed

+38
-0
lines changed

changelog.d/16133.bugfix

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fix a rare race that could block new events from being sent for up to two minutes. Introduced in v1.90.0.
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/* Copyright 2023 The Matrix.org Foundation C.I.C
2+
*
3+
* Licensed under the Apache License, Version 2.0 (the "License");
4+
* you may not use this file except in compliance with the License.
5+
* You may obtain a copy of the License at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software
10+
* distributed under the License is distributed on an "AS IS" BASIS,
11+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
* See the License for the specific language governing permissions and
13+
* limitations under the License.
14+
*/
15+
16+
-- To avoid the possibility of a deadlock, lock the
17+
-- `worker_read_write_locks_mode` table so that we serialize inserts/deletes
18+
-- for a specific lock name/key.
19+
20+
CREATE OR REPLACE FUNCTION delete_read_write_lock_parent_before() RETURNS trigger AS $$
21+
BEGIN
22+
-- `PERFORM` is a `SELECT` which discards the rows.
23+
PERFORM * FROM worker_read_write_locks_mode
24+
WHERE
25+
lock_name = OLD.lock_name
26+
AND lock_key = OLD.lock_key
27+
FOR UPDATE;
28+
29+
RETURN OLD;
30+
END
31+
$$
32+
LANGUAGE plpgsql;
33+
34+
DROP TRIGGER IF EXISTS delete_read_write_lock_parent_before_trigger ON worker_read_write_locks;
35+
CREATE TRIGGER delete_read_write_lock_parent_before_trigger BEFORE DELETE ON worker_read_write_locks
36+
FOR EACH ROW
37+
EXECUTE PROCEDURE delete_read_write_lock_parent_before();

0 commit comments

Comments
 (0)