Skip to content

fix(datastore): fix context leak in Iterator and Transaction spans#14478

Merged
bhshkh merged 7 commits into
googleapis:mainfrom
bhshkh:fix/ds-ctx-wrapping
May 13, 2026
Merged

fix(datastore): fix context leak in Iterator and Transaction spans#14478
bhshkh merged 7 commits into
googleapis:mainfrom
bhshkh:fix/ds-ctx-wrapping

Conversation

@bhshkh

@bhshkh bhshkh commented Apr 23, 2026

Copy link
Copy Markdown
Contributor

Fixes #9528

This PR fixes a context leak in the datastore library where several methods of Iterator and Transaction were overwriting their internal context field with a new span context on every call (e.g., t.ctx = trace.StartSpan(t.ctx, ...)).

When these methods are called repeatedly (e.g., in a loop over millions of objects calling Cursor() or Get()), the context chain grows linearly, eventually leading to a stack overflow in context.Done() or context.Value() due to deep recursion.

Fix is to use a local context variable for the span instead of overwriting.

@bhshkh bhshkh requested review from a team as code owners April 23, 2026 07:51
@product-auto-label product-auto-label Bot added the api: datastore Issues related to the Datastore API. label Apr 23, 2026
@bhshkh bhshkh enabled auto-merge (squash) April 23, 2026 07:51

@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 refactors context management within the datastore package to prevent the overwriting of internal struct contexts during tracing. By utilizing local context variables for spans and passing them explicitly to methods like nextBatch, the code ensures better context isolation. Feedback was provided regarding an inconsistency in beginTransaction, where a tracing span is only conditionally created, potentially leading to missing telemetry compared to the Commit and Rollback implementations.

Comment thread datastore/transaction.go Outdated
Comment thread datastore/transaction.go Outdated
@bhshkh bhshkh requested a review from shollyman April 24, 2026 13:54
@shollyman shollyman disabled auto-merge May 11, 2026 19:49

@shollyman shollyman 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.

Approving as is (disabled automerge), but please consider whether we could add a test here for either validating the contexts are unmingled or that we don't end up with a deeply nested span pattern.

@bhshkh bhshkh enabled auto-merge (squash) May 13, 2026 07:57
@bhshkh

bhshkh commented May 13, 2026

Copy link
Copy Markdown
Contributor Author

Approving as is (disabled automerge), but please consider whether we could add a test here for either validating the contexts are unmingled or that we don't end up with a deeply nested span pattern.

Added tests

@bhshkh bhshkh merged commit 78de20d into googleapis:main May 13, 2026
13 checks passed
@bhshkh bhshkh deleted the fix/ds-ctx-wrapping branch May 13, 2026 20:50
bhshkh added a commit that referenced this pull request May 19, 2026
PR created by the Librarian CLI to initialize a release. Merging this PR
will auto trigger a release.

Librarian Version: v0.13.2-0.20260518181009-04b8e642ea4c
Language Image:
us-central1-docker.pkg.dev/cloud-sdk-librarian-prod/images-prod/librarian-go@sha256:b04b076f5eedbb5546bd6fc1404969dd3698c8b19c0f34ae815a84ae735a606a
<details><summary>datastore: v1.24.0</summary>

##
[v1.24.0](datastore/v1.23.0...datastore/v1.24.0)
(2026-05-19)

### Bug Fixes

* detach rollback context from transaction cancellation (#14602)
([0a8d10d](0a8d10d1))

* fix context leak in Iterator and Transaction spans (#14478)
([78de20d](78de20da))

* add retries to emulator (#14591)
([d944830](d9448309))

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

Labels

api: datastore Issues related to the Datastore API.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

datastore: query iterating over 36m objects panics with stack overflow

2 participants