Skip to content

fix(spanner): guard against empty exc.errors in run_in_transaction#17905

Open
abhi-0203 wants to merge 1 commit into
googleapis:mainfrom
abhi-0203:fix/spanner-empty-errors-index
Open

fix(spanner): guard against empty exc.errors in run_in_transaction#17905
abhi-0203 wants to merge 1 commit into
googleapis:mainfrom
abhi-0203:fix/spanner-empty-errors-index

Conversation

@abhi-0203

Copy link
Copy Markdown

Summary

Guard against empty exc.errors in Session.run_in_transaction to prevent IndexError.

Bug: When run_in_transaction catches an Aborted exception with an empty errors list (e.g. manually instantiated Aborted("...")), it crashes with IndexError: list index out of range on exc.errors[0].

Fix: Add a conditional guard: exc.errors[0] if exc.errors else None. When exc.errors is empty, None is passed to _get_retry_delay, which gracefully falls back to default_retry_delay or exponential backoff (2**attempts + random()).

Applied to both occurrences in run_in_transaction (lines 545-547 and 582-584).

Reproducer:

from google.api_core.exceptions import Aborted
from google.cloud import spanner

spanner_client = spanner.Client()
instance = spanner_client.instance("your-instance-id")
database = instance.database("your-database-id")

def callback(transaction):
    raise Aborted("VersionMismatch: CAS failed")

database.run_in_transaction(callback)  # IndexError before fix

Closes #17903

When run_in_transaction catches an Aborted exception with an empty
errors list (e.g. manually instantiated Aborted('...')), it crashes
with an uncaught IndexError on exc.errors[0]. Add a guard to fall
back to default_retry_delay when exc.errors is empty.

Closes googleapis#17903
@abhi-0203
abhi-0203 requested a review from a team as a code owner July 27, 2026 04:39
@google-cla

google-cla Bot commented Jul 27, 2026

Copy link
Copy Markdown

Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

View this failed invocation of the CLA check for more information.

For the most up to date status, view the checks section at the bottom of the pull request.

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request updates the run_in_transaction method in session.py to safely handle cases where an Aborted exception is raised with an empty errors list. By checking if exc.errors is populated before accessing its first element, it prevents potential IndexError exceptions. There are no review comments provided, so I have no additional feedback to offer.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[spanner] run_in_transaction raises uncaught IndexError when Aborted.errors is empty

1 participant