chore: defensive improvements for robustness and best practices#426
Closed
zhengdaqi wants to merge 1 commit into
Closed
chore: defensive improvements for robustness and best practices#426zhengdaqi wants to merge 1 commit into
zhengdaqi wants to merge 1 commit into
Conversation
- 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>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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
UnboundLocalErrorinwrite_memory_output()benchmark_namewas only assigned insideif isinstance(benchmark_cfg, dict). Ifbenchmark_cfgis not a dict, the variable is used uninitialized on the next line. Fixed by initializingbenchmark_name = ""before the conditional.2. Fix
__main__entry pointmain()is a synchronous function that internally callsasyncio.run()for async subcommands. The__main__block wrapped it in anotherasyncio.run(main()), which would raiseValueErroraftermain()completes (since it returnsNone, 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 downstreamzip-based processing. Changed toitem.get(key)to maintain alignment (returnsNonefor missing keys).4. Replace bare
except:withexcept Exception:Four bare
except:clauses incustom.pycatchSystemExitandKeyboardInterruptin addition to regular exceptions. This can prevent clean process termination. Changed toexcept Exception:per PEP 8 guidelines.5. Fix file handle leak in
server.pyyaml.safe_dump(data, out_path.open("w"))creates a file handle without awithstatement. Ifsafe_dumpraises an exception, the handle may not be closed promptly. Wrapped withwithstatement for proper resource cleanup.Test Plan
pytestto verify no regressionsMade with Cursor