Mogelijke Race Condition bij het bewerken van een bericht

AI-generated summary

The discussion addresses a potential race condition when editing a post in Discourse, where concurrent edits can corrupt the quoted_posts relationship, leading to issues like failed avatar rebakes. concerto explains that the non-atomic check in PostRevisor#revise! allows one edit to delete quoted_posts while another adds them, causing data inconsistency. A possible fix involves using a Redis lock to prevent concurrent edits, though sam cautions about avoiding deadlocks and suggests considering shared editing as an alternative.

Problem

Concurrent edits to one wiki page can lead to unexpected behavior. In my case, the quoted_posts relationship between two post becomes wrong.

This may leads to other problems when quoted_posts is used, like in User#refresh_avatar. The missing quoted_posts makes it unable to rebake all quoted posts.

Possible Cause

The warning introduced by this commit is not enough to safeguard concurrent edit to a post, as the check is not atomic. This is a rare situation but it still can happen.

PostRevisor#revise! calls QuotedPost.extract_from(@post) , which delete all quoted_posts if there are no any quotes in a post. When two edit calls PostRevisor#revise! simultaneously, with one edit to add a quoted post and another to add a quoted post, the post content could end up with the edit that has quotes and the quoted_post relationship destroyed by the edit without any quotes.

Possible Fix

Maybe we should consider acquiring a Redis lock when editing a post.

3 likes

This is possible I guess and I am not strongly against adding a distributed lock here. The tricky thing is that I don’t want to risk any deadlocks so we would have to be extremely careful.

If concurrent editing is really your thing why not try shared edits?

3 likes