Commit 5bff9c3
Fix Orchestrator.__repr__ crash on partially-initialized instances (#5073)
Summary:
Pull Request resolved: #5073
Fix `AttributeError` in `Orchestrator.__repr__()` when the object is only
partially initialized. The method checked `hasattr(self, "experiment")` but
did not check `hasattr(self, "generation_strategy")`, causing a crash when
`__repr__` was called between the two attribute assignments.
**Bug details:**
`Orchestrator.__init__` sets attributes in this order:
```
self.experiment = experiment # line 228
self._set_logger(options=options) # line 231
self.options = options # line 232
self.generation_strategy = generation_strategy # line 235
```
If any error occurs between lines 228-235 (e.g. in `_set_logger`), then
`self.experiment` exists but `self.generation_strategy` does not.
The `log_usage()` decorator on `OrcOpticsScheduler.__init__` catches such
errors and calls `repr(args)` to log them. Since `args[0]` is the partially-
initialized scheduler instance, this triggers `Orchestrator.__repr__()`:
```python
def __repr__(self) -> str:
if not hasattr(self, "experiment"): # True -- experiment IS set
return f"{self.__class__.__name__}"
return (
f"...generation_strategy={self.generation_strategy}..." # BOOM
)
```
The secondary `AttributeError` replaces the original error, making the real
failure impossible to diagnose.
**Example:** FBLearner run f1051747930 failed with:
```
AttributeError: 'OrcOpticsScheduler' object has no attribute 'generation_strategy'
```
Stack trace: `OrcOpticsScheduler.__init__` -> `Orchestrator.__init__` ->
(error between lines 228-235) -> `log_usage` error handler ->
`repr(args)` -> `Orchestrator.__repr__` -> `self.generation_strategy` crash.
The actual root cause was completely masked by this `__repr__` bug.
**Fix:** Add `hasattr(self, "generation_strategy")` guard alongside the
existing `hasattr(self, "experiment")` check.
Reviewed By: Balandat
Differential Revision: D97166104
fbshipit-source-id: f31e55ffe028e482c627527e203a4998f24f5b0e1 parent 358c4c6 commit 5bff9c3
1 file changed
Lines changed: 1 addition & 1 deletion
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
501 | 501 | | |
502 | 502 | | |
503 | 503 | | |
504 | | - | |
| 504 | + | |
505 | 505 | | |
506 | 506 | | |
507 | 507 | | |
| |||
0 commit comments