Skip to content

Fix UnboundLocalError in training thread exception handlers#2

Merged
davepl merged 2 commits intoSeptAvgLevelfrom
copilot/vscode1757961482423
Sep 15, 2025
Merged

Fix UnboundLocalError in training thread exception handlers#2
davepl merged 2 commits intoSeptAvgLevelfrom
copilot/vscode1757961482423

Conversation

Copy link
Contributor

Copilot AI commented Sep 15, 2025

Problem

Training threads were failing with two related errors:

  1. 'DQNAgent' object has no attribute 'train_step'
  2. UnboundLocalError: cannot access local variable 'traceback' where it is not associated with a value

The root cause was redundant local import traceback statements inside exception handlers in the background_train and train_step methods.

Root Cause Analysis

The issue occurred in two locations in aimodel.py:

# Line 693-696 (background_train method)
except Exception as e:
    print(f"Training error in {worker_id}: {e}")
    import traceback  # ← Problematic local import
    traceback.print_exc()

# Line 913-916 (train_step method)  
except Exception as e:
    print(f"Training error: {e}")
    import traceback  # ← Problematic local import
    traceback.print_exc()

The module already imports traceback at the top level (line 66), making these local imports redundant and potentially problematic in threading contexts where the local import can cause variable scoping issues.

Solution

Removed the redundant local import traceback statements and use the existing module-level import:

# Fixed version
except Exception as e:
    print(f"Training error in {worker_id}: {e}")
    traceback.print_exc()  # Uses module-level import

Testing

  • ✅ Verified train_step method exists and is callable
  • ✅ Tested training threads run without AttributeError or UnboundLocalError
  • ✅ Confirmed main application runs normally without errors
  • ✅ All training worker threads start and execute successfully

The training system now operates correctly with proper exception handling and no thread-related import issues.

Created from VS Code via the GitHub Pull Request extension.


✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

@davepl davepl marked this pull request as ready for review September 15, 2025 18:38
@davepl davepl merged commit 656fbe4 into SeptAvgLevel Sep 15, 2025
1 check passed
Copilot AI changed the title [WIP] Remember we don't want to protect AGAINST this case, it's an assert. We want to FIX whatever issue is getting us into that situation, not by masking or or patching, but by understanding and fixing the underlying issue. Fix UnboundLocalError in training thread exception handlers Sep 15, 2025
Copilot AI requested a review from davepl September 15, 2025 18:54
davepl added a commit that referenced this pull request Feb 21, 2026
[WIP] Remember we don't want to protect AGAINST this case, it's an assert.  We want to FIX whatever issue is getting us into that situation, not by masking or or patching, but by understanding and fixing the underlying issue.
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.

2 participants