]> git.ipfire.org Git - thirdparty/patchwork.git/commit
parsemail: fix SeriesReference race with concurrent delivery
authorRobin Jarry <robin@jarry.cc>
Mon, 8 Jun 2026 12:00:55 +0000 (14:00 +0200)
committerStephen Finucane <stephen@that.guru>
Wed, 10 Jun 2026 12:44:57 +0000 (13:44 +0100)
commit481adeb0606f3867de71755c39d0ee10f1d264bb
treed781e3f47253a01a267d5ae6dbe0f50c5298e13a
parent6ab05c907d21d51c0ced4c81a91587840e3f7c8c
parsemail: fix SeriesReference race with concurrent delivery

When multiple parsemail processes run in parallel (e.g. postfix
delivering several messages from the same series at once), two
processes can try to create a SeriesReference for the same msgid
simultaneously. The second one fails with an IntegrityError:

  django.db.utils.IntegrityError: duplicate key value violates
  unique constraint "patchwork_seriesreference_project_id_msgid_..."
  DETAIL: Key (project_id, msgid)=(2, <...>) already exists.

This can result in incomplete series that never reach the
"received_all" state because the failed parsemail invocation
prevents one of the patches from being recorded.

The existing get/create pattern has a classic TOCTOU race: the get
succeeds (no reference found), but by the time create runs, another
process has already inserted the row. Replace both the try/get/
except/create block and the bare create call with get_or_create
which handles the race atomically at the database level.

Signed-off-by: Robin Jarry <robin@jarry.cc>
Reviwed-by: Stephen Finucane <stephen@that.guru>
(cherry picked from commit 3558907533a1a420ccceec6935635e7c2245eb69)
patchwork/parser.py
releasenotes/notes/parsemail-race-fix-e5f6g7h8i9j0k1l2.yaml [new file with mode: 0644]