[Internal] Fix the bug that Ctrl+C doesn't returns flow result.#3174
[Internal] Fix the bug that Ctrl+C doesn't returns flow result.#3174
Conversation
promptflow SDK CLI Azure E2E Test Result heta/fix-cancel 4 files 4 suites 4m 8s ⏱️ Results for commit be2966e. ♻️ This comment has been updated with latest results. |
promptflow-core test result0 tests 0 ✅ 0s ⏱️ Results for commit be2966e. ♻️ This comment has been updated with latest results. |
SDK CLI Global Config Test Result heta/fix-cancel6 tests 6 ✅ 1m 15s ⏱️ Results for commit be2966e. ♻️ This comment has been updated with latest results. |
Executor Unit Test Result heta/fix-cancel792 tests 792 ✅ 3m 43s ⏱️ Results for commit be2966e. ♻️ This comment has been updated with latest results. |
Executor E2E Test Result heta/fix-cancel243 tests 238 ✅ 5m 31s ⏱️ Results for commit be2966e. ♻️ This comment has been updated with latest results. |
SDK CLI Test Result heta/fix-cancel 4 files 4 suites 1h 1m 10s ⏱️ Results for commit be2966e. ♻️ This comment has been updated with latest results. |
Description
Fix the bug that Ctrl+C doesn't returns flow result because the signal handler would raise an exception while asyncio.run cannot handle it correctly.
Note that for py<3.11, there is no default sigint handler when using asyncio.run to run an async function, so we add one for py<3.11 following python's implementation: https://github.com/python/cpython/blob/46c808172fd3148e3397234b23674bf70734fb55/Lib/asyncio/runners.py#L150
Such logic is initialized in this PR: python/cpython#32105
This pull request introduces significant changes to the
src/promptflow-core/promptflow/_utils/async_utils.pyfile, aimed at improving the handling of asynchronous tasks and signal interruption. The changes also include minor modifications to several other files in thesrc/promptflow-core/promptflow/directory.Here are the key changes:
Signal Handling and Asynchronous Task Management:
src/promptflow-core/promptflow/_utils/async_utils.py: A new class_AsyncTaskSigIntHandlerwas added to handle SIGINT signals during the execution of asynchronous tasks. This class cancels the current task if a SIGINT signal is received, which is particularly useful for Python versions less than 3.11 where the default cancelling behavior is not supported. The function_invoke_async_with_sigint_handlerwas also added to invoke asynchronous functions with the SIGINT handler. The functionasync_run_allowing_running_loopwas modified to use this new function. [1] [2] [3]Minor Changes in Other Files:
src/promptflow-core/promptflow/_utils/process_utils.py: The signal handler for SIGINT was changed tosignal.default_int_handlerin theblock_terminate_signal_to_parentfunction.src/promptflow-core/promptflow/executor/_async_nodes_scheduler.py: The import of thesignalmodule was removed. In theexecutemethod, the signal handler registration was removed and the exception handling was modified to includeasyncio.CancelledError. Thesignal_handlerfunction was removed and replaced with thecancelmethod. [1] [2] [3] [4]src/promptflow-core/promptflow/executor/flow_executor.py: In the_exec_asyncmethod, the exception handling was modified to handleasyncio.CancelledErrorinstead ofKeyboardInterrupt.All Promptflow Contribution checklist:
General Guidelines and Best Practices
Testing Guidelines