Skip to content

Commit 177fb4c

Browse files
RADXIshanstvml
authored andcommitted
Update cvedb.py
1 parent 059094c commit 177fb4c

File tree

1 file changed

+25
-5
lines changed

1 file changed

+25
-5
lines changed

cve_bin_tool/cvedb.py

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -766,27 +766,47 @@ def update_vendors(self, cve_data):
766766
return updated_severity, updated_affected
767767

768768
def db_open_and_get_cursor(self) -> sqlite3.Cursor:
769-
"""Opens connection to SQLite database with retry logic to handle 'database is locked' errors."""
769+
"""Opens connection to SQLite database with retry logic and cursor existence check."""
770+
771+
# If we already have a valid connection, try to use it
772+
if self.connection:
773+
try:
774+
cursor = self.connection.cursor()
775+
if cursor is None:
776+
LOGGER.error("Database cursor does not exist")
777+
raise CVEDBError("Cursor creation failed.")
778+
return cursor
779+
except sqlite3.Error as e:
780+
LOGGER.warning(f"Existing DB connection failed, retrying from scratch. Error: {e}")
781+
self.connection = None # Reset and retry below
770782

771783
retries = 0
772784
while retries < MAX_RETRIES:
773785
try:
774-
self.connection = sqlite3.connect(self.dbpath, timeout=10) # Extend timeout for better handling
775-
return self.connection.cursor()
786+
self.connection = sqlite3.connect(self.dbpath, timeout=10)
787+
cursor = self.connection.cursor()
788+
if cursor is None:
789+
LOGGER.error("Database cursor does not exist")
790+
raise CVEDBError("Cursor creation failed.")
791+
return cursor
776792
except sqlite3.OperationalError as e:
777793
if "database is locked" in str(e):
778794
retries += 1
779-
wait_time = RETRY_DELAY * (2 ** retries) # Exponential backoff
795+
wait_time = RETRY_DELAY * (2 ** retries)
780796
LOGGER.warning(f"Database locked, retrying in {wait_time:.2f} seconds...")
781797
time.sleep(wait_time)
782798
else:
783799
LOGGER.error(f"Database operation failed: {e}")
784-
raise CVEDBError(f"Database operation failed: {e}") # Ensuring consistent error handling
800+
raise CVEDBError(f"Database operation failed: {e}")
801+
except sqlite3.Error as e:
802+
LOGGER.error(f"Unexpected database error: {e}")
803+
raise CVEDBError(f"Unexpected database error: {e}")
785804

786805
LOGGER.error("Max retries reached. Could not obtain database connection.")
787806
raise CVEDBError("Failed to open database connection after multiple attempts")
788807

789808

809+
790810
def db_close(self) -> None:
791811
"""Closes connection to sqlite database."""
792812
if self.connection:

0 commit comments

Comments
 (0)