Skip to content

Commit 0aeb747

Browse files
committed
fix: update async context manager exit methods to return None for exception propagation
1 parent 542be3f commit 0aeb747

File tree

3 files changed

+17
-46
lines changed

3 files changed

+17
-46
lines changed

python/copilot/client.py

Lines changed: 3 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -236,30 +236,14 @@ async def __aexit__(
236236
exc_type: Optional[type[BaseException]],
237237
exc_val: Optional[BaseException],
238238
exc_tb: Optional[TracebackType],
239-
) -> bool:
239+
) -> None:
240240
"""
241241
Exit the async context manager.
242242
243243
Performs graceful cleanup by destroying all active sessions and stopping
244-
the CLI server. If a cleanup error occurs and no exception was raised
245-
inside the context, the cleanup error is propagated. If an exception
246-
was already raised inside the context, the cleanup error is suppressed
247-
so the original exception is not masked.
248-
249-
Args:
250-
exc_type: The type of exception that occurred, if any.
251-
exc_val: The exception instance that occurred, if any.
252-
exc_tb: The traceback of the exception that occurred, if any.
253-
254-
Returns:
255-
False to propagate any exception that occurred in the context.
244+
the CLI server.
256245
"""
257-
try:
258-
await self.stop()
259-
except Exception:
260-
if exc_type is None:
261-
raise
262-
return False
246+
await self.stop()
263247

264248
@property
265249
def rpc(self) -> ServerRpc:

python/copilot/session.py

Lines changed: 2 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -108,30 +108,13 @@ async def __aexit__(
108108
exc_type: Optional[type[BaseException]],
109109
exc_val: Optional[BaseException],
110110
exc_tb: Optional[TracebackType],
111-
) -> bool:
111+
) -> None:
112112
"""
113113
Exit the async context manager.
114114
115115
Automatically destroys the session and releases all associated resources.
116-
If a cleanup error occurs and no exception was raised inside the context,
117-
the cleanup error is propagated. If an exception was already raised inside
118-
the context, the cleanup error is suppressed so the original exception is
119-
not masked.
120-
121-
Args:
122-
exc_type: The type of exception that occurred, if any.
123-
exc_val: The exception instance that occurred, if any.
124-
exc_tb: The traceback of the exception that occurred, if any.
125-
126-
Returns:
127-
False to propagate any exception that occurred in the context.
128116
"""
129-
try:
130-
await self.destroy()
131-
except Exception:
132-
if exc_type is None:
133-
raise
134-
return False
117+
await self.destroy()
135118

136119
@property
137120
def rpc(self) -> SessionRpc:

python/test_client.py

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -328,33 +328,37 @@ async def test_client_context_manager_returns_self(self):
328328
await client.force_stop()
329329

330330
@pytest.mark.asyncio
331-
async def test_client_aexit_returns_false(self):
332-
"""Test that __aexit__ returns False to propagate exceptions."""
331+
async def test_client_aexit_returns_none(self):
332+
"""Test that __aexit__ returns None to propagate exceptions."""
333333
client = CopilotClient({"cli_path": CLI_PATH})
334334
await client.start()
335335
result = await client.__aexit__(None, None, None)
336-
assert result is False
336+
assert result is None
337337

338338
@pytest.mark.asyncio
339339
async def test_session_context_manager_returns_self(self):
340340
"""Test that session __aenter__ returns the session instance."""
341341
client = CopilotClient({"cli_path": CLI_PATH})
342342
await client.start()
343343
try:
344-
session = await client.create_session()
344+
session = await client.create_session(
345+
{"on_permission_request": PermissionHandler.approve_all}
346+
)
345347
returned_session = await session.__aenter__()
346348
assert returned_session is session
347349
finally:
348350
await client.force_stop()
349351

350352
@pytest.mark.asyncio
351-
async def test_session_aexit_returns_false(self):
352-
"""Test that session __aexit__ returns False to propagate exceptions."""
353+
async def test_session_aexit_returns_none(self):
354+
"""Test that session __aexit__ returns None to propagate exceptions."""
353355
client = CopilotClient({"cli_path": CLI_PATH})
354356
await client.start()
355357
try:
356-
session = await client.create_session()
358+
session = await client.create_session(
359+
{"on_permission_request": PermissionHandler.approve_all}
360+
)
357361
result = await session.__aexit__(None, None, None)
358-
assert result is False
362+
assert result is None
359363
finally:
360364
await client.force_stop()

0 commit comments

Comments
 (0)