zulip/zerver
Alex Vandiver 003fa7adda message_edit: Lock the Message row in check_update_message.
Fundamentally, we should take a write lock on the message, check its
validity for a change, and then make and commit that change.
Previously, `check_update_message` did not operate in a transaction,
but `do_update_message` did -- which led to the ordering:

 - `check_update_message` reads Message, not in a transaction
 - `check_update_message` verifies properties of the Message
 - `do_update_message` starts a transaction
 - `do_update_message` takes a read lock on UserMessage
 - `do_update_message` writes on UserMessage
 - `do_update_message` writes Message
 - `do_update_message` commits

This leads to race conditions, where the `check_update_message` may
have verified based on stale data, and `do_update_message` may
improperly overwrite it; as well as deadlocks, where
other (properly-written) codepaths take a write lock on Message
_before_ updating UserMessage, and thus deadlock with
`do_update_message`.

Change `check_update_message` to open a transaction, and take the
write lock when first accessing the Message row.  We update the
comment above `do_update_message` to clarify this expectation.

The new ordering is thus:

 - `check_update_message` starts a transaction
 - `check_update_message` takes a write lock on Message
 - `check_update_message` verifies properties of the Message
 - `do_update_message` writes on UserMessage
 - `do_update_message` writes Message
 - `check_update_message` commits
2023-07-17 10:53:38 -07:00
..
actions message_edit: Lock the Message row in check_update_message. 2023-07-17 10:53:38 -07:00
data_import emojis: Use get_emoji_data. 2023-07-17 09:35:53 -07:00
integration_fixtures/nagios
lib mention: Soft-reactivate users receiving @topic mention notifications. 2023-07-17 09:39:24 -07:00
management soft activation: Avoid QuerySet and use List instead. 2023-07-13 08:09:14 -07:00
migrations mention: Send notifications for @topic wildcard mentions. 2023-07-17 09:39:24 -07:00
openapi streams: Send stream creation events when subscribing guests. 2023-07-13 14:04:51 -07:00
tests mention: Soft-reactivate users receiving @topic mention notifications. 2023-07-17 09:39:24 -07:00
tornado mention: Send notifications for @topic wildcard mentions. 2023-07-17 09:39:24 -07:00
transaction_tests test_classes: Do not necessary wrap test cases in a transaction. 2023-07-06 11:44:50 -07:00
views emojis: Use get_emoji_data. 2023-07-17 09:35:53 -07:00
webhooks integrations: Replace use of 'subject' to 'topic'. 2023-07-17 10:35:51 -07:00
worker missed-message: Lock ScheduledMessageNotificationEmail rows. 2023-07-13 11:50:42 -07:00
__init__.py
apps.py requirements: Upgrade Python requirements. 2023-04-03 22:39:21 -07:00
context_processors.py emails: Add corporate_enabled to context for emails. 2023-05-01 10:32:43 -07:00
decorator.py logging_util: Remove dependence on get_current_request. 2023-07-11 22:23:47 -07:00
filters.py
forms.py accounts: Allow user to change email visibility during first login. 2023-05-16 13:52:56 -07:00
logging_handlers.py python: Use Python 3.8 typing.{Protocol,TypedDict}. 2022-04-27 12:57:49 -07:00
middleware.py logging_util: Remove dependence on get_current_request. 2023-07-11 22:23:47 -07:00
models.py mention: Send notifications for @topic wildcard mentions. 2023-07-17 09:39:24 -07:00
signals.py requirements: Upgrade to Django 4.0. 2022-07-13 16:07:17 -07:00