From 0d6dd343dee916c9d446e7c3e43aeca3c466f22c Mon Sep 17 00:00:00 2001 From: Michael Opdenacker Date: Sat, 11 May 2024 16:31:32 +0530 Subject: bitbake: prserv: avoid possible race condition in database code Remove a possible race condition by allowing a read-only server to create the PR table anyway. This avoids a failure if both a read-only and read-write server try to access an empty database at the same time. (Bitbake rev: b171caec5ebbe579bf4b8b2005930240ae5c8ce2) Signed-off-by: Michael Opdenacker Suggested-by: Joshua Watt Cc: Tim Orling Cc: Thomas Petazzoni Signed-off-by: Richard Purdie --- bitbake/lib/prserv/db.py | 25 +++++++++++-------------- 1 file changed, 11 insertions(+), 14 deletions(-) (limited to 'bitbake/lib/prserv/db.py') diff --git a/bitbake/lib/prserv/db.py b/bitbake/lib/prserv/db.py index f430586d73..79c9001bf5 100644 --- a/bitbake/lib/prserv/db.py +++ b/bitbake/lib/prserv/db.py @@ -30,21 +30,18 @@ class PRTable(object): self.read_only = read_only self.table = table + # Creating the table even if the server is read-only. + # This avoids a race condition if a shared database + # is accessed by a read-only server first. + with closing(self.conn.cursor()) as cursor: - if self.read_only: - table_exists = cursor.execute( - "SELECT count(*) FROM sqlite_master \ - WHERE type='table' AND name='%s'" % (self.table)) - if not table_exists: - raise prserv.NotFoundError - else: - cursor.execute("CREATE TABLE IF NOT EXISTS %s \ - (version TEXT NOT NULL, \ - pkgarch TEXT NOT NULL, \ - checksum TEXT NOT NULL, \ - value TEXT, \ - PRIMARY KEY (version, pkgarch, checksum, value));" % self.table) - self.conn.commit() + cursor.execute("CREATE TABLE IF NOT EXISTS %s \ + (version TEXT NOT NULL, \ + pkgarch TEXT NOT NULL, \ + checksum TEXT NOT NULL, \ + value TEXT, \ + PRIMARY KEY (version, pkgarch, checksum, value));" % self.table) + self.conn.commit() def _extremum_value(self, rows, is_max): value = None -- cgit v1.2.3-54-g00ecf