Skip to content

Commit efb3ed9

Browse files
committed
always finalize the serialized transaction so that it doesn't leak outside the function
1 parent b169f78 commit efb3ed9

File tree

1 file changed

+28
-21
lines changed

1 file changed

+28
-21
lines changed

backend/danswer/background/indexing/run_indexing.py

Lines changed: 28 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -387,30 +387,37 @@ def _prepare_index_attempt(
387387
# after the next commit:
388388
# https://docs.sqlalchemy.org/en/20/orm/session_transaction.html#setting-isolation-for-individual-transactions
389389
db_session.connection(execution_options={"isolation_level": "SERIALIZABLE"}) # type: ignore
390-
if tenant_id is not None:
391-
# Explicitly set the search path for the given tenant
392-
db_session.execute(text(f'SET search_path TO "{tenant_id}"'))
393-
# Verify the search path was set correctly
394-
result = db_session.execute(text("SHOW search_path"))
395-
current_search_path = result.scalar()
396-
logger.info(f"Current search path set to: {current_search_path}")
397-
398-
attempt = get_index_attempt(
399-
db_session=db_session,
400-
index_attempt_id=index_attempt_id,
401-
)
390+
try:
391+
if tenant_id is not None:
392+
# Explicitly set the search path for the given tenant
393+
db_session.execute(text(f'SET search_path TO "{tenant_id}"'))
394+
# Verify the search path was set correctly
395+
result = db_session.execute(text("SHOW search_path"))
396+
current_search_path = result.scalar()
397+
logger.info(f"Current search path set to: {current_search_path}")
398+
399+
attempt = get_index_attempt(
400+
db_session=db_session,
401+
index_attempt_id=index_attempt_id,
402+
)
402403

403-
if attempt is None:
404-
raise RuntimeError(f"Unable to find IndexAttempt for ID '{index_attempt_id}'")
404+
if attempt is None:
405+
raise RuntimeError(
406+
f"Unable to find IndexAttempt for ID '{index_attempt_id}'"
407+
)
405408

406-
if attempt.status != IndexingStatus.NOT_STARTED:
407-
raise RuntimeError(
408-
f"Indexing attempt with ID '{index_attempt_id}' is not in NOT_STARTED status. "
409-
f"Current status is '{attempt.status}'."
410-
)
409+
if attempt.status != IndexingStatus.NOT_STARTED:
410+
raise RuntimeError(
411+
f"Indexing attempt with ID '{index_attempt_id}' is not in NOT_STARTED status. "
412+
f"Current status is '{attempt.status}'."
413+
)
414+
415+
mark_attempt_in_progress(attempt, db_session)
411416

412-
# only commit once, to make sure this all happens in a single transaction
413-
mark_attempt_in_progress(attempt, db_session)
417+
# only commit once, to make sure this all happens in a single transaction
418+
db_session.commit()
419+
except Exception:
420+
db_session.rollback()
414421

415422
return attempt
416423

0 commit comments

Comments
 (0)