Skip to content

fix(api_server): replace bare except: with specific exception types#90

Merged
warren618 merged 1 commit into
HKUDS:mainfrom
Teerapat-Vatpitak:fix/api-server-bare-except
May 10, 2026
Merged

fix(api_server): replace bare except: with specific exception types#90
warren618 merged 1 commit into
HKUDS:mainfrom
Teerapat-Vatpitak:fix/api-server-bare-except

Conversation

@Teerapat-Vatpitak

Copy link
Copy Markdown
Contributor

Summary

  • Replaces three bare except: clauses in agent/api_server.py with explicit exception classes.
  • Pure code-quality change; no behaviour change for the failures these blocks are meant to swallow; KeyboardInterrupt / SystemExit / programming bugs now propagate as they should.

Why

Three blocks in api_server.py use bare except: — which catches BaseException subclasses including KeyboardInterrupt, SystemExit, and GeneratorExit. That makes the API server unresponsive to Ctrl+C inside these blocks and silently hides real bugs (NameError, AttributeError) as no-op passes.

The blocks themselves are best-effort metadata extraction from per-run artifact files where falling through with the default value is the right behaviour — but only for the specific I/O / parse failures these blocks can actually produce. Tightening the catch surface preserves that intent while removing the foot-gun.

Changes

Line Block Before After
1015 req.json prompt extraction except: except (json.JSONDecodeError, OSError):
1022 planner_output.json prompt extraction except: except (json.JSONDecodeError, OSError):
1042 metrics.csv numeric parse except: except (OSError, ValueError):

The other except Exception: clauses in this file (e.g. lines 638, 730, 745) already exclude BaseException and are out of scope for this PR.

Test plan

  • pytest --ignore=agent/tests/e2e_backtest --tb=line -q passes — test_settings_api, test_security_auth_api, and test_upload_api all still green.
  • grep -nE '^\s+except:\s*$' agent/api_server.py returns no matches.

Checklist

  • No changes to protected areas.
  • No new dependencies / hardcoded values.
  • Code follows CONTRIBUTING.md (Conventional Commit prefix fix:, no comment-preserved code).
  • Documentation updated — N/A.
Three bare except: clauses in api_server.py silently caught BaseException
(KeyboardInterrupt, SystemExit, GeneratorExit) in addition to ordinary
errors. That makes the API server unresponsive to Ctrl+C inside these
blocks and hides real bugs (NameError, AttributeError) as silent passes.

The blocks themselves are best-effort metadata extraction from per-run
artifact files, where falling through with the default values is the
right behavior — but only for the specific I/O / parse failures these
blocks can actually produce. The new clauses spell those out:

- lines 1015, 1022 (req.json / planner_output.json prompt extraction):
    except (json.JSONDecodeError, OSError)
- line 1042 (metrics.csv numeric parse):
    except (OSError, ValueError)

Behavior is unchanged for the failures the blocks are designed to swallow;
KeyboardInterrupt, SystemExit, and programming errors now propagate as
they should.
@warren618 warren618 merged commit 4eb682d into HKUDS:main May 10, 2026
1 check passed
@warren618

Copy link
Copy Markdown
Collaborator

Thanks for the cleanup. The narrowed exception handling keeps the intended best-effort behavior while avoiding bare except: swallowing BaseException. I verified the affected API tests and syntax checks before merging.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

2 participants