Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Internal] Fix the bug that Ctrl+C doesn't returns flow result. (#3174)
# 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.py` file, aimed at improving the handling of asynchronous tasks and signal interruption. The changes also include minor modifications to several other files in the `src/promptflow-core/promptflow/` directory. Here are the key changes: ### Signal Handling and Asynchronous Task Management: * [`src/promptflow-core/promptflow/_utils/async_utils.py`](diffhunk://#diff-ebca5e7d6a45f737be917f296f2fb0a3480fa26bc378bde80f11368c9ac2c4fbR7-R8): A new class `_AsyncTaskSigIntHandler` was 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_handler` was also added to invoke asynchronous functions with the SIGINT handler. The function `async_run_allowing_running_loop` was modified to use this new function. [[1]](diffhunk://#diff-ebca5e7d6a45f737be917f296f2fb0a3480fa26bc378bde80f11368c9ac2c4fbR7-R8) [[2]](diffhunk://#diff-ebca5e7d6a45f737be917f296f2fb0a3480fa26bc378bde80f11368c9ac2c4fbR27-R79) [[3]](diffhunk://#diff-ebca5e7d6a45f737be917f296f2fb0a3480fa26bc378bde80f11368c9ac2c4fbL39-R94) ### Minor Changes in Other Files: * [`src/promptflow-core/promptflow/_utils/process_utils.py`](diffhunk://#diff-50bb354dd49c977ba18f8fa38059525658153f097ef6f808f4226cff4d7a8891L32-R32): The signal handler for SIGINT was changed to `signal.default_int_handler` in the `block_terminate_signal_to_parent` function. * [`src/promptflow-core/promptflow/executor/_async_nodes_scheduler.py`](diffhunk://#diff-cd6772e1603e398a564a4d2768ee2f7ffdf7c5e3e0bb7cda5bf19e560f143901L9): The import of the `signal` module was removed. In the `execute` method, the signal handler registration was removed and the exception handling was modified to include `asyncio.CancelledError`. The `signal_handler` function was removed and replaced with the `cancel` method. [[1]](diffhunk://#diff-cd6772e1603e398a564a4d2768ee2f7ffdf7c5e3e0bb7cda5bf19e560f143901L9) [[2]](diffhunk://#diff-cd6772e1603e398a564a4d2768ee2f7ffdf7c5e3e0bb7cda5bf19e560f143901L49-R59) [[3]](diffhunk://#diff-cd6772e1603e398a564a4d2768ee2f7ffdf7c5e3e0bb7cda5bf19e560f143901R77-R81) [[4]](diffhunk://#diff-cd6772e1603e398a564a4d2768ee2f7ffdf7c5e3e0bb7cda5bf19e560f143901L174-L183) * [`src/promptflow-core/promptflow/executor/flow_executor.py`](diffhunk://#diff-bec06607cb28fd791b8ed11bb488979344ca342be5f1c67ba6dd663d5e12240fL1096-R1100): In the `_exec_async` method, the exception handling was modified to handle `asyncio.CancelledError` instead of `KeyboardInterrupt`. # All Promptflow Contribution checklist: - [ ] **The pull request does not introduce [breaking changes].** - [ ] **CHANGELOG is updated for new features, bug fixes or other significant changes.** - [ ] **I have read the [contribution guidelines](../CONTRIBUTING.md).** - [ ] **Create an issue and link to the pull request to get dedicated review from promptflow team. Learn more: [suggested workflow](../CONTRIBUTING.md#suggested-workflow).** ## General Guidelines and Best Practices - [ ] Title of the pull request is clear and informative. - [ ] There are a small number of commits, each of which have an informative message. This means that previously merged commits do not appear in the history of the PR. For more information on cleaning up the commits in your PR, [see this page](https://github.com/Azure/azure-powershell/blob/master/documentation/development-docs/cleaning-up-commits.md). ### Testing Guidelines - [ ] Pull request includes test coverage for the included changes. --------- Co-authored-by: Heyi <heta@microsoft.com>
- Loading branch information