Skip to content

fix: initialize content_hash to avoid UnboundLocalError on seek failure#3089

Open
totto wants to merge 1 commit into
topoteretes:mainfrom
totto:fix/content-hash-unbound-local-error
Open

fix: initialize content_hash to avoid UnboundLocalError on seek failure#3089
totto wants to merge 1 commit into
topoteretes:mainfrom
totto:fix/content-hash-unbound-local-error

Conversation

@totto

@totto totto commented Jun 18, 2026

Copy link
Copy Markdown

Bug

get_file_metadata raises UnboundLocalError: cannot access local variable 'content_hash' before assignment when the file object does not support seek().

Root cause

content_hash is assigned only inside the try block. When file.seek(0) raises io.UnsupportedOperation, the except block logs the error but never sets content_hash. The variable is then referenced unconditionally at the return statement.

try:
    file.seek(0)
    content_hash = await get_file_content_hash(file)  # never reached
    file.seek(0)
except io.UnsupportedOperation as error:
    logger.error(...)  # content_hash still unbound

return FileMetadata(
    ...
    content_hash=content_hash,  # UnboundLocalError
)

Fix

Initialize content_hash = "" before the try block so the return statement always has a valid value regardless of whether the hash computation succeeds.

Test scenario

Any seekable wrapper (e.g. io.RawIOBase subclass without seek) or a network stream passed as file will trigger the crash. Initializing to "" is consistent with the FileMetadata type which declares content_hash: str.

🤖 Generated with Claude Code

When file.seek(0) raises io.UnsupportedOperation the except block logs
the error but content_hash is never assigned, causing UnboundLocalError
at the return statement. Initialize to empty string before the try block.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
@totto totto requested a review from Vasilije1990 as a code owner June 18, 2026 05:07
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.

1 participant