<p dir="auto">As discussed in <a class="issue-link js-issue-link" data-error-text="Failed to load title" data-id="2141346072" data-permission-text="Title is private" data-url="https://github.com/openstreetmap/openstreetmap-website/issues/4528" data-hovercard-type="issue" data-hovercard-url="/openstreetmap/openstreetmap-website/issues/4528/hovercard" href="https://github.com/openstreetmap/openstreetmap-website/issues/4528">#4528</a>, I'm proposing to add some note id locking as described here: <a href="https://api.rubyonrails.org/v7.1.3.4/classes/ActiveRecord/Locking/Pessimistic.html" rel="nofollow">https://api.rubyonrails.org/v7.1.3.4/classes/ActiveRecord/Locking/Pessimistic.html</a> (first two examples).</p>
<p dir="auto">The change covers destroy, comment, close and reopen methods, since all of them are evaluating a note's visible and/or closed flag.</p>
<p dir="auto">The important change is the "FOR UPDATE" addition when reading a note id initially. This way, two parallel processes cannot read the same note id at the same time and then both decide that e.g. reopening is permitted.</p>
<pre class="notranslate"><code class="notranslate"> TRANSACTION (0.1ms) BEGIN
↳ app/controllers/api/notes_controller.rb:208:in `block in reopen'
Note Load (14.7ms) SELECT "notes".* FROM "notes" WHERE "notes"."id" = $1 LIMIT $2 FOR UPDATE [["id", 3], ["LIMIT", 1]]
[...]
Note Update (0.3ms) UPDATE "notes" SET "updated_at" = $1, "status" = $2, "closed_at" = $3 WHERE "notes"."id" = $4 [["updated_at", "2024-08-06 17:46:42.692378"], ["status", "open"], ["closed_at", nil], ["id", 3]]
↳ app/models/note.rb:58:in `reopen'
[...]
TRANSACTION (1.6ms) COMMIT
</code></pre>
<p dir="auto"><span class="issue-keyword tooltipped tooltipped-se" aria-label="This pull request closes issue #4528.">Fixes</span> <a class="issue-link js-issue-link" data-error-text="Failed to load title" data-id="2141346072" data-permission-text="Title is private" data-url="https://github.com/openstreetmap/openstreetmap-website/issues/4528" data-hovercard-type="issue" data-hovercard-url="/openstreetmap/openstreetmap-website/issues/4528/hovercard" href="https://github.com/openstreetmap/openstreetmap-website/issues/4528">#4528</a></p>
<hr>
<h4>You can view, comment on, or merge this pull request online at:</h4>
<p> <a href='https://github.com/openstreetmap/openstreetmap-website/pull/5053'>https://github.com/openstreetmap/openstreetmap-website/pull/5053</a></p>
<h4>Commit Summary</h4>
<ul>
<li><a href="https://github.com/openstreetmap/openstreetmap-website/pull/5053/commits/5b707ae5ea3cd7397d723a9059d3fdcfebb88397" class="commit-link">5b707ae</a> Lock note during status update to avoid race condition</li>
</ul>
<h4 style="display: inline-block">File Changes </h4> <p style="display: inline-block">(<a href="https://github.com/openstreetmap/openstreetmap-website/pull/5053/files">1 file</a>)</p>
<ul>
<li>
<strong>M</strong>
<a href="https://github.com/openstreetmap/openstreetmap-website/pull/5053/files#diff-6e9fe051c0ef757618e376ffe731dfca8d4960ef0838b7d6a05609b46ba0fad1">app/controllers/api/notes_controller.rb</a>
(46)
</li>
</ul>
<h4>Patch Links:</h4>
<ul>
<li><a href='https://github.com/openstreetmap/openstreetmap-website/pull/5053.patch'>https://github.com/openstreetmap/openstreetmap-website/pull/5053.patch</a></li>
<li><a href='https://github.com/openstreetmap/openstreetmap-website/pull/5053.diff'>https://github.com/openstreetmap/openstreetmap-website/pull/5053.diff</a></li>
</ul>
<p style="font-size:small;-webkit-text-size-adjust:none;color:#666;">—<br />Reply to this email directly, <a href="https://github.com/openstreetmap/openstreetmap-website/pull/5053">view it on GitHub</a>, or <a href="https://github.com/notifications/unsubscribe-auth/AAK2OLKWT7IF5OQZYU4AEHLZQEKKDAVCNFSM6AAAAABMC4YHKSVHI2DSMVQWIX3LMV43ASLTON2WKOZSGQ2TCNBYGMZTKMY">unsubscribe</a>.<br />You are receiving this because you are subscribed to this thread.<img src="https://github.com/notifications/beacon/AAK2OLNY52RDPRASPEENZ6TZQEKKDA5CNFSM6AAAAABMC4YHKSWGG33NNVSW45C7OR4XAZNFJFZXG5LFVJRW63LNMVXHIX3JMTHJEHVK3E.gif" height="1" width="1" alt="" /><span style="color: transparent; font-size: 0; display: none; visibility: hidden; overflow: hidden; opacity: 0; width: 0; height: 0; max-width: 0; max-height: 0; mso-hide: all">Message ID: <span><openstreetmap/openstreetmap-website/pull/5053</span><span>@</span><span>github</span><span>.</span><span>com></span></span></p>
<script type="application/ld+json">[
{
"@context": "http://schema.org",
"@type": "EmailMessage",
"potentialAction": {
"@type": "ViewAction",
"target": "https://github.com/openstreetmap/openstreetmap-website/pull/5053",
"url": "https://github.com/openstreetmap/openstreetmap-website/pull/5053",
"name": "View Pull Request"
},
"description": "View this Pull Request on GitHub",
"publisher": {
"@type": "Organization",
"name": "GitHub",
"url": "https://github.com"
}
}
]</script>