[PR #1952] [MERGED] fix(tinytorch): UnboundLocalError when reading an HTTP error body fails #34676

Closed
opened 2026-07-14 19:28:42 -05:00 by GiteaMirror · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/harvard-edge/cs249r_book/pull/1952
Author: @Shashank-Tripathi-07
Created: 7/7/2026
Status: Merged
Merged: 7/10/2026
Merged by: @profvjreddi

Base: devHead: fix/submission-unbound-error-body


📝 Commits (1)

  • a53c941 fix(tinytorch): UnboundLocalError when reading an HTTP error body fails

📊 Changes

1 file changed (+3 additions, -1 deletions)

View changed files

📝 tinytorch/tito/core/submission.py (+3 -1)

📄 Description

Summary

  • In the HTTP-error handler, error_body was only assigned inside the try block (e.read().decode('utf-8')). If that read/decode itself raised (network hiccup mid-read, or a non-UTF-8 error body from the server), the broad except (json.JSONDecodeError, Exception) still ran and referenced error_body unconditionally, raising UnboundLocalError -- a confusing crash in the middle of tito module complete's auto-sync step, instead of the clean "Upload failed" message this code is trying to produce.

Fix

Initialize error_body = "" before the try, and only print it in the except block when it's non-empty, so a read/decode failure degrades to "nothing to print" instead of crashing.

Test plan

  • Simulated both scenarios in isolation: (1) the read/decode call raising before error_body is ever set -- old logic crashes with UnboundLocalError, new logic handles it silently; (2) read succeeding but json.loads failing (the normal case this except block exists for) -- both old and new logic correctly print the error body.
  • python -m py_compile passes on the edited file.

🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/harvard-edge/cs249r_book/pull/1952 **Author:** [@Shashank-Tripathi-07](https://github.com/Shashank-Tripathi-07) **Created:** 7/7/2026 **Status:** ✅ Merged **Merged:** 7/10/2026 **Merged by:** [@profvjreddi](https://github.com/profvjreddi) **Base:** `dev` ← **Head:** `fix/submission-unbound-error-body` --- ### 📝 Commits (1) - [`a53c941`](https://github.com/harvard-edge/cs249r_book/commit/a53c941dda44b6cc8342a7eab39b4fda06ef9061) fix(tinytorch): UnboundLocalError when reading an HTTP error body fails ### 📊 Changes **1 file changed** (+3 additions, -1 deletions) <details> <summary>View changed files</summary> 📝 `tinytorch/tito/core/submission.py` (+3 -1) </details> ### 📄 Description ## Summary - In the HTTP-error handler, `error_body` was only assigned inside the `try` block (`e.read().decode('utf-8')`). If that read/decode itself raised (network hiccup mid-read, or a non-UTF-8 error body from the server), the broad `except (json.JSONDecodeError, Exception)` still ran and referenced `error_body` unconditionally, raising `UnboundLocalError` -- a confusing crash in the middle of `tito module complete`'s auto-sync step, instead of the clean "Upload failed" message this code is trying to produce. ## Fix Initialize `error_body = ""` before the `try`, and only print it in the `except` block when it's non-empty, so a read/decode failure degrades to "nothing to print" instead of crashing. ## Test plan - [x] Simulated both scenarios in isolation: (1) the read/decode call raising before `error_body` is ever set -- old logic crashes with `UnboundLocalError`, new logic handles it silently; (2) read succeeding but `json.loads` failing (the normal case this except block exists for) -- both old and new logic correctly print the error body. - [x] `python -m py_compile` passes on the edited file. --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
GiteaMirror added the pull-request label 2026-07-14 19:28:42 -05:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github-starred/cs249r_book#34676