Skip to content

Commit b537cf0

Browse files
author
luk384090-cloud
committed
refactor(server): extract client disconnect response logic
- Moved client disconnect response to a dedicated function _client_disconnect_response - Simplified handler to return the new function's response on disconnect - Improved logging of client disconnect event with request ID - Applied consistent multi-line formatting for async client call - Cleaned up redundant parentheses in exception handling block - Updated import statements to remove unused OSSUploadError - Corrected pylint disable comment for abstract class instantiation in tests - Minor formatting improvements in test logging string concatenation
1 parent e506860 commit b537cf0

6 files changed

Lines changed: 27 additions & 20 deletions

File tree

‎dashscope/applications/finetune/reinforcement/common/constants.py‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ def resolve_fc_api_key(api_key: Optional[str] = None) -> str:
6767
or ""
6868
)
6969

70+
7071
# --------------------------
7172
# Logging Configuration
7273
# --------------------------

‎dashscope/applications/finetune/reinforcement/common/model.py‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1323,7 +1323,11 @@ async def query_function_instance_logs(
13231323
if keywords:
13241324
request_data["keywords"] = keywords
13251325

1326-
result = await client_fc(resolve_fc_api_key(api_key), url, request_data)
1326+
result = await client_fc(
1327+
resolve_fc_api_key(api_key),
1328+
url,
1329+
request_data,
1330+
)
13271331
status = result.get("status", {})
13281332
if isinstance(status, Dict) and status.get("code", 200) != 200:
13291333
raise InstanceQueryError(

‎dashscope/applications/finetune/reinforcement/component/server/server.py‎

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -211,9 +211,7 @@ async def validation_exception_handler(
211211
try:
212212
body_preview = str(deep_mask(json.loads(body_bytes)))[:2000]
213213
except Exception:
214-
body_preview = (
215-
body_bytes.decode("utf-8", errors="ignore")[:200]
216-
)
214+
body_preview = body_bytes.decode("utf-8", errors="ignore")[:200]
217215
logger.debug(f"[Server] Request body (masked): {body_preview}")
218216

219217
return JSONResponse(
@@ -466,6 +464,19 @@ async def _cancel_task_on_disconnect(
466464
return False
467465

468466

467+
def _client_disconnect_response(request: "Request") -> JSONResponse:
468+
"""Build the 499 response returned when the client disconnects."""
469+
logger.warning(
470+
"[Server] Client disconnected during processing, "
471+
"task cancelled. x-fc-request-id: %s",
472+
get_fc_request_id(request),
473+
)
474+
return JSONResponse(
475+
status_code=499,
476+
content={"message": "Client disconnected, request cancelled."},
477+
)
478+
479+
469480
async def _log_request_metrics(
470481
request: "Request",
471482
processor_input,
@@ -567,16 +578,7 @@ async def _listen_for_disconnect():
567578

568579
if disconnected_flag:
569580
cancelled = True
570-
fc_request_id = get_fc_request_id(request)
571-
logger.warning(
572-
"[Server] Client disconnected during processing, "
573-
"task cancelled. x-fc-request-id: %s",
574-
fc_request_id,
575-
)
576-
return JSONResponse(
577-
status_code=499,
578-
content={"message": "Client disconnected, request cancelled."},
579-
)
581+
return _client_disconnect_response(request)
580582

581583
# Processing completed normally
582584
result = process_task.result()

‎tests/legacy_1x/unit/test_agentic_rl_model.py‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
FunctionComponentModel,
1818
FunctionComponentRuntime,
1919
RegistrationError,
20-
OSSUploadError,
2120
OutputError,
2221
get_filepath_classname,
2322
)

‎tests/legacy_1x/unit/test_agentic_rl_review_regressions.py‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,10 @@
4040
to_bailian_data,
4141
)
4242
from dashscope.applications.finetune.reinforcement.common import constants
43-
from dashscope.applications.finetune.reinforcement.component.demo.reward_processor_demo import ( # noqa: E501
43+
from dashscope.applications.finetune.reinforcement.component.demo import (
4444
DemoRewardProcessor,
4545
)
46-
from dashscope.applications.finetune.reinforcement.component.func_manager import ( # noqa: E501
46+
from dashscope.applications.finetune.reinforcement.component.func_manager import (
4747
FuncManager,
4848
)
4949

@@ -252,6 +252,9 @@ async def test_two_instances_do_not_share_metadata(self):
252252
),
253253
)
254254

255+
# ``@reward_func`` injects ``process()`` and clears
256+
# ``__abstractmethods__`` at decoration time, which astroid cannot see.
257+
# pylint: disable=abstract-class-instantiated
255258
p1 = _IsolationProcessor(1.0)
256259
p2 = _IsolationProcessor(2.0)
257260

‎tests/legacy_1x/unit/test_agentic_rl_server_hardening.py‎

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -209,9 +209,7 @@ async def fake_wait_for(awaitable, timeout):
209209
)
210210

211211
assert result is True
212-
logged = " ".join(
213-
str(c) for c in logger_mock.warning.call_args_list
214-
)
212+
logged = " ".join(str(c) for c in logger_mock.warning.call_args_list)
215213
assert "still running" in logged
216214

217215
with contextlib.suppress(asyncio.CancelledError):

0 commit comments

Comments
 (0)