Skip to content

chore: defensive improvements for robustness and best practices#426

Closed
zhengdaqi wants to merge 1 commit into
OpenBMB:mainfrom
zhengdaqi:fix/defensive-improvements
Closed

chore: defensive improvements for robustness and best practices#426
zhengdaqi wants to merge 1 commit into
OpenBMB:mainfrom
zhengdaqi:fix/defensive-improvements

Conversation

@zhengdaqi
Copy link
Copy Markdown
Contributor

Summary

Five defensive improvements that harden edge-case handling and follow Python best practices. All changes are non-breaking and only affect error paths or rare edge cases.

1. Fix potential UnboundLocalError in write_memory_output()

benchmark_name was only assigned inside if isinstance(benchmark_cfg, dict). If benchmark_cfg is not a dict, the variable is used uninitialized on the next line. Fixed by initializing benchmark_name = "" before the conditional.

2. Fix __main__ entry point

main() is a synchronous function that internally calls asyncio.run() for async subcommands. The __main__ block wrapped it in another asyncio.run(main()), which would raise ValueError after main() completes (since it returns None, not a coroutine).

3. Fix benchmark key mapping producing ragged lists

When some data records are missing a mapped key, the original list comprehension [item[key] for item in data if key in item] skips those records entirely. This produces lists of different lengths for different aliases, breaking downstream zip-based processing. Changed to item.get(key) to maintain alignment (returns None for missing keys).

4. Replace bare except: with except Exception:

Four bare except: clauses in custom.py catch SystemExit and KeyboardInterrupt in addition to regular exceptions. This can prevent clean process termination. Changed to except Exception: per PEP 8 guidelines.

5. Fix file handle leak in server.py

yaml.safe_dump(data, out_path.open("w")) creates a file handle without a with statement. If safe_dump raises an exception, the handle may not be closed promptly. Wrapped with with statement for proper resource cleanup.

Test Plan

  • Run pytest to verify no regressions
  • These are defensive fixes for edge cases; primary functionality is unaffected

Made with Cursor

- Fix potential UnboundLocalError for benchmark_name in
  write_memory_output() when benchmark_cfg is not a dict
- Fix __main__ block calling asyncio.run() on sync main() function
  (main() already calls asyncio.run() internally for async commands)
- Fix benchmark key mapping producing ragged lists when keys are
  missing from some records (use .get() to maintain list alignment)
- Replace 4 bare except clauses with except Exception to avoid
  accidentally catching SystemExit and KeyboardInterrupt
- Fix file handle leak in server.py: use with-statement for
  yaml.safe_dump to ensure proper cleanup on exceptions

Co-authored-by: Cursor <cursoragent@cursor.com>
@xhd0728 xhd0728 closed this May 21, 2026
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