Added timeout and cancellation support to ModelClient polling loop…#559
Open
Timandes wants to merge 1 commit intoalibaba:masterfrom
Open
Added timeout and cancellation support to ModelClient polling loop…#559Timandes wants to merge 1 commit intoalibaba:masterfrom
Timandes wants to merge 1 commit intoalibaba:masterfrom
Conversation
Timandes
added a commit
to Timandes/ROCK
that referenced
this pull request
Mar 4, 2026
…ing loops - Add DEFAULT_POLL_TIMEOUT constant (60 seconds) - Add PollOptions interface for timeout and AbortSignal support - Update popRequest() to accept timeout and cancellation via AbortSignal - Update waitForFirstRequest() to accept timeout and cancellation - Add comprehensive unit tests for timeout and cancellation scenarios Fixes issue where while(true) loops could block forever without timeout or cancellation mechanism. Aligns with Python SDK PR alibaba#559.
d044c6b to
6a34796
Compare
BCeZn
reviewed
Mar 4, 2026
| logger = logging.getLogger(__name__) | ||
|
|
||
| # Default timeout for polling operations (in seconds) | ||
| DEFAULT_POLL_TIMEOUT = 60.0 |
Collaborator
There was a problem hiding this comment.
默认值给大点, 或者说默认值就应该是None, 这个设计是等待Agent Action的
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
Fixes #550 - Add timeout and cancellation support to
ModelClientpolling loops.Problem
ModelClient中的轮询循环 (while True) 缺少超时机制和取消支持,可能导致程序永久阻塞:pop_request方法无限等待请求wait_for_first_request方法无限等待日志文件asyncio.CancelledError取消信号Solution
结合方案一和方案二,实现完整的超时和取消支持:
超时机制
timeout参数,默认值 60 秒time.monotonic()计算超时时间TimeoutError取消支持
asyncio.CancelledErrorChanges
Core Implementation
rock/sdk/model/client.pyDEFAULT_POLL_TIMEOUT = 60.0constantpop_request(index, timeout=DEFAULT_POLL_TIMEOUT)- 添加超时参数wait_for_first_request(timeout=DEFAULT_POLL_TIMEOUT)- 添加超时参数asyncio.CancelledErrorTests
tests/unit/sdk/model/test_model_client.pytest_pop_request_raises_timeout_error_when_timeout_expirestest_wait_for_first_request_raises_timeout_error_when_timeout_expirestest_pop_request_propagates_cancelled_errortest_wait_for_first_request_propagates_cancelled_errorDocumentation
Examples
examples/model_client_demo.py- Comprehensive demo showcasing:Usage Example
Breaking Changes
None. The
timeoutparameter has a sensible default value, making the change backward compatible.Related