Skip to content

Commit b3ce74f

Browse files
committed
_hnd -> _handle
1 parent 9487486 commit b3ce74f

2 files changed

Lines changed: 16 additions & 18 deletions

File tree

lmdeploy/serve/managers/session_manager.py

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
class Session:
1818
"""Session for the engine."""
1919

20-
def __init__(self, session_id: int, session_mgr: 'SessionManager', **kwargs):
20+
def __init__(self, session_id: int, session_mgr: SessionManager, **kwargs):
2121
self.session_id = session_id
2222
self.prompt: Any = None
2323
self.response: Response | None = None
@@ -26,8 +26,8 @@ def __init__(self, session_id: int, session_mgr: 'SessionManager', **kwargs):
2626
self.step: int = 0
2727
# event to wait for the session to be active
2828
self._active: asyncio.Event | None = None
29-
self._hnd = None # inference instance
30-
self._session_mgr: 'SessionManager' = weakref.ref(session_mgr)
29+
self._handle = None # inference instance
30+
self._session_mgr: SessionManager = weakref.ref(session_mgr)
3131
self.update(**kwargs)
3232

3333
def update(self, **kwargs):
@@ -65,22 +65,22 @@ def reset(self):
6565
self.gen_config = None
6666
self.step = 0
6767
self._active = None
68-
self._hnd = None
69-
del self._session_mgr
68+
self._handle = None
69+
self._session_mgr = None
7070
logger.debug(f'Session {self.session_id} has been reset.')
7171

7272
@asynccontextmanager
7373
async def request_handle(self):
74-
if self._hnd is not None:
74+
if self._handle is not None:
7575
raise RuntimeError(f'Session {self.session_id} already has an inference instance.')
7676
logger.debug(f'[acquire_request_handle] session {self.session_id} acquiring an instance')
7777

78-
hnd_pool = self._session_mgr().request_handle_pool
79-
self._hnd = await hnd_pool.get()
78+
hnd_pool = self._session_mgr().request_handle_pool # 要不要有 ()
79+
self._handle = await hnd_pool.get()
8080
self._active = asyncio.Event()
8181
logger.debug(f'[acquire_request_handle] session {self.session_id} acquired an instance')
8282
try:
83-
yield self._hnd
83+
yield self._handle
8484
except SafeRunException:
8585
# SafeRunException is raised by AsyncEngine.safe_run. We don't need to handle it here.
8686
pass
@@ -90,28 +90,26 @@ async def request_handle(self):
9090
finally:
9191
logger.debug(f'[acquire_request_handle] session {self.session_id} releasing the instance')
9292
# Return inference instance if it was acquired
93-
if self._hnd is not None:
94-
hnd_pool.put(self._hnd)
95-
self._hnd = None
93+
if self._handle is not None:
94+
hnd_pool.put(self._handle)
95+
self._handle = None
9696
# MUST set the signal after releasing the instance to avoid race condition
9797
# refer to async_end method
9898
self._active.set()
9999

100100
async def async_abort(self):
101101
"""Abort the session."""
102102
logger.debug(f'Aborting session {self.session_id}')
103-
if self._hnd is not None:
104-
await self._hnd.async_cancel(self.session_id)
103+
if self._handle is not None:
104+
await self._handle.async_cancel(self.session_id)
105105
# DO NOT reset the session here because it might be used by other components.
106106
# Leave the cleanup to the caller.
107107

108108
async def async_close(self):
109109
"""End the session."""
110110
logger.debug(f'Ending session {self.session_id}')
111-
if self._hnd is not None:
111+
if self._handle is not None:
112112
await self._active.wait()
113-
if self._hnd is not None:
114-
raise RuntimeError(f'Session {self.session_id} is not finished yet.')
115113
async with self.request_handle() as handle:
116114
try:
117115
await handle.async_end(self.session_id)

lmdeploy/serve/openai/api_server.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -622,7 +622,7 @@ async def completion_stream_generator() -> AsyncGenerator[str, None]:
622622
async for res in result_generator:
623623
if await raw_request.is_disconnected():
624624
# Abort the request if the client disconnects.
625-
await session.stop_session()
625+
await session.async_abort()
626626
return create_error_response(HTTPStatus.BAD_REQUEST, 'Client disconnected')
627627
final_res = res
628628
text += res.response

0 commit comments

Comments
 (0)