Repository navigation
fix(tortoiseorm): avoid IndexError in _do_execute for empty/whitespace-only statements - #5065
Conversation
…e-only statements
|
|
henry3260
left a comment
There was a problem hiding this comment.
overall looks great just one nit
…no tokens Fallback suggested in review. Co-authored-by: Henry Chen <108824346+henry3260@users.noreply.github.com>
Shriprasad-P
left a comment
There was a problem hiding this comment.
Thanks for the PR. Looks focused from a first pass — please confirm tests cover the new path and CI is green.
Pull request dashboard statusMerged · refreshed 2026-10-23 18:44 UTC Status above doesn't look right?
|
|
Both already in place on the current head: \test_span_name_empty_or_whitespace_statement\ exercises an empty and a whitespace-only statement and asserts each span gets the :memory:\ fallback name instead of raising, and CI is green on c72a159. |
tammy-baylis-swi
left a comment
There was a problem hiding this comment.
Thanks for another one
|
/dashboard route:reviewers |
|
@tammy-baylis-swi, this pull request was routed to reviewers. The handoff remains active across pushes until newer actionable human feedback arrives. Top-level feedback through this request will not return; unresolved review threads remain open. |
|
This PR has been automatically marked as stale because it has not had any activity for 14 days. It will be closed if no further activity occurs within 14 days of this comment. |
|
Bumping this so it doesn't go stale. @xrmx, could you take a look when you have a moment? |
chrikrah
left a comment
There was a problem hiding this comment.
@JoeyPatricio the guard fixes the crash your body reproduces. I ran the package suite at 9c33139, then put the base __init__.py over yours and the new case died on IndexError. I would merge this as it stands.
$ cd /scratch/lane18/a_tortoise/head && CORE_REPO_SHA=9406f34 .venv-scout/bin/tox -e py312-test-instrumentation-tortoiseorm # head 9c331395a0df120c88051e7ff7cfad192a0a0b5d, Python 3.12.3, pytest 7.4.4
instrumentation/opentelemetry-instrumentation-tortoiseorm/tests/test_tortoiseorm_instrumentation.py::TestTortoiseORMInstrumentor::test_span_name_empty_or_whitespace_statement PASSED [ 37%]
============================== 8 passed in 0.59s ===============================
py312-test-instrumentation-tortoiseorm: OK (2.84=setup[1.74]+cmd[1.10] seconds)
# CORE_REPO_SHA pins core before 0f5c340 (2026-09-25): core main then moved to 0.67b0.dev0, and contrib's
# `opentelemetry-semantic-conventions == 0.66b0.dev` pin stops resolving against it on a fresh run
# not run: lint-instrumentation-tortoiseorm, ruff, typecheck, the other package suites
$ cp /scratch/lane18/a_tortoise/base/instrumentation/opentelemetry-instrumentation-tortoiseorm/src/opentelemetry/instrumentation/tortoiseorm/__init__.py instrumentation/opentelemetry-instrumentation-tortoiseorm/src/opentelemetry/instrumentation/tortoiseorm/__init__.py && CORE_REPO_SHA=9406f34 .venv-scout/bin/tox -e py312-test-instrumentation-tortoiseorm
FAILED instrumentation/opentelemetry-instrumentation-tortoiseorm/tests/test_tortoiseorm_instrumentation.py::TestTortoiseORMInstrumentor::test_span_name_empty_or_whitespace_statement
E IndexError: list index out of range
instrumentation/opentelemetry-instrumentation-tortoiseorm/src/opentelemetry/instrumentation/tortoiseorm/__init__.py:268: IndexError
========================= 1 failed, 7 passed in 0.34s ==========================
non-blocking: dbapi strips a leading /* ... */ before it takes the token, and this wrapper does not, so a statement that opens with a block comment names its span /*. I would reuse that one regex here while the line is open.
$ .tox/py312-test-instrumentation-tortoiseorm/bin/python -m pytest instrumentation/opentelemetry-instrumentation-tortoiseorm/tests/test_tortoiseorm_instrumentation.py::TestTortoiseORMInstrumentor::test_probe_leading_comment_span_name -q -s # probe method appended to the test file, removed after the run
PROBE span names: ['CREATE', '/*']
1 passed in 0.22s
$ python3 -c "import re; print(re.compile(r'^/\*.*?\*/').sub('', '/* c */ select 1).split()[0])"
select
Would you take the comment strip in this change?
A statement starting with /* ... */ produced a span named /*. Use the same regex as dbapi, asyncpg, psycopg and sqlalchemy.
|
Thanks for running it against the base. Folded the strip in with the same |
chrikrah
left a comment
There was a problem hiding this comment.
@JoeyPatricio the strip went in as dbapi's own ^/\*.*?\*/, and the new test is what proves it. Put the 204b796 __init__.py back under it and the span names come out as ['CREATE', '/*', '/*']. I would merge this at 131d99a.
$ tox -e py312-test-instrumentation-tortoiseorm # exported tree at 131d99a7b09252378f752e2bd4396904dbd5dff3, Python 3.12.3, tox 4.66.0
============================== 9 passed in 1.24s ===============================
$ git show 204b7966:instrumentation/opentelemetry-instrumentation-tortoiseorm/src/opentelemetry/instrumentation/tortoiseorm/__init__.py > instrumentation/opentelemetry-instrumentation-tortoiseorm/src/opentelemetry/instrumentation/tortoiseorm/__init__.py
$ tox -e py312-test-instrumentation-tortoiseorm # tests from 131d99a, source from 204b796
E AssertionError: 'SELECT' not found in ['CREATE', '/*', '/*']
========================= 1 failed, 8 passed in 0.53s ==========================
$ tox -e lint-instrumentation-tortoiseorm # source restored to 131d99a
Your code has been rated at 10.00/10
# not run: typecheck, the other package suites
A comment-only statement now falls through to :memory:, the same fallback the empty-statement guard uses, so one branch covers both.
@emdneto, my approval carries no weight in the count here, so this needs a maintainer's. Could you review it this week?
There was a problem hiding this comment.
🟢 Approval recommended
The narrowly scoped fix addresses the reported failures with regression coverage and no identified blocking issues.
No open issues 🎉
What changed in this PR
Fixes TortoiseORM span-name parsing so empty or whitespace-only SQL reaches the driver without an instrumentation IndexError, consistent with related database instrumentation fixes.
Changes:
- Guards empty token lists and falls back to the database name or dialect.
- Strips a leading block comment before extracting the operation name.
- Adds regression tests and a changelog entry.
| File | Description |
|---|---|
| instrumentation/opentelemetry-instrumentation-tortoiseorm/tests/test_tortoiseorm_instrumentation.py | Tests empty, whitespace-only, comment-prefixed, and comment-only statements. |
| instrumentation/opentelemetry-instrumentation-tortoiseorm/src/opentelemetry/instrumentation/tortoiseorm/__init__.py | Guards token extraction, strips leading comments, and supplies fallback span names. |
| .changelog/5065.fixed | Documents the parsing fix. |
Review effort: Balanced
Description
Same class as #4934 / #4936 / #4942 / #4943, in the tortoiseorm instrumentation's
_do_execute. The span name is taken as the first token of the statement with an unguardedargs[0].split()[0]. An empty or whitespace-only statement has no tokens, so the wrapper raisesIndexErrorand the user's query never reaches the driver. Without the instrumentor the same call succeeds.Guards it the same way the dbapi base does (
tokens[0] if tokens else "").Repro on
main(tortoise-orm 1.1.8 and the pinned 0.20.0):Only raw SQL passed through the
execute_*client methods is affected; ORM-generated SQL is never empty.The same line also took a leading
/* ... */comment as the operation, so/* c */ SELECT 1produced a span named/*. It now strips the comment with the same^/\*.*?\*/regex dbapi, asyncpg, psycopg and sqlalchemy use. A statement that is only a comment has no tokens left and gets the fallback name. Suggested in review.No open issue for this one; found while checking for siblings of #4934.
Type of change
How Has This Been Tested?
Added
test_span_name_empty_or_whitespace_statement. It fails onmainwithIndexErrorand passes with the fix. Addedtest_span_name_strips_leading_comment, which fails before the strip with span names['CREATE', '/*', '/*'].tox -e py313-test-instrumentation-tortoiseorm(9 passed),tox -e lint-instrumentation-tortoiseormandruff check/ruff format --checkpass.Does This PR Require a Core Repo Change?
Checklist: