Fix file handle leak in multiprocess execution by ensuring proper logger cleanup#2
Conversation
…r cleanup Co-authored-by: coolmian <36444522+coolmian@users.noreply.github.com>
|
@copilot 补充一个py-spy的监控报告,你前面的修复是否能解决这个问题? %Own %Total OwnTime TotalTime Function (filename) |
Yes, my fix should help address the performance issues shown in your py-spy report. The profiling data shows significant CPU time spent in logging-related functions:
These bottlenecks are directly related to the file handle leak I fixed. Here's how my solution addresses the root causes: Root Cause: The subprocess execution was creating
My Fix (commit b12126b):
Expected Performance Impact:
The fix prevents the "Too many open files" error while also addressing the underlying performance issues your py-spy report identified. The tests confirm file handlers are properly cleaned up in both normal and error scenarios. |
Problem
The multiprocess execution system had a file handle leak issue that would eventually cause the following error:
This occurred because
PyxxlFileHandlerinstances created in subprocess execution were not being properly closed. While the main process uses thenew_logger()context manager that ensuresDiskLog.after_running()is called to close file handlers, subprocesses created their own loggers without this cleanup mechanism.Root Cause
In the subprocess execution flow:
ExecutorHandler.start()callsrun_handler_in_process()in a subprocess_create_process_logger()creates aPyxxlFileHandlerfor logging to disknew_logger())Solution
Added proper cleanup to the
run_handler_in_process()function:_cleanup_process_logger(): New function that closes and removes file handlers, mimicking the behavior ofDiskLog.after_running()Changes
run_handler_in_process()to use try/finally pattern ensuring cleanup_cleanup_process_logger()function that properly closes file handlerstest_process_executor.pyto verify the fixTesting
The fix includes thorough tests that verify:
Backward Compatibility
This fix is fully backward compatible and maintains all existing functionality while preventing the "Too many open files" error. No API changes or breaking changes are introduced.
💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.