zulip/zerver/actions
Zixuan James Li a081428ad2 user_groups: Make locks required for updating user group memberships.
**Background**

User groups are expected to comply with the DAG constraint for the
many-to-many inter-group membership. The check for this constraint has
to be performed recursively so that we can find all direct and indirect
subgroups of the user group to be added.

This kind of check is vulnerable to phantom reads which is possible at
the default read committed isolation level because we cannot guarantee
that the check is still valid when we are adding the subgroups to the
user group.

**Solution**

To avoid having another transaction concurrently update one of the
to-be-subgroup after the recursive check is done, and before the subgroup
is added, we use SELECT FOR UPDATE to lock the user group rows.

The lock needs to be acquired before a group membership change is about
to occur before any check has been conducted.

Suppose that we are adding subgroup B to supergroup A, the locking protocol
is specified as follows:

1. Acquire a lock for B and all its direct and indirect subgroups.
2. Acquire a lock for A.

For the removal of user groups, we acquire a lock for the user group to
be removed with all its direct and indirect subgroups. This is the special
case A=B, which is still complaint with the protocol.

**Error handling**

We currently rely on Postgres' deadlock detection to abort transactions
and show an error for the users. In the future, we might need some
recovery mechanism or at least better error handling.

**Notes**

An important note is that we need to reuse the recursive CTE query that
finds the direct and indirect subgroups when applying the lock on the
rows. And the lock needs to be acquired the same way for the addition and
removal of direct subgroups.

User membership change (as opposed to user group membership) is not
affected. Read-only queries aren't either. The locks only protect
critical regions where the user group dependency graph might violate
the DAG constraint, where users are not participating.

**Testing**

We implement a transaction test case targeting some typical scenarios
when an internal server error is expected to happen (this means that the
user group view makes the correct decision to abort the transaction when
something goes wrong with locks).

To achieve this, we add a development view intended only for unit tests.
It has a global BARRIER that can be shared across threads, so that we
can synchronize them to consistently reproduce certain potential race
conditions prevented by the database locks.

The transaction test case lanuches pairs of threads initiating possibly
conflicting requests at the same time. The tests are set up such that exactly N
of them are expected to succeed with a certain error message (while we don't
know each one).

**Security notes**

get_recursive_subgroups_for_groups will no longer fetch user groups from
other realms. As a result, trying to add/remove a subgroup from another
realm results in a UserGroup not found error response.

We also implement subgroup-specific checks in has_user_group_access to
keep permission managing in a single place. Do note that the API
currently don't have a way to violate that check because we are only
checking the realm ID now.
2023-08-24 17:21:08 -07:00
..
__init__.py
alert_words.py actions: Split out zerver.actions.alert_words. 2022-04-14 17:14:31 -07:00
bots.py migration: Rename extra_data_json to extra_data in audit log models. 2023-08-16 17:18:14 -07:00
create_realm.py migration: Rename extra_data_json to extra_data in audit log models. 2023-08-16 17:18:14 -07:00
create_user.py migration: Rename extra_data_json to extra_data in audit log models. 2023-08-16 17:18:14 -07:00
custom_profile_fields.py ruff: Fix PLR1714 Consider merging multiple comparisons. 2023-07-23 15:21:33 -07:00
default_streams.py python: Convert translated positional {} fields to {named} fields. 2023-07-18 15:19:07 -07:00
hotspots.py actions: Split out zerver.actions.hotspots. 2022-04-14 17:14:31 -07:00
invites.py emails: Add corporate_enabled to context for emails. 2023-05-01 10:32:43 -07:00
message_delete.py zerver: Update comments with "private message" or "PM". 2023-06-23 11:24:13 -07:00
message_edit.py message: Pass realm as argument to wildcard_mention_allowed. 2023-08-23 11:38:32 -07:00
message_flags.py python: Convert translated positional {} fields to {named} fields. 2023-07-18 15:19:07 -07:00
message_send.py message: Pass realm as argument to wildcard_mention_allowed. 2023-08-23 11:38:32 -07:00
muted_users.py migration: Rename extra_data_json to extra_data in audit log models. 2023-08-16 17:18:14 -07:00
presence.py presence: Support null values in UserPresence. 2023-04-26 14:26:47 -07:00
reactions.py message: Access realm object directly from message. 2023-08-23 11:38:32 -07:00
realm_domains.py migration: Rename extra_data_json to extra_data in audit log models. 2023-08-16 17:18:14 -07:00
realm_emoji.py migration: Rename extra_data_json to extra_data in audit log models. 2023-08-16 17:18:14 -07:00
realm_export.py migration: Rename extra_data_json to extra_data in audit log models. 2023-08-16 17:18:14 -07:00
realm_icon.py migration: Rename extra_data_json to extra_data in audit log models. 2023-08-16 17:18:14 -07:00
realm_linkifiers.py migration: Rename extra_data_json to extra_data in audit log models. 2023-08-16 17:18:14 -07:00
realm_logo.py django_api: Extract send_event_on_commit helper. 2023-06-19 13:42:40 -07:00
realm_playgrounds.py migration: Rename extra_data_json to extra_data in audit log models. 2023-08-16 17:18:14 -07:00
realm_settings.py migration: Rename extra_data_json to extra_data in audit log models. 2023-08-16 17:18:14 -07:00
scheduled_messages.py scheduled-messages: Split out edit scheduled message endpoint. 2023-05-26 18:05:55 -07:00
streams.py streams: Don't compute traffic data for sub objects in zephyr realm. 2023-08-21 15:21:58 -07:00
submessage.py django_api: Extract send_event_on_commit helper. 2023-06-19 13:42:40 -07:00
typing.py python: Convert translated positional {} fields to {named} fields. 2023-07-18 15:19:07 -07:00
uploads.py uploads: Rewrite comment in do_claim_attachments to improve clarity. 2023-08-15 10:08:11 -07:00
user_activity.py Remove statsd support. 2023-04-25 19:58:16 -07:00
user_groups.py user_groups: Make locks required for updating user group memberships. 2023-08-24 17:21:08 -07:00
user_settings.py migration: Rename extra_data_json to extra_data in audit log models. 2023-08-16 17:18:14 -07:00
user_status.py user-status: Stop updating the UserStatus model for `away` updates. 2022-09-23 12:27:54 -07:00
user_topics.py user_topics: Update 'set visibility policy' codepath for bulk db operations. 2023-03-29 09:50:06 -07:00
users.py migration: Rename extra_data_json to extra_data in audit log models. 2023-08-16 17:18:14 -07:00
video_calls.py