Skip to content

Commit

Permalink
Lock the journal insertion lock earlier in the process (#16025)
Browse files Browse the repository at this point in the history
  • Loading branch information
dstufft authored May 30, 2024
1 parent eabc81d commit 4d077ff
Showing 1 changed file with 12 additions and 9 deletions.
21 changes: 12 additions & 9 deletions warehouse/packaging/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -871,8 +871,8 @@ def __table_args__(cls): # noqa
submitted_by: Mapped[User] = orm.relationship(lazy="raise_on_sql")


@db.listens_for(JournalEntry, "before_insert")
def ensure_monotonic_journals(config, mapper, connection, target):
@db.listens_for(db.Session, "before_flush")
def ensure_monotonic_journals(config, session, flush_context, instances):
# We rely on `journals.id` to be a monotonically increasing integer,
# however the way that SERIAL is implemented, it does not guarentee
# that is the case.
Expand All @@ -885,14 +885,17 @@ def ensure_monotonic_journals(config, mapper, connection, target):
# The way this works, not even the SERIALIZABLE transaction types give
# us this property. Instead we have to implement our own locking that
# ensures that each new journal entry will be serialized.
connection.execute(
select(
func.pg_advisory_xact_lock(
cast(cast(target.__tablename__, REGCLASS), Integer),
_MONOTONIC_SEQUENCE,
for obj in session.new:
if isinstance(obj, JournalEntry):
session.execute(
select(
func.pg_advisory_xact_lock(
cast(cast(JournalEntry.__tablename__, REGCLASS), Integer),
_MONOTONIC_SEQUENCE,
)
)
)
)
)
return


class ProhibitedProjectName(db.Model):
Expand Down

0 comments on commit 4d077ff

Please sign in to comment.